Skip to content

Commit

Permalink
Amo/PTFM-787/reconnection problems (#1)
Browse files Browse the repository at this point in the history
* PTFM-787 Add test log

* PTFM-787 Don't spawn exponential reconnections

* Remove typo
  • Loading branch information
amodelbello authored Feb 6, 2021
1 parent e4e9cfc commit 7bacc19
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/AmqpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export class AmqpClient {
private amqpConfig: AmqpConfig
private exchangeConfig: ExchangeConfig
private consumers: Consumer[] = []
private isReconnecting = false

public connection: Connection
public channel: Channel

Expand Down Expand Up @@ -61,25 +63,32 @@ export class AmqpClient {

this.connection.on('error', (e): void => {
log.error(e)
this.close()
})

this.connection.on('close', this.reconnect.bind(this))
}

private async reconnect(): Promise<void> {
log.warn('AMQP connection closed!')
const { autoReconnect, retryConnectionInterval } = this.amqpConfig

if (autoReconnect) {
log.info('Attempting to reconnect...')
setTimeout(async () => {
try {
await this.init(this.exchangeConfig)
log.warn('Reconnection successful!')
} catch (e) {
log.info('Unable to reconnect: ', e)
}
}, retryConnectionInterval)
if (!this.isReconnecting) {
this.isReconnecting = true
log.warn('AMQP connection closed!')
const { autoReconnect, retryConnectionInterval } = this.amqpConfig

if (autoReconnect) {
log.info('Attempting to reconnect...')
setTimeout(async () => {
try {
await this.init(this.exchangeConfig)
this.isReconnecting = false
log.warn('Reconnection successful!')
} catch (e) {
this.isReconnecting = false
log.info('Unable to reconnect: ', e)
this.reconnect()
}
}, retryConnectionInterval)
}
}
}

Expand Down

0 comments on commit 7bacc19

Please sign in to comment.