* Time the body of an `add()` call. Subclasses wrap their work with this * so all add latency is reported uniformly via the `addObserver` hook. * Subclasses may call `reportAddSkipped(reason)` from inside the fn to * mark the call as a no-op (e.g. cached-and-skipped).
(fn: () => Promise<T>)
| 283 | * mark the call as a no-op (e.g. cached-and-skipped). |
| 284 | */ |
| 285 | protected async timeAdd<T>(fn: () => Promise<T>): Promise<T> { |
| 286 | const start = performance.now(); |
| 287 | this.inflightAddStats = {}; |
| 288 | try { |
| 289 | return await fn(); |
| 290 | } finally { |
| 291 | try { |
| 292 | this.addObserver?.({ |
| 293 | buffer: this.name, |
| 294 | durationMs: performance.now() - start, |
| 295 | skipped: this.inflightAddStats.skipped, |
| 296 | skipReason: this.inflightAddStats.skipReason, |
| 297 | }); |
| 298 | } catch { |
| 299 | // observer errors must not break add |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /** Mark the in-flight add() as a no-op for observability. */ |
| 305 | protected reportAddSkipped(reason: string) { |