import { QueryHookPlugin, ConnectionPoolContext } from '../index'; /** * Connection leak details */ export interface ConnectionLeak { connectionId: string; age: number; stackTrace?: string; queries: number; } /** * Options for ConnectionLeakDetectorPlugin */ export interface ConnectionLeakDetectorOptions { /** * Maximum connection age in milliseconds (default: 30000ms / 30 seconds) * Connections held longer than this are considered potential leaks */ maxConnectionAge?: number; /** * Warn threshold as percentage of pool capacity (default: 0.8 / 80%) * Warns when pool usage exceeds this percentage */ warnThreshold?: number; /** * Capture stack trace on connection acquire (default: true) * Helps identify where leaks originate */ captureStackTrace?: boolean; /** * Callback when connection leak is detected (optional) * * @param leak - Connection leak details */ onLeak?: (leak: ConnectionLeak) => void; /** * Callback when pool capacity warning is triggered (optional) * * @param context - Connection pool context */ onPoolWarning?: (context: ConnectionPoolContext) => void; /** * Enable console logging for this plugin (default: false) */ enableLogging?: boolean; } /** * Plugin for detecting database connection leaks * Prevents connection pool exhaustion and app crashes * * 💧 CRITICAL: Connection leaks cause "no available connections" errors * * **The Problem:** * ```typescript * const queryRunner = dataSource.createQueryRunner(); * await queryRunner.connect(); * await queryRunner.query('SELECT ...'); * // FORGOT to call queryRunner.release() !!! * // Connection is leaked - never returned to pool * ``` * * **Common Causes:** * 1. Forgot to call queryRunner.release() * 2. Exception thrown before release() * 3. Early return in function * 4. Async/await mistakes * * **What it detects:** * - Connections held too long * - Pool capacity warnings * - Stack traces of where connections were acquired * * @example * ```typescript * import { ConnectionLeakDetectorPlugin } from 'typeorm-query-hooks/plugins/connection-leak-detector'; * * registerPlugin(ConnectionLeakDetectorPlugin({ * maxConnectionAge: 30000, // 30 seconds * warnThreshold: 0.8, // Warn at 80% capacity * captureStackTrace: true, * enableLogging: true, * onLeak: (leak) => { * logger.error('💧 CONNECTION LEAK DETECTED:', { * age: `${leak.age}ms`, * queries: leak.queries, * stackTrace: leak.stackTrace * }); * * // Send critical alert * monitoring.alert({ * type: 'connection_leak', * severity: 'critical', * connectionId: leak.connectionId, * age: leak.age * }); * }, * onPoolWarning: (context) => { * logger.warn('⚠️ Connection pool capacity warning:', { * active: context.activeConnections, * idle: context.idleConnections, * max: context.maxConnections, * waiting: context.waitingCount * }); * } * })); * ``` */ export declare function ConnectionLeakDetectorPlugin(options?: ConnectionLeakDetectorOptions): QueryHookPlugin; //# sourceMappingURL=connection-leak-detector.d.ts.map