Skip to content

Commit

Permalink
feat: Additional suspend measures
Browse files Browse the repository at this point in the history
  • Loading branch information
colin969 committed Mar 18, 2024
1 parent 41dea60 commit e9e2708
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 9 deletions.
37 changes: 30 additions & 7 deletions src/back/SocketServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ type Callback<T, U extends (...args: any[]) => any> = (event: T, ...args: Parame
/** Callback that is registered to all messages. */
type AnyCallback<T, U extends number> = (event: T, type: U, args: any[]) => void

const MAX_RETRIES = 5;

export class SocketServer {
api: BackAPI = create_api();
clients: BackClients = create_server();

retryCounter = 0;

/** Underlying WebSocket server. */
server?: ws.Server;
/** Chosen host */
host: string | undefined = undefined;
/** Port the server is listening on (-1 if not listening). */
port = -1;
/** Secret value used for authentication. */
Expand All @@ -65,15 +71,32 @@ export class SocketServer {
* @param maxPort Maximum port number (tried last).
* @param host Server host (determines what clients can connect).
*/
public listen(minPort: number, maxPort: number, host: string | undefined): Promise<void> {
return startServer(minPort, maxPort, host)
.then(result => {
result.server.on('connection', this.onConnect.bind(this));
this.server = result.server;
this.port = result.port;
});
public async listen(minPort: number, maxPort: number, host: string | undefined): Promise<void> {
this.host = host;
const result = await startServer(this.port !== -1 ? this.port : minPort, this.port !== -1 ? this.port : maxPort, host);
result.server.on('connection', this.onConnect.bind(this));
this.server = result.server;
this.port = result.port;
this.retryCounter = 0; // Reset retries on a good connection
this.server.on('error', this.onError);
}

onError(err: Error) {
if (this.retryCounter < MAX_RETRIES) {
if (this.server) { // Try and close / remove server first
try {
this.server.close();
} catch {/** Ignore closure errors */} finally {
this.server = undefined;
}
}
this.retryCounter++;
setTimeout(() => { // Sleep then try and connect again
this.listen(0,0, this.host)
.catch(this.onError); // If failed to open, keep trying until we get there!
}, 1500);
}
}
/** Close the server. */
public close(): Promise<void> {
return new Promise((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion src/back/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ async function prepForInit(message: any): Promise<void> {

console.log('Back - Opened Websocket');

// Set up general message handler now
// Set up general Main message handler now
process.on('message', onProcessMessage);
state.readyForInit = true;

Expand Down
5 changes: 5 additions & 0 deletions src/back/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ const axios = axiosImport.default;
export function registerRequestCallbacks(state: BackState, init: () => Promise<void>): void {
state.socketServer.register(BackIn.KEEP_ALIVE, () => {});

state.socketServer.register(BackIn.TEST_RECONNECTIONS, async () => {
// Close connections, expect them to restart
state.socketServer.onError(new Error('test'));
});

state.socketServer.register(BackIn.ADD_LOG, (event, data) => {
switch (data.logLevel) {
case LogLevel.TRACE:
Expand Down
5 changes: 5 additions & 0 deletions src/renderer/components/pages/DeveloperPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ export class DeveloperPage extends React.Component<DeveloperPageProps, Developer
value={'Test Tag Sync'}
title={'Test Tag Sync from first Metadata source'}
onClick={this.onTestTagSync} />
<SimpleButton
value={'Test Disconnection'}
onClick={() => {
window.Shared.back.send(BackIn.TEST_RECONNECTIONS);
}}/>
{ this.props.devScripts.map(contribution => contribution.value.map((script, index) => (
<SimpleButton
key={contribution.extId + index}
Expand Down
2 changes: 1 addition & 1 deletion src/shared/back/SocketClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class SocketClient<SOCKET extends BaseSocket> {
if (count < 5) {
console.error(`Failed Connection Attempt: ${error}`);
await new Promise<void>(resolve => {
setTimeout(resolve, 1000);
setTimeout(resolve, 2000);
});
return this.reconnect(count + 1);
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/shared/back/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ export enum BackIn {
// Dialogs
DIALOG_RESPONSE,
NEW_DIALOG_RESPONSE,

// Tests
TEST_RECONNECTIONS,
}

export enum BackOut {
Expand Down Expand Up @@ -386,6 +389,9 @@ export type BackInTemplate = SocketTemplate<BackIn, {
// Dialogs
[BackIn.DIALOG_RESPONSE]: (dialog: DialogState, button: number) => void;
[BackIn.NEW_DIALOG_RESPONSE]: (dialogId: string, responseId: string) => void;

// Tests
[BackIn.TEST_RECONNECTIONS]: () => void;
}>

export type BackOutTemplate = SocketTemplate<BackOut, {
Expand Down

0 comments on commit e9e2708

Please sign in to comment.