From ad7ab69d362b4a595701e9fc0049bd197b5461ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Arturo=20Cabral=20Mej=C3=ADa?= Date: Fri, 27 Jan 2023 22:06:26 -0500 Subject: [PATCH] fix: patch knex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ricardo Arturo Cabral Mejía --- src/database/client.ts | 28 +++++++++++------------ src/handlers/subscribe-message-handler.ts | 2 +- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/database/client.ts b/src/database/client.ts index ac2fda8..8cacb1c 100644 --- a/src/database/client.ts +++ b/src/database/client.ts @@ -3,20 +3,20 @@ import 'pg-query-stream' import knex, { Knex } from 'knex' import { createLogger } from '../factories/logger-factory' -knex.Client.prototype.releaseConnection = function (connection) { - //debug('releasing connection to pool: %s', connection.__knexUid); - console.log('releasing connection to pool') - const didRelease = this.pool.release(connection) +((knex) => { + knex.Client.prototype.releaseConnection = function (connection) { + const released = this.pool.release(connection) - if (!didRelease) { - console.log('pool refused connection') - //debug('pool refused connection: %s', connection.__knexUid); + if (released) { + console.log(`${this.config.tag} conneciton pool: ${this.pool.numUsed()} used / ${this.pool.numFree()} free / ${this.pool.numPendingAcquires()} pending`) + } + + return Promise.resolve() } - - return Promise.resolve() -} +})(knex) const getMasterConfig = (): Knex.Config => ({ + tag: 'master', client: 'pg', connection: { host: process.env.DB_HOST, @@ -37,9 +37,10 @@ const getMasterConfig = (): Knex.Config => ({ acquireConnectionTimeout: process.env.DB_ACQUIRE_CONNECTION_TIMEOUT ? Number(process.env.DB_ACQUIRE_CONNECTION_TIMEOUT) : 60000, -}) +} as any) const getReadReplicaConfig = (): Knex.Config => ({ + tag: 'read-replica', client: 'pg', connection: { host: process.env.RR_DB_HOST, @@ -57,10 +58,7 @@ const getReadReplicaConfig = (): Knex.Config => ({ ? Number(process.env.RR_DB_ACQUIRE_CONNECTION_TIMEOUT) : 60000, }, - acquireConnectionTimeout: process.env.RR_DB_ACQUIRE_CONNECTION_TIMEOUT - ? Number(process.env.RR_DB_ACQUIRE_CONNECTION_TIMEOUT) - : 60000, -}) +} as any) let writeClient: Knex diff --git a/src/handlers/subscribe-message-handler.ts b/src/handlers/subscribe-message-handler.ts index 63bdaf3..0c93d37 100644 --- a/src/handlers/subscribe-message-handler.ts +++ b/src/handlers/subscribe-message-handler.ts @@ -71,7 +71,7 @@ export class SubscribeMessageHandler implements IMessageHandler, IAbortable { ) } catch (error) { if (error instanceof Error && error.name === 'AbortError') { - debug('subscription aborted: %o', error) + debug('subscription %s aborted: %o', subscriptionId, error) findEvents.destroy() } else { debug('error streaming events: %o', error)