Skip to content

Commit

Permalink
fix: connection resource cleanup logic
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 25, 2024
1 parent aa0a573 commit a9eac4b
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/connection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ export class Connection extends EventEmitter implements ConnectionContract {
}
}

/**
* Cleans up reference for the write client and also the
* read client when not using replicas
*/
private cleanupWriteClient() {
if (this.client === this.readClient) {
this.cleanupReadClient()
}
this.client = undefined
}

/**
* Cleans up reference for the read client
*/
private cleanupReadClient() {
this.roundRobinCounter = 0
this.readClient = undefined
this.readReplicas = []
}

/**
* Does cleanup by removing knex reference and removing all listeners.
* For the same of simplicity, we get rid of both read and write
Expand All @@ -117,17 +137,15 @@ export class Connection extends EventEmitter implements ConnectionContract {
*/
this.pool!.on('poolDestroySuccess', () => {
this.logger.trace({ connection: this.name }, 'pool destroyed, cleaning up resource')
this.client = undefined
this.cleanupWriteClient()
this.emit('disconnect', this)
this.removeAllListeners()
})

if (this.readPool !== this.pool) {
this.readPool!.on('poolDestroySuccess', () => {
this.logger.trace({ connection: this.name }, 'pool destroyed, cleaning up resource')
this.roundRobinCounter = 0
this.readClient = undefined
this.readReplicas = []
this.cleanupReadClient()
this.emit('disconnect', this)
this.removeAllListeners()
})
Expand Down

0 comments on commit a9eac4b

Please sign in to comment.