Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

perf(backend:database): avoid polling healthy connections

johaven847eb8323d5c
diff --git a/backend/src/infrastructure/availability/availability.md b/backend/src/infrastructure/availability/availability.md
index 57eb6cfd..4382331e 100644
--- a/backend/src/infrastructure/availability/availability.md
+++ b/backend/src/infrastructure/availability/availability.md
@@ -11,6 +11,14 @@ this.availability.register(INFRASTRUCTURE_DEPENDENCY.DATABASE)
this.availability.setAvailable(INFRASTRUCTURE_DEPENDENCY.DATABASE, true)
```
+The database runs `SELECT 1` until MySQL is available during startup. It does not send periodic health queries after that initial connection. The
+default mysql2 pool configuration retains idle connections, and an error from any established pooled connection marks the database unavailable and
+starts a single `SELECT 1` recovery loop. The loop stops as soon as MySQL accepts queries again.
+
+This event-driven behavior avoids database traffic while the service is healthy. A silent network interruption may remain undetected until the TCP
+socket or the next application query reports it. Any future pool configuration that evicts every idle connection must also account for connection
+acquisition failures, which occur before mysql2 emits the pool's `connection` event.
+
`AvailabilityGuard` is registered as an application guard. It returns HTTP `503 Service unavailable` while at least one registered dependency is
unavailable.
diff --git a/backend/src/infrastructure/database/database.module.spec.ts b/backend/src/infrastructure/database/database.module.spec.ts
new file mode 100644
index 00000000..fc2d7b9a
--- /dev/null
+++ b/backend/src/infrastructure/database/database.module.spec.ts
@@ -0,0 +1,121 @@
+import { INFRASTRUCTURE_DEPENDENCY } from '../availability/availability.constants.js'
+import { Availability } from '../availability/availability.service.js'
+import { DatabaseModule } from './database.module.js'
+
+describe(DatabaseModule.name, () => {
+ let connectionListener: (connection: any) => void
+ let query: ReturnType<typeof vi.fn>
+ let end: ReturnType<typeof vi.fn>
+ let availability: Availability
+ let databaseModule: DatabaseModule
+
+ const addPoolConnection = (): ((error: unknown) => void) => {
+ let errorListener: (error: unknown) => void = () => undefined
+ const connection = {
+ once: vi.fn((event: string, listener: (error: unknown) => void) => {
+ if (event === 'error') errorListener = listener
+ }),
+ query: vi.fn()
+ }
+ connectionListener(connection)
+ return (error: unknown) => errorListener(error)
+ }
+
+ beforeEach(() => {
+ query = vi.fn().mockResolvedValue([])
+ end = vi.fn().mockResolvedValue(undefined)
+ const promisePool = { query, end }
+ const pool = {
+ on: vi.fn((event: string, listener: typeof connectionListener) => {
+ if (event === 'connection') connectionListener = listener
+ }),
+ promise: vi.fn(() => promisePool)
+ }
+ availability = new Availability()
+ databaseModule = new DatabaseModule({ $client: pool } as any, availability)
+ vi.spyOn(databaseModule['logger'], 'error').mockImplementation(() => undefined)
+ vi.spyOn(databaseModule['logger'], 'log').mockImplementation(() => undefined)
+ vi.spyOn(databaseModule['logger'], 'warn').mockImplementation(() => undefined)
+ })
+
+ afterEach(async () => {
+ await databaseModule.beforeApplicationShutdown()
+ })
+
+ it('does not poll MySQL after the initial connection succeeds', async () => {
+ const waitRetryDelay = vi.spyOn(databaseModule as any, 'waitRetryDelay').mockResolvedValue(false)
+
+ await databaseModule.onModuleInit()
+
+ expect(query).toHaveBeenCalledOnce()
+ expect(query).toHaveBeenCalledWith('SELECT 1')
+ expect(waitRetryDelay).not.toHaveBeenCalled()
+ expect(availability.isAvailable(INFRASTRUCTURE_DEPENDENCY.DATABASE)).toBe(true)
+ })
+
+ it('starts only one immediate recovery when pooled connections report errors', async () => {
+ await databaseModule.onModuleInit()
+ const firstConnectionError = addPoolConnection()
+ const secondConnectionError = addPoolConnection()
+ let resolveRecovery: (value: unknown) => void = () => undefined
+ query.mockImplementationOnce(
+ () =>
+ new Promise((resolve) => {
+ resolveRecovery = resolve
+ })
+ )
+ const connectionError = Object.assign(new Error('Connection lost'), { code: 'ECONNRESET' })
+
+ firstConnectionError(connectionError)
+ const recoveryPromise = databaseModule['recoveryPromise']
+ secondConnectionError(connectionError)
+
+ expect(availability.isAvailable(INFRASTRUCTURE_DEPENDENCY.DATABASE)).toBe(false)
+ expect(query).toHaveBeenCalledTimes(2)
+ expect(databaseModule['recoveryPromise']).toBe(recoveryPromise)
+
+ resolveRecovery([])
+ await recoveryPromise
+
+ expect(availability.isAvailable(INFRASTRUCTURE_DEPENDENCY.DATABASE)).toBe(true)
+ expect(databaseModule['recoveryPromise']).toBeUndefined()
+ })
+
+ it('retries a recoverable connection failure and stops after the first success', async () => {
+ await databaseModule.onModuleInit()
+ const connectionError = addPoolConnection()
+ const retryableError = Object.assign(new Error('Connection refused'), { code: 'ECONNREFUSED' })
+ query.mockRejectedValueOnce(retryableError).mockResolvedValueOnce([])
+ const waitRetryDelay = vi.spyOn(databaseModule as any, 'waitRetryDelay').mockResolvedValue(true)
+
+ connectionError(retryableError)
+ await databaseModule['recoveryPromise']
+
+ expect(query).toHaveBeenCalledTimes(3)
+ expect(waitRetryDelay).toHaveBeenCalledOnce()
+ expect(availability.isAvailable(INFRASTRUCTURE_DEPENDENCY.DATABASE)).toBe(true)
+ expect(databaseModule['recoveryPromise']).toBeUndefined()
+ })
+
+ it('does not restore availability when an in-flight recovery finishes during shutdown', async () => {
+ await databaseModule.onModuleInit()
+ const connectionError = addPoolConnection()
+ let resolveRecovery: (value: unknown) => void = () => undefined
+ query.mockImplementationOnce(
+ () =>
+ new Promise((resolve) => {
+ resolveRecovery = resolve
+ })
+ )
+
+ connectionError(Object.assign(new Error('Connection lost'), { code: 'ECONNRESET' }))
+ const shutdownPromise = databaseModule.beforeApplicationShutdown()
+
+ expect(end).not.toHaveBeenCalled()
+ resolveRecovery([])
+ await shutdownPromise
+
+ expect(availability.isAvailable(INFRASTRUCTURE_DEPENDENCY.DATABASE)).toBe(false)
+ expect(end).toHaveBeenCalledOnce()
+ })
+})
diff --git a/backend/src/infrastructure/database/database.module.ts b/backend/src/infrastructure/database/database.module.ts
index 260ff540..0a029e12 100644
--- a/backend/src/infrastructure/database/database.module.ts
+++ b/backend/src/infrastructure/database/database.module.ts
@@ -34,7 +34,7 @@ export class DatabaseModule implements OnModuleInit, BeforeApplicationShutdown {
private readonly logger = new Logger(DatabaseModule.name)
private readonly pool: Pool
private readonly shutdownController = new AbortController()
- private monitorPromise?: Promise<void>
+ private recoveryPromise?: Promise<void>
private poolClosed = false
constructor(
@@ -47,38 +47,41 @@ export class DatabaseModule implements OnModuleInit, BeforeApplicationShutdown {
async onModuleInit(): Promise<void> {
this.pool.on('connection', (conn: Connection) => {
- conn.on('error', () => this.availability.setAvailable(INFRASTRUCTURE_DEPENDENCY.DATABASE, false))
+ conn.once('error', (error: unknown) => this.handleConnectionError(error))
for (const query of DB_SESSION_INIT_QUERIES) {
conn.query(query)
}
})
await this.waitUntilAvailable()
- if (!this.shutdownController.signal.aborted) {
- this.monitorPromise = this.monitorAvailability()
- }
}
async beforeApplicationShutdown(): Promise<void> {
this.availability.setAvailable(INFRASTRUCTURE_DEPENDENCY.DATABASE, false)
this.shutdownController.abort()
- await this.monitorPromise
+ await this.recoveryPromise
await this.closePool()
}
- private async waitUntilAvailable(): Promise<void> {
+ private async waitUntilAvailable(isRecovery = false): Promise<void> {
while (!this.shutdownController.signal.aborted) {
try {
await this.pool.promise().query('SELECT 1')
+ if (this.shutdownController.signal.aborted) return
this.availability.setAvailable(INFRASTRUCTURE_DEPENDENCY.DATABASE, true)
- this.logger.log('Connected to MySQL server')
+ this.logger.log(isRecovery ? 'Connection to MySQL server restored' : 'Connected to MySQL server')
return
} catch (error) {
this.availability.setAvailable(INFRASTRUCTURE_DEPENDENCY.DATABASE, false)
- const message = `Unable to connect to MySQL server: ${connectionErrorMessage(error)}`
+ const message = `Unable to ${isRecovery ? 'reconnect to' : 'connect to'} MySQL server: ${connectionErrorMessage(error)}`
if (!isRetryableConnectionError(error)) {
- await this.closePool()
- throw new Error(message)
+ if (!isRecovery) {
+ await this.closePool()
+ throw new Error(message)
+ }
+ this.logger.error(message)
+ this.logger.error('MySQL connection cannot be retried without changing the configuration')
+ return
}
this.logger.error(message)
this.logger.warn(`Retrying connection to MySQL server in ${INFRASTRUCTURE_CONNECTION_RETRY_DELAY / 1000}s`)
@@ -87,24 +90,20 @@ export class DatabaseModule implements OnModuleInit, BeforeApplicationShutdown {
}
}
- private async monitorAvailability(): Promise<void> {
- while (await this.waitRetryDelay()) {
- try {
- await this.pool.promise().query('SELECT 1')
- if (!this.availability.isAvailable(INFRASTRUCTURE_DEPENDENCY.DATABASE)) {
- this.logger.log('Connection to MySQL server restored')
- }
- this.availability.setAvailable(INFRASTRUCTURE_DEPENDENCY.DATABASE, true)
- } catch (error) {
- this.availability.setAvailable(INFRASTRUCTURE_DEPENDENCY.DATABASE, false)
- const message = `Connection to MySQL server lost: ${connectionErrorMessage(error)}`
- this.logger.error(message)
- if (!isRetryableConnectionError(error)) {
- this.logger.error('MySQL connection cannot be retried without changing the configuration')
- return
- }
- this.logger.warn(`Retrying connection to MySQL server in ${INFRASTRUCTURE_CONNECTION_RETRY_DELAY / 1000}s`)
- }
+ private handleConnectionError(error: unknown): void {
+ const wasAvailable = this.availability.isAvailable(INFRASTRUCTURE_DEPENDENCY.DATABASE)
+ this.availability.setAvailable(INFRASTRUCTURE_DEPENDENCY.DATABASE, false)
+ if (!wasAvailable || this.shutdownController.signal.aborted || this.recoveryPromise) return
+
+ this.logger.error(`Connection to MySQL server lost: ${connectionErrorMessage(error)}`)
+ this.recoveryPromise = this.recoverAvailability()
+ }
+
+ private async recoverAvailability(): Promise<void> {
+ try {
+ await this.waitUntilAvailable(true)
+ } finally {
+ this.recoveryPromise = undefined
}
}