diff --git a/channel-sender/config/config-local.yaml b/channel-sender/config/config-local.yaml index 306627d..f0c82d7 100644 --- a/channel-sender/config/config-local.yaml +++ b/channel-sender/config/config-local.yaml @@ -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 diff --git a/clients/client-js/src/async-client.ts b/clients/client-js/src/async-client.ts index c37fa9f..ea60ba9 100644 --- a/clients/client-js/src/async-client.ts +++ b/clients/client-js/src/async-client.ts @@ -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)); @@ -91,7 +90,7 @@ export class AsyncClient { } } - private onSocketError(event) { + private onSocketError(_event) { // console.log('error', event) } @@ -99,6 +98,7 @@ export class AsyncClient { 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; @@ -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); diff --git a/clients/client-js/src/retry-timer.ts b/clients/client-js/src/retry-timer.ts index 1feb8e5..73623d5 100644 --- a/clients/client-js/src/retry-timer.ts +++ b/clients/client-js/src/retry-timer.ts @@ -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.`); } diff --git a/clients/client-js/test/retry-timer.test.ts b/clients/client-js/test/retry-timer.test.ts index 54579e5..ad0db7e 100644 --- a/clients/client-js/test/retry-timer.test.ts +++ b/clients/client-js/test/retry-timer.test.ts @@ -12,6 +12,7 @@ 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(() => { @@ -19,8 +20,8 @@ describe('Exponential Retry Timer Tests', function() { 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(); diff --git a/examples/front-async-angular/package-lock.json b/examples/front-async-angular/package-lock.json index f824fa9..527434e 100644 --- a/examples/front-async-angular/package-lock.json +++ b/examples/front-async-angular/package-lock.json @@ -2329,7 +2329,7 @@ "node_modules/@bancolombia/chanjs-client": { "version": "0.2.1", "resolved": "http://localhost:4873/@bancolombia/chanjs-client/-/chanjs-client-0.2.1.tgz", - "integrity": "sha512-HrcG4AEUp4GfZ/3nBuyrDRB42D0C3L6ATYL5t1uEsMxH+QRpf14c6W/urzxCxpFrCZcdD4Z4+yo+4t5+bT4dww==", + "integrity": "sha512-9+BqYeLfxINMkV7SN6iV6ANQ57Z6GMlZVvSsLPTipi+NntZd8XuIxxh26WkQVtCgT7L0SENkprUI24JmNeYH4A==", "dependencies": { "lru-cache": "^10.0.1" }