Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions src/checks/probe_connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* @adonisjs/redis
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { Result } from '@adonisjs/core/health'
import type { Connection } from '../types.ts'

/**
* Pings a connection, then runs `action`, for the health checks. It never
* connects or reconnects the connection itself, and each run fails after
* its own `timeout` (in milliseconds), which covers the wait for a
* connecting connection, the ping and the action.
*
* ```
* ready, connect ──▶ ping ──▶ action
* connecting ──▶ wait for "ready" ──▶ ping ──▶ action
* └── "close" / "end" ──▶ failed (lastError)
* anything else ──▶ failed at once (lastError)
* timeout elapsed ──▶ failed
* ```
*
* @example
* ```ts
* const result = await probeConnection(connection, 1_000, async () => Result.ok('Redis answered'))
* ```
*/
export async function probeConnection(
connection: Connection,
timeout: number,
action: () => Promise<Result>
): Promise<Result> {
if (!connection.isReady() && connection.status !== 'connecting') {
return Result.failed('Unable to connect to the redis server', connection.lastError)
}

const ioConnection = connection.ioConnection
const statusEvents = ['ready', 'close', 'end']
let onStatusChange: () => void = () => {}
let timer: NodeJS.Timeout | undefined
const timedOut = new Promise<Result>((resolve) => {
timer = setTimeout(() => {
resolve(Result.failed(`Redis did not respond within ${timeout}ms`, connection.lastError))
}, timeout)
})

const probe = async () => {
if (connection.status === 'connecting') {
await new Promise<void>((resolve) => {
onStatusChange = resolve
statusEvents.forEach((event) => ioConnection.once(event, onStatusChange))
})

if (!connection.isReady()) {
return Result.failed('Unable to connect to the redis server', connection.lastError)
}
}

await connection.ping()
return action()
}

/**
* On timeout, the ping stays in the ioredis queue until the server
* answers or the socket closes, so each timed out run on a silent
* connection leaves one PING behind. The application's own commands
* pile up on that connection the same way.
*/
try {
return await Promise.race([probe(), timedOut])
} catch (error) {
return Result.failed(error)
} finally {
clearTimeout(timer)
statusEvents.forEach((event) => ioConnection.off(event, onStatusChange))
}
}
75 changes: 27 additions & 48 deletions src/checks/redis_check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@
* file that was distributed with this source code.
*/

import { setTimeout } from 'node:timers/promises'
import stringHelpers from '@adonisjs/core/helpers/string'
import { BaseCheck, Result } from '@adonisjs/core/health'
import type { HealthCheckResult } from '@adonisjs/core/types/health'
import { probeConnection } from './probe_connection.ts'
import type { Connection } from '../types.ts'

/**
* The RedisCheck pings the redis server to ensure we are
* able to connect to it.
*
* The check fails at once when the connection is neither ready nor
* connecting (for example while reconnecting), and fails when waiting
* for the connection plus the ping take longer than the timeout. It
* never connects or reconnects the connection itself.
*
* @example
* ```ts
* const check = new RedisCheck(redis.connection())
* const check = new RedisCheck(redis.connection()).timeout('2s')
* const result = await check.run()
*
* if (result.isHealthy) {
Expand All @@ -33,9 +39,9 @@ export class RedisCheck extends BaseCheck {
#connection: Connection

/**
* Number of times `ping` was deferred, at max we defer it for 3 times
* Time in milliseconds to wait for the connection and the ping
*/
#pingAttempts = 0
#timeout = 1_000

/**
* Health check public name
Expand Down Expand Up @@ -72,40 +78,20 @@ export class RedisCheck extends BaseCheck {
}

/**
* Internal method to ping the redis server
* Define how long the check waits for a connecting connection and
* the ping, together. Defaults to 1 second.
*
* @param duration - Milliseconds or a duration expression (e.g. '2s')
*
* @example
* ```ts
* check.timeout('2s')
* check.timeout(500)
* ```
*/
async #ping(): Promise<Result | undefined> {
/**
* When in connecting status, we should wait for maximum 3 seconds with
* (divided into 3 attempts). However, if there was an error, we will
* not wait for 3 seconds.
*/
if (this.#connection.isConnecting() && this.#pingAttempts < 3 && !this.#connection.lastError) {
await setTimeout(1000)
this.#pingAttempts++
return this.#ping()
}

/**
* Re-connect when connection is in closed state
*/
if (this.#connection.isClosed()) {
await this.#connection.ioConnection.connect()
return this.#ping()
}

/**
* If we are not in `connect` or `ready` state, then we should
* report an error.
*/
if (!this.#connection.isReady()) {
return Result.failed(
'Unable to connect to the redis server',
this.#connection.lastError
).mergeMetaData(this.#getConnectionMetadata())
}

await this.#connection.ping()
timeout(duration: string | number) {
this.#timeout = stringHelpers.milliseconds.parse(duration)
return this
}

/**
Expand All @@ -118,17 +104,10 @@ export class RedisCheck extends BaseCheck {
* ```
*/
async run(): Promise<HealthCheckResult> {
try {
const result = await this.#ping()
if (result) {
return result
}
const result = await probeConnection(this.#connection, this.#timeout, async () =>
Result.ok('Successfully connected to the redis server')
)

return Result.ok('Successfully connected to the redis server').mergeMetaData(
this.#getConnectionMetadata()
)
} catch (error) {
return Result.failed(error).mergeMetaData(this.#getConnectionMetadata())
}
return result.mergeMetaData(this.#getConnectionMetadata())
}
}
146 changes: 57 additions & 89 deletions src/checks/redis_memory_usage_check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
* file that was distributed with this source code.
*/

import { setTimeout } from 'node:timers/promises'
import stringHelpers from '@adonisjs/core/helpers/string'
import { BaseCheck, Result } from '@adonisjs/core/health'
import type { HealthCheckResult } from '@adonisjs/core/types/health'
import * as errors from '../errors.ts'
import { probeConnection } from './probe_connection.ts'

import type { Connection } from '../types.ts'

Expand All @@ -37,9 +37,10 @@ export class RedisMemoryUsageCheck extends BaseCheck {
#connection: Connection

/**
* Number of times `ping` was deferred, at max we defer it for 3 times
* Time in milliseconds to wait for the connection, the ping and the
* memory info
*/
#pingAttempts = 0
#timeout = 1_000

/**
* Method to compute the memory consumption
Expand Down Expand Up @@ -119,43 +120,6 @@ export class RedisMemoryUsageCheck extends BaseCheck {
}
}

/**
* Internal method to ping the redis server
*/
async #ping(): Promise<Result | undefined> {
/**
* When in connecting status, we should wait for maximum 3 seconds with
* (divided into 3 attempts). However, if there was an error, we will
* not wait for 3 seconds.
*/
if (this.#connection.isConnecting() && this.#pingAttempts < 3 && !this.#connection.lastError) {
await setTimeout(1000)
this.#pingAttempts++
return this.#ping()
}

/**
* Re-connect when connection is in closed state
*/
if (this.#connection.isClosed()) {
await this.#connection.ioConnection.connect()
return this.#ping()
}

/**
* If we are not in `connect` or `ready` state, then we should
* report an error.
*/
if (!this.#connection.isReady()) {
return Result.failed(
'Unable to connect to the redis server',
this.#connection.lastError
).mergeMetaData(this.#getConnectionMetadata())
}

await this.#connection.ping()
}

/**
* Define the memory threshold after which a warning
* should be created.
Expand Down Expand Up @@ -222,6 +186,22 @@ export class RedisMemoryUsageCheck extends BaseCheck {
return this
}

/**
* Define how long the check waits for a connecting connection, the
* ping and the memory info, together. Defaults to 1 second.
*
* @param duration - Milliseconds or a duration expression (e.g. '2s')
*
* @example
* ```ts
* check.timeout('2s')
* ```
*/
timeout(duration: string | number) {
this.#timeout = stringHelpers.milliseconds.parse(duration)
return this
}

/**
* Executes the health check
*
Expand All @@ -234,60 +214,48 @@ export class RedisMemoryUsageCheck extends BaseCheck {
* ```
*/
async run(): Promise<HealthCheckResult> {
try {
const result = await this.#ping()
if (result) {
return result
}

/**
* Get memory usage when tracking memory
*/
const memoryUsage = await this.#computeFn(this.#connection)
const result = await probeConnection(this.#connection, this.#timeout, () => this.#checkMemory())
return result.mergeMetaData(this.#getConnectionMetadata())
}

/**
* Return early when we do not have access to the memory
* usage.
*/
if (!memoryUsage) {
return Result.failed('Check failed. Unable to get redis memory info').mergeMetaData(
this.#getConnectionMetadata()
)
}
/**
* Computes the memory usage and compares it with the thresholds
*/
async #checkMemory(): Promise<Result> {
const memoryUsage = await this.#computeFn(this.#connection)

const memoryUsagePretty = stringHelpers.bytes.format(memoryUsage)
const warnThresholdPretty = stringHelpers.bytes.format(this.#warnThreshold)
const failureThresholdPretty = stringHelpers.bytes.format(this.#failThreshold)
/**
* Return early when we do not have access to the memory
* usage.
*/
if (!memoryUsage) {
return Result.failed('Check failed. Unable to get redis memory info')
}

/**
* Check if we have crossed the failure threshold
*/
if (this.#failThreshold && memoryUsage > this.#failThreshold) {
return Result.failed(
`Redis memory usage is ${memoryUsagePretty}, which is above the threshold of ${failureThresholdPretty}`
)
.mergeMetaData(this.#getConnectionMetadata())
.mergeMetaData(this.#getMemoryMetadata(memoryUsage))
}
const memoryUsagePretty = stringHelpers.bytes.format(memoryUsage)
const warnThresholdPretty = stringHelpers.bytes.format(this.#warnThreshold)
const failureThresholdPretty = stringHelpers.bytes.format(this.#failThreshold)

/**
* Check if we have crossed the warning threshold
*/
if (this.#warnThreshold && memoryUsage > this.#warnThreshold) {
return Result.warning(
`Redis memory usage is ${stringHelpers.bytes.format(memoryUsage)}, which is above the threshold of ${warnThresholdPretty}`
)
.mergeMetaData(this.#getConnectionMetadata())
.mergeMetaData(this.#getMemoryMetadata(memoryUsage))
}
/**
* Check if we have crossed the failure threshold
*/
if (this.#failThreshold && memoryUsage > this.#failThreshold) {
return Result.failed(
`Redis memory usage is ${memoryUsagePretty}, which is above the threshold of ${failureThresholdPretty}`
).mergeMetaData(this.#getMemoryMetadata(memoryUsage))
}

return Result.ok(
`Redis memory usage is ${memoryUsagePretty}, which is under the defined thresholds`
)
.mergeMetaData(this.#getConnectionMetadata())
.mergeMetaData(this.#getMemoryMetadata(memoryUsage))
} catch (error) {
return Result.failed(error).mergeMetaData(this.#getConnectionMetadata())
/**
* Check if we have crossed the warning threshold
*/
if (this.#warnThreshold && memoryUsage > this.#warnThreshold) {
return Result.warning(
`Redis memory usage is ${memoryUsagePretty}, which is above the threshold of ${warnThresholdPretty}`
).mergeMetaData(this.#getMemoryMetadata(memoryUsage))
}

return Result.ok(
`Redis memory usage is ${memoryUsagePretty}, which is under the defined thresholds`
).mergeMetaData(this.#getMemoryMetadata(memoryUsage))
}
}
Loading
Loading