Skip to content

Commit

Permalink
chore(server/healthcheck): remove postgres from readiness probe (#3820)
Browse files Browse the repository at this point in the history
* chore(server/healthcheck): remove postgres from readiness probe
- instead rely on number of available free connections
- reduce period over which we are averaging the available free connections to 4s
  • Loading branch information
iainsproat authored Jan 14, 2025
1 parent 3351e09 commit e349ff3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 36 deletions.
39 changes: 5 additions & 34 deletions packages/server/healthchecks/health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,35 +88,9 @@ export const handleLivenessFactory =

export const handleReadinessFactory = (deps: {
isRedisAlive: RedisCheck
areAllPostgresAlive: MultiDBCheck
getFreeConnectionsCalculators: () => FreeConnectionsCalculators
}): ReadinessHandler => {
return async () => {
const allPostgresResults = await deps.areAllPostgresAlive()
const deadPostgresKeys = Object.entries(allPostgresResults)
.filter((result) => !result[1].isAlive)
.map((result) => result[0])

if (deadPostgresKeys.length) {
throw new ReadinessError(
`Readiness health check failed. Postgres for ${join(
deadPostgresKeys,
', '
)} is not available.`,
{
cause: new MultiError(
Object.entries(allPostgresResults).map((kv) =>
ensureErrorOrWrapAsCause(
//HACK: kv[1] is not typed correctly as the filter does not narrow the type
(kv[1] as { isAlive: false; err: unknown }).err,
'Unknown Postgres error.'
)
)
)
}
)
}

const redisClient = getGenericRedis()
const redisCheck = await deps.isRedisAlive({ client: redisClient })
if (!redisCheck.isAlive) {
Expand Down Expand Up @@ -149,14 +123,11 @@ export const handleReadinessFactory = (deps: {

return {
details: {
postgres: merge(
allPostgresResults,
Object.fromEntries(
Object.entries(percentageFreeConnections).map(([k, v]) => [
k,
{ percentageFreeConnections: v.toFixed(0) }
])
)
postgres: Object.fromEntries(
Object.entries(percentageFreeConnections).map(([k, v]) => [
k,
{ percentageFreeConnections: v.toFixed(0) }
])
),
redis: true
}
Expand Down
3 changes: 1 addition & 2 deletions packages/server/healthchecks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const updateFreeDbConnectionSamplers = async () => {
knexFreeDbConnectionSamplerFactory({
db: dbClient.client,
collectionPeriod: highFrequencyMetricsCollectionPeriodMs(),
sampledDuration: 20_000 //number of ms over which to average the database connections, before declaring unready. 20 seconds.
sampledDuration: 4_000 //number of ms over which to average the database connections, before declaring unready. 4 seconds.
})
knexFreeDbConnectionSamplerReadiness[dbClient.regionKey].start()
}
Expand All @@ -71,7 +71,6 @@ export const initFactory: () => (

const readinessHandler = handleReadinessFactory({
isRedisAlive,
areAllPostgresAlive,
getFreeConnectionsCalculators: getKnexFreeDbConnectionSamplerReadiness
})

Expand Down

0 comments on commit e349ff3

Please sign in to comment.