{"version":3,"file":"details.mjs","sources":["../../src/pool-diagnostics/details.ts"],"sourcesContent":["import type { EventLoopDelay } from './event-loop';\n\nexport const POOL_TIMEOUT_CODE = 'DB_POOL_ACQUIRE_TIMEOUT';\n\nexport interface PoolState {\n  used: number;\n  free: number;\n  pendingAcquires: number;\n  pendingCreates: number;\n  min?: number;\n  max?: number;\n}\n\nexport interface PoolTimeoutDetails {\n  code: typeof POOL_TIMEOUT_CODE;\n  /** How long this caller waited before knex gave up. */\n  waitedMs: number;\n  /** The timeout knex applied: the smaller of acquireConnectionTimeout and pool.acquireTimeoutMillis. */\n  acquireTimeoutMs?: number;\n  /** As configured, when set. */\n  acquireConnectionTimeout?: number;\n  poolAcquireTimeoutMillis?: number;\n  /** Pool counts at the moment of the timeout. */\n  pool?: PoolState;\n  /** tarn's message: the error of a failed connection attempt, or \"operation timed out for an unknown reason\". */\n  poolReason?: string;\n  eventLoopDelay?: EventLoopDelay;\n  phase?: string;\n  hostname: string;\n  pid: number;\n}\n\nexport interface TarnPoolLike {\n  min?: number;\n  max?: number;\n  acquireTimeoutMillis?: number;\n  numUsed(): number;\n  numFree(): number;\n  numPendingAcquires(): number;\n  numPendingCreates(): number;\n}\n\nexport interface KnexClientLike {\n  config?: { acquireConnectionTimeout?: unknown; pool?: { acquireTimeoutMillis?: unknown } };\n  /** A tarn pool, or knex's wrapper around a native driver pool, which has no counters. */\n  pool?: Partial<TarnPoolLike> | null;\n}\n\nexport interface CollectInput {\n  waitedMs: number;\n  cause: unknown;\n  eventLoopDelay?: EventLoopDelay;\n  phase?: string;\n  hostname: string;\n  pid: number;\n}\n\nconst asNumber = (value: unknown) =>\n  typeof value === 'number' && Number.isFinite(value) ? value : undefined;\n\nconst hasCounters = (pool: Partial<TarnPoolLike>): pool is TarnPoolLike =>\n  typeof pool.numUsed === 'function' &&\n  typeof pool.numFree === 'function' &&\n  typeof pool.numPendingAcquires === 'function' &&\n  typeof pool.numPendingCreates === 'function';\n\nexport const readPoolState = (\n  pool: Partial<TarnPoolLike> | null | undefined\n): PoolState | undefined => {\n  // knex also accepts a pool that only has acquire, release and destroy\n  if (!pool || !hasCounters(pool)) {\n    return undefined;\n  }\n\n  return {\n    used: pool.numUsed(),\n    free: pool.numFree(),\n    pendingAcquires: pool.numPendingAcquires(),\n    pendingCreates: pool.numPendingCreates(),\n    min: asNumber(pool.min),\n    max: asNumber(pool.max),\n  };\n};\n\nexport const collectPoolTimeoutDetails = (\n  client: KnexClientLike,\n  input: CollectInput\n): PoolTimeoutDetails => {\n  const config = client.config ?? {};\n\n  return {\n    code: POOL_TIMEOUT_CODE,\n    waitedMs: input.waitedMs,\n    // knex writes the effective (smaller) timeout into the tarn pool options\n    acquireTimeoutMs: asNumber(client.pool?.acquireTimeoutMillis),\n    acquireConnectionTimeout: asNumber(config.acquireConnectionTimeout),\n    poolAcquireTimeoutMillis: asNumber(config.pool?.acquireTimeoutMillis),\n    pool: readPoolState(client.pool),\n    // knex 3.3 keeps tarn's error as `cause`; its message is all tarn keeps of a failed attempt\n    poolReason: input.cause instanceof Error ? input.cause.message : undefined,\n    eventLoopDelay: input.eventLoopDelay,\n    phase: input.phase,\n    hostname: input.hostname,\n    pid: input.pid,\n  };\n};\n"],"names":["POOL_TIMEOUT_CODE","asNumber","value","Number","isFinite","undefined","hasCounters","pool","numUsed","numFree","numPendingAcquires","numPendingCreates","readPoolState","used","free","pendingAcquires","pendingCreates","min","max","collectPoolTimeoutDetails","client","input","config","code","waitedMs","acquireTimeoutMs","acquireTimeoutMillis","acquireConnectionTimeout","poolAcquireTimeoutMillis","poolReason","cause","Error","message","eventLoopDelay","phase","hostname","pid"],"mappings":"AAEO,MAAMA,oBAAoB;AAuDjC,MAAMC,QAAAA,GAAW,CAACC,KAAAA,GAChB,OAAOA,KAAAA,KAAU,YAAYC,MAAAA,CAAOC,QAAQ,CAACF,KAAAA,CAAAA,GAASA,KAAAA,GAAQG,SAAAA;AAEhE,MAAMC,WAAAA,GAAc,CAACC,IAAAA,GACnB,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