{"version":3,"file":"event-loop.mjs","sources":["../../src/pool-diagnostics/event-loop.ts"],"sourcesContent":["import { monitorEventLoopDelay, type IntervalHistogram } from 'node:perf_hooks';\n\n/** Sampling interval of the event loop delay histogram. */\nexport const EVENT_LOOP_RESOLUTION_MS = 20;\n\n/** Every rotation drops the older half, so a report covers the last 30 to 60 s. */\nexport const EVENT_LOOP_ROTATION_MS = 30_000;\n\nexport interface EventLoopDelay {\n  /** Longest delay seen in the window, in ms, sampling interval removed. */\n  maxMs: number;\n  p99Ms: number;\n  /** Length of the window the values cover. */\n  windowMs: number;\n}\n\nexport interface EventLoopMonitor {\n  read(): EventLoopDelay | undefined;\n  stop(): void;\n}\n\nexport interface EventLoopMonitorOptions {\n  /** Injectable clock for tests. */\n  now?: () => number;\n  /** How often the older histogram is cleared and reused. */\n  rotateEveryMs?: number;\n}\n\n/** The histogram records the time between samples, so an idle loop reads one sampling interval. */\nexport const toDelayMs = (nanoseconds: number): number =>\n  Math.max(0, Math.round(nanoseconds / 1e6 - EVENT_LOOP_RESOLUTION_MS));\n\nconst startHistogram = (): IntervalHistogram => {\n  const histogram = monitorEventLoopDelay({ resolution: EVENT_LOOP_RESOLUTION_MS });\n  histogram.enable();\n  return histogram;\n};\n\n/**\n * A starved process fires timers late, so an acquire can time out even though the database\n * answered. Two histograms take turns so a report covers the last one to two rotation periods,\n * not everything since the process started. Neither the histograms nor the rotation timer keep\n * the process alive.\n */\nexport const createEventLoopMonitor = ({\n  now = () => Date.now(),\n  rotateEveryMs = EVENT_LOOP_ROTATION_MS,\n}: EventLoopMonitorOptions = {}): EventLoopMonitor => {\n  let older = startHistogram();\n  let newer = startHistogram();\n  let olderStartedAt = now();\n  let newerStartedAt = olderStartedAt;\n\n  const rotate = () => {\n    older.reset();\n    [older, newer] = [newer, older];\n    olderStartedAt = newerStartedAt;\n    newerStartedAt = now();\n  };\n\n  const timer = setInterval(rotate, rotateEveryMs);\n  timer.unref();\n\n  return {\n    read() {\n      if (older.count === 0) {\n        return undefined;\n      }\n\n      return {\n        maxMs: toDelayMs(older.max),\n        p99Ms: toDelayMs(older.percentile(99)),\n        windowMs: now() - olderStartedAt,\n      };\n    },\n\n    stop() {\n      clearInterval(timer);\n      older.disable();\n      newer.disable();\n    },\n  };\n};\n"],"names":["EVENT_LOOP_RESOLUTION_MS","EVENT_LOOP_ROTATION_MS","toDelayMs","nanoseconds","Math","max","round","startHistogram","histogram","monitorEventLoopDelay","resolution","enable","createEventLoopMonitor","now","Date","rotateEveryMs","older","newer","olderStartedAt","newerStartedAt","rotate","reset","timer","setInterval","unref","read","count","undefined","maxMs","p99Ms","percentile","windowMs","stop","clearInterval","disable"],"mappings":";;AAEA,4DACO,MAAMA,wBAAAA,GAA2B;AAExC,oFACO,MAAMC,sBAAAA,GAAyB;AAsBtC,oGACO,MAAMC,SAAAA,GAAY,CAACC,WAAAA,GACxBC,IAAAA,CAAKC,GAAG,CAAC,GAAGD,IAAAA,CAAKE,KAAK,CAACH,WAAAA,GAAc,MAAMH,wBAAAA,CAAAA;AAE7C,MAAMO,cAAAA,GAAiB,IAAA;AACrB,IAAA,MAAMC,YAAYC,qBAAAA,CAAsB;QAAEC,UAAAA,EAAYV;AAAyB,KAAA,CAAA;AAC/EQ,IAAAA,SAAAA,CAAUG,MAAM,EAAA;IAChB,OAAOH,SAAAA;AACT,CAAA;AAEA;;;;;AAKC,IACM,MAAMI,sBAAAA,GAAyB,CAAC,EACrCC,MAAM,IAAMC,IAAAA,CAAKD,GAAG,EAAE,EACtBE,aAAAA,GAAgBd,sBAAsB,EACd,GAAG,EAAE,GAAA;AAC7B,IAAA,IAAIe,KAAAA,GAAQT,cAAAA,EAAAA;AACZ,IAAA,IAAIU,KAAAA,GAAQV,cAAAA,EAAAA;AACZ,IAAA,IAAIW,cAAAA,GAAiBL,GAAAA,EAAAA;AACrB,IAAA,IAAIM,cAAAA,GAAiBD,cAAAA;AAErB,IAAA,MAAME,MAAAA,GAAS,IAAA;AACbJ,QAAAA,KAAAA,C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