Skip to content

Commit 5f5bd94

Browse files
authored
Use service connection pools for health checks + db pool configuration (#248)
1 parent b348c73 commit 5f5bd94

File tree

3 files changed

+88
-18
lines changed

3 files changed

+88
-18
lines changed

src/services/health-service/index.ts

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { SystemHealth, ListingStatus } from 'onecore-types'
33
import config from '../../common/config'
44
import { healthCheck as xpandSoapApiHealthCheck } from '../lease-service/adapters/xpand/xpand-soap-adapter'
55
import { healthCheck as creditSafeHealthCheck } from '../creditsafe/adapters/creditsafe-adapter'
6-
import knex from 'knex'
6+
import { db as leasingDb } from '../lease-service/adapters/db'
7+
import { xpandDb } from '../lease-service/adapters/xpand/xpandDb'
78

89
const healthChecks: Map<string, SystemHealth> = new Map()
910

@@ -69,12 +70,7 @@ const subsystems = [
6970
config.health.leasingDatabase.systemName,
7071
config.health.leasingDatabase.minimumMinutesBetweenRequests,
7172
async () => {
72-
const db = knex({
73-
client: 'mssql',
74-
connection: config.leasingDatabase,
75-
})
76-
77-
await db.table('listing').limit(1)
73+
await leasingDb.table('listing').limit(1)
7874
}
7975
)
8076
},
@@ -85,12 +81,7 @@ const subsystems = [
8581
config.health.xpandDatabase.systemName,
8682
config.health.xpandDatabase.minimumMinutesBetweenRequests,
8783
async () => {
88-
const db = knex({
89-
client: 'mssql',
90-
connection: config.xpandDatabase,
91-
})
92-
93-
await db.table('cmctc').limit(1)
84+
await xpandDb.table('cmctc').limit(1)
9485
}
9586
)
9687
},
@@ -101,11 +92,7 @@ const subsystems = [
10192
config.health.expiredListingsScript.systemName,
10293
config.health.expiredListingsScript.minimumMinutesBetweenRequests,
10394
async () => {
104-
const db = knex({
105-
client: 'mssql',
106-
connection: config.leasingDatabase,
107-
})
108-
const expiredActiveListings = await db('listing')
95+
const expiredActiveListings = await leasingDb('listing')
10996
.where('PublishedTo', '<', new Date(Date.now() - 86400000))
11097
.andWhere('Status', ListingStatus.Active)
11198

@@ -225,4 +212,75 @@ export const routes = (router: KoaRouter) => {
225212

226213
ctx.body = health
227214
})
215+
216+
const CONNECTIONS = [
217+
{
218+
name: 'leasing',
219+
pool: leasingDb,
220+
},
221+
{
222+
name: 'xpand',
223+
pool: xpandDb,
224+
},
225+
]
226+
227+
/**
228+
* @openapi
229+
* /health/db:
230+
* get:
231+
* summary: Database connection pool metrics
232+
* tags: [Health]
233+
* responses:
234+
* '200':
235+
* description: Connection pool stats per configured DB connection.
236+
* content:
237+
* application/json:
238+
* schema:
239+
* type: object
240+
* properties:
241+
* connectionPools:
242+
* type: integer
243+
* minimum: 0
244+
* metrics:
245+
* type: array
246+
* items:
247+
* type: object
248+
* properties:
249+
* name:
250+
* type: string
251+
* pool:
252+
* type: object
253+
* properties:
254+
* used: { type: integer, minimum: 0 }
255+
* free: { type: integer, minimum: 0 }
256+
* pendingCreates: { type: integer, minimum: 0 }
257+
* pendingAcquires: { type: integer, minimum: 0 }
258+
* examples:
259+
* sample:
260+
* value:
261+
* connectionPools: 2
262+
* metrics:
263+
* - name: "primary"
264+
* pool: { used: 3, free: 5, pendingCreates: 0, pendingAcquires: 1 }
265+
* - name: "reporting"
266+
* pool: { used: 0, free: 8, pendingCreates: 0, pendingAcquires: 0 }
267+
*/
268+
router.get('(.*)/health/db', async (ctx) => {
269+
ctx.body = {
270+
connectionPools: CONNECTIONS.length,
271+
metrics: CONNECTIONS.map((conn) => {
272+
const pool = conn.pool.client.pool
273+
274+
return {
275+
name: conn.name,
276+
pool: {
277+
used: pool.numUsed(),
278+
free: pool.numFree(),
279+
pendingCreates: pool.numPendingCreates(),
280+
pendingAcquires: pool.numPendingAcquires(),
281+
},
282+
}
283+
}),
284+
}
285+
})
228286
}

src/services/lease-service/adapters/db.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ export const createDbClient = () =>
66
knex({
77
client: 'mssql',
88
connection: Config.leasingDatabase,
9+
pool: {
10+
min: 2,
11+
max: 20,
12+
idleTimeoutMillis: 30000,
13+
destroyTimeoutMillis: 5000,
14+
},
915
})
1016

1117
export const db = createDbClient()

src/services/lease-service/adapters/xpand/xpandDb.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ export const createXpandDbClient = () =>
66
knex({
77
client: 'mssql',
88
connection: Config.xpandDatabase,
9+
pool: {
10+
min: 2,
11+
max: 20,
12+
idleTimeoutMillis: 30000,
13+
destroyTimeoutMillis: 5000,
14+
},
915
})
1016

1117
export const xpandDb = createXpandDbClient()

0 commit comments

Comments
 (0)