Skip to content

Commit

Permalink
fix(reconnect): Manage max retries when connection error
Browse files Browse the repository at this point in the history
  • Loading branch information
juancgalvis committed Feb 5, 2025
1 parent 0b185d6 commit 3114ae3
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion channel-sender/config/config-local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ channel_sender_ex:
# initial time in seconds to wait before re-send a message not acked to a channel
initial_redelivery_time: 900

# max time in seconds to wait the client to send the auth token
# max time in milliseconds to wait the client to send the auth token
# before closing the channel
socket_idle_timeout: 90000

Expand Down
5 changes: 3 additions & 2 deletions clients/client-js/src/async-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ export class AsyncClient {
private onSocketOpen(event) {
this.selectSerializerForProtocol();
this.isOpen = true;
this.reconnectTimer.reset();
this.resetHeartbeat();
this.socket.send(`Auth::${this.actualToken}`)
this.stateCallbacks.open.forEach((callback) => callback(event));
Expand All @@ -91,14 +90,15 @@ export class AsyncClient {
}
}

private onSocketError(event) {
private onSocketError(_event) {
// console.log('error', event)
}

private onSocketMessage(event) {
const message = this.serializer.decode(event)
if (!this.isActive && message.event == "AuthOk") {
this.isActive = true;
this.reconnectTimer.reset();
console.log('async-client. Auth OK');
} else if (message.event == ":hb" && message.correlation_id == this.pendingHeartbeatRef) {
this.pendingHeartbeatRef = null;
Expand Down Expand Up @@ -182,6 +182,7 @@ export class AsyncClient {
}

private onSocketClose(event) {
this.isActive = false;
console.warn(`async-client. channel close: ${event.code} ${event.reason}`);
clearInterval(this.heartbeatTimer)
const reason = this.extractReason(event.reason);
Expand Down
14 changes: 8 additions & 6 deletions clients/client-js/src/retry-timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ export class RetryTimer {

public schedule(): void {
if (typeof window !== 'undefined') {
const delayMS = this.delay();
console.log(`async-client. scheduling retry in ${delayMS} ms`);
this.timer = window.setTimeout(() => {
if (this.tries <= this.maxRetries) {
const delayMS = this.delay();
this.tries = this.tries + 1;
if (this.tries <= this.maxRetries) {
console.log(`async-client. scheduling retry in ${delayMS} ms`);
this.timer = window.setTimeout(() => {
console.log(`async-client. retrying ${this.tries} of ${this.maxRetries}`);
this.callback();
}
}, delayMS)
}, delayMS)
} else {
console.log(`async-client. max retries reached.`);
}
} else {
console.warn(`async-client. could not setup scheduler for rety: window is undefined.`);
}
Expand Down
5 changes: 3 additions & 2 deletions clients/client-js/test/retry-timer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ describe('Exponential Retry Timer Tests', function() {
let counter = 0;
let lastTime = Date.now();
let times: number[] = [];
const maxRetries = 7;

let retryProcess = new Promise(resolve => {
timer = new RetryTimer(() => {
let now = Date.now();
times.push(now - lastTime);
lastTime = now;
counter = counter + 1;
counter < 7 ? timer.schedule() : resolve(0);
}, 10, x => x );
counter < maxRetries ? timer.schedule() : resolve(0);
}, 10, x => x, maxRetries);
});

timer.schedule();
Expand Down
2 changes: 1 addition & 1 deletion examples/front-async-angular/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3114ae3

Please sign in to comment.