Skip to content

Commit

Permalink
improve retry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
SanterreJo committed Feb 27, 2024
1 parent a683db8 commit 185d601
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/hilo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class Hilo implements DynamicPlatformPlugin {
axios.isAxiosError(error) ? error.response?.data : error
);
this.retryWebsocketConnection();
return;
}
if (!url) return;
this.wsConnection = new signalR.HubConnectionBuilder()
Expand Down Expand Up @@ -208,6 +209,7 @@ class Hilo implements DynamicPlatformPlugin {
);
} catch (e) {
this.log.error(`Unable to subscribe to location ${location.id}`, e);
this.stopWebsocket();
this.retryWebsocketConnection();
return;
}
Expand All @@ -216,22 +218,21 @@ class Hilo implements DynamicPlatformPlugin {
}

retryWebsocketConnection() {
const backoff = 2 ** this.webSocketRetries * 30_000;
this.log.info(
`Attempting to reconnect to websocket in ${backoff / 1000} seconds`
);
if (this.webSocketRetries < 8) {
setTimeout(async () => {
this.webSocketRetries++;
this.log.info(`Reconnection attempt ${this.webSocketRetries}`);
this.stopWebsocket();
this.setupWebsocketConnection();
}, backoff);
} else {
if (this.webSocketRetries > 8) {
this.log.error(
`Unable to reconnect to websocket after ${this.webSocketRetries} attempts`
);
return;
}
const backoff = 2 ** this.webSocketRetries * 30_000;
this.log.info(
`Attempting to reconnect to websocket in ${backoff / 1000} seconds`
);
setTimeout(async () => {
this.webSocketRetries++;
this.log.info(`Reconnection attempt ${this.webSocketRetries}`);
this.setupWebsocketConnection();
}, backoff);
}

stopWebsocket() {
Expand Down

0 comments on commit 185d601

Please sign in to comment.