From 34a4740c19003e95abd509ba4588673bca068247 Mon Sep 17 00:00:00 2001 From: effect-bot <129879685+effect-bot@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:07:51 -0500 Subject: [PATCH] Expose in-flight acquisitions in pool statistics (#10660) --- .changeset/pool-acquiring-stats.md | 8 +++ packages/effect/src/Pool.ts | 36 ++++++++--- packages/effect/test/Pool.test.ts | 63 ++++++++++++++++--- packages/sql/pg/src/PgClient.ts | 11 +++- .../sql/pg/test/PgPool.in-process.test.ts | 10 +-- 5 files changed, 102 insertions(+), 26 deletions(-) create mode 100644 .changeset/pool-acquiring-stats.md diff --git a/.changeset/pool-acquiring-stats.md b/.changeset/pool-acquiring-stats.md new file mode 100644 index 0000000000..e81d0c543f --- /dev/null +++ b/.changeset/pool-acquiring-stats.md @@ -0,0 +1,8 @@ +--- +"effect": minor +"@effect/sql-pg": minor +--- + +Add `acquiring` to `Pool.stats` and `PgClient.poolStats` to count in-flight acquisitions independently of waiting borrowers. + +Manually constructed `Pool.Stats` snapshots must include `acquiring` (use `0` when no acquisition is running). diff --git a/packages/effect/src/Pool.ts b/packages/effect/src/Pool.ts index 484d2fc8a3..e50c7b3893 100644 --- a/packages/effect/src/Pool.ts +++ b/packages/effect/src/Pool.ts @@ -30,11 +30,13 @@ const TypeId = "~effect/Pool" const Acquire = Symbol() const AcquireContext = Symbol() const PoolClock = Symbol() +const Acquiring = Symbol() interface PoolImpl extends Pool { readonly [Acquire]: Effect.Effect readonly [AcquireContext]: Context.Context readonly [PoolClock]: Clock + [Acquiring]: number } /** @@ -100,8 +102,10 @@ export interface Config { * **Details** * * `idle` and `used` count acquired items, not borrowers, so an item shared by - * several borrowers counts once. `pending` counts borrowers waiting for an - * item. `min` and `max` are the configured size bounds. + * several borrowers counts once. `acquiring` counts acquisition effects that + * have started but not yet exited, including pre-warming and replacements, + * independently of whether any borrower is waiting. `pending` counts borrowers + * waiting for an item. `min` and `max` are the configured size bounds. * * @see {@link stats} for reading a snapshot * @@ -112,6 +116,7 @@ export interface Config { export interface Stats { readonly idle: number readonly used: number + readonly acquiring: number readonly pending: number readonly min: number readonly max: number @@ -412,6 +417,7 @@ export const makeWithStrategy = (options: { [Acquire]: options.acquire as Effect.Effect, [AcquireContext]: services as Context.Context, [PoolClock]: yield* Clock, + [Acquiring]: 0, config, state, pipe() { @@ -933,8 +939,8 @@ export const reserve: { ) /** - * Returns a snapshot of how many items are idle or in use and how many - * borrowers are waiting. + * Returns a snapshot of how many items are idle, in use, or being acquired + * and how many borrowers are waiting. * * **When to use** * @@ -943,16 +949,23 @@ export const reserve: { * * **Details** * - * - Only successfully acquired items are counted. Items still being acquired - * and failed acquisitions are not. + * - `idle` and `used` count only successfully acquired items. + * - `acquiring` counts acquisition effects from start until success, failure, + * or interruption. Queued acquisitions that have not started and reclaimed + * items do not count. * - An item is used while it has a borrower or a reservation, otherwise idle. * - An invalidated item counts as used until its last borrower returns it. * * **Gotchas** * * The replacement for an invalidated item is acquired while the old one is - * still borrowed, so `idle + used` can briefly exceed `max`. A borrower that - * has been woken but has not yet taken its item is not counted as pending. + * still borrowed, so `idle + used + acquiring` can briefly exceed `max`. A + * borrower that has been woken but has not yet taken its item is not counted + * as pending. `pending` and `acquiring` are independent: a borrower waiting + * on an acquisition contributes to both. + * + * `idle + used + acquiring === 0` means the pool holds and is opening no items, + * not that every release finalizer has finished. * * @see {@link Stats} for the snapshot fields * @@ -972,6 +985,7 @@ export const stats = (self: Pool): Effect.Effect => return { idle, used, + acquiring: (self as PoolImpl)[Acquiring], pending: self.state.waiters.size, min: self.config.minSize, max: self.config.maxSize @@ -1060,7 +1074,12 @@ const allocate = (self: Pool): Effect.Effect> => const scope = internal.scopeMakeUnsafe() const previousContext = fiber.context fiber.setContext(Context.add(impl[AcquireContext], Scope.Scope, scope)) + let acquiring = true + impl[Acquiring]++ const use = Effect.flatMap(Effect.exit(impl[Acquire]), (exit) => { + // Publish completion and the item without yielding between them. + acquiring = false + impl[Acquiring]-- const item: PoolItem = { exit, finalizer: Effect.ignoreCause(Scope.close(scope, exit), { @@ -1103,6 +1122,7 @@ const allocate = (self: Pool): Effect.Effect> => restore(use) as Effect.Effect>, (exit) => { fiber.setContext(previousContext) + if (acquiring) impl[Acquiring]-- return exit._tag === "Failure" ? internal.scopeCloseUnsafe(scope, exit) : undefined }, true diff --git a/packages/effect/test/Pool.test.ts b/packages/effect/test/Pool.test.ts index 8cb49ba80f..1c4dd99e8e 100644 --- a/packages/effect/test/Pool.test.ts +++ b/packages/effect/test/Pool.test.ts @@ -1400,28 +1400,71 @@ describe("Pool", () => { } describe("stats", () => { + it.effect("counts an acquisition that outlives its interrupted borrower", () => + Effect.gen(function*() { + const started = yield* Deferred.make() + const gate = yield* Deferred.make() + const pool = yield* Pool.makeWithTTL({ + acquire: Effect.andThen(Deferred.succeed(started, undefined), Deferred.await(gate)), + min: 0, + max: 2, + timeToLive: "1 minute" + }) + const fiber = yield* Effect.scoped(Pool.get(pool)).pipe(Effect.forkChild({ startImmediately: true })) + yield* Deferred.await(started) + const waiting = yield* Pool.stats(pool) + yield* Fiber.interrupt(fiber) + const interrupted = yield* Pool.stats(pool) + yield* Deferred.succeed(gate, undefined) + const completed = yield* Effect.andThen(Effect.yieldNow, Pool.stats(pool)).pipe( + Effect.repeat({ until: (stats) => stats.idle === 1 }) + ) + deepStrictEqual([waiting, interrupted, completed], [ + { idle: 0, used: 0, acquiring: 1, pending: 1, min: 0, max: 2 }, + { idle: 0, used: 0, acquiring: 1, pending: 0, min: 0, max: 2 }, + { idle: 1, used: 0, acquiring: 0, pending: 0, min: 0, max: 2 } + ]) + })) + + it.effect("stops counting a pre-warming acquisition when the pool scope closes", () => + Effect.gen(function*() { + const started = yield* Deferred.make() + const scope = yield* Scope.fork(yield* Effect.scope) + const pool = yield* Pool.make({ + acquire: Effect.andThen(Deferred.succeed(started, undefined), Effect.never), + size: 1 + }).pipe(Scope.provide(scope)) + yield* Deferred.await(started) + const acquiring = yield* Pool.stats(pool) + yield* Scope.close(scope, Exit.void) + deepStrictEqual([acquiring, yield* Pool.stats(pool)], [ + { idle: 0, used: 0, acquiring: 1, pending: 0, min: 1, max: 1 }, + { idle: 0, used: 0, acquiring: 0, pending: 0, min: 1, max: 1 } + ]) + })) + it.effect("counts leased items rather than borrowers, and parked waiters as pending", () => Effect.gen(function*() { let acquired = 0 const pool = yield* Pool.make({ acquire: Effect.sync(() => ++acquired), size: 2, concurrency: 2 }) yield* waitUntil(() => acquired === 2) - deepStrictEqual(yield* Pool.stats(pool), { idle: 2, used: 0, pending: 0, min: 2, max: 2 }) + deepStrictEqual(yield* Pool.stats(pool), { idle: 2, used: 0, acquiring: 0, pending: 0, min: 2, max: 2 }) const leases = yield* Scope.fork(yield* Effect.scope) yield* Scope.provide(Pool.get(pool), leases) yield* Scope.provide(Pool.get(pool), leases) - deepStrictEqual(yield* Pool.stats(pool), { idle: 1, used: 1, pending: 0, min: 2, max: 2 }) + deepStrictEqual(yield* Pool.stats(pool), { idle: 1, used: 1, acquiring: 0, pending: 0, min: 2, max: 2 }) yield* Scope.provide(Pool.get(pool), leases) yield* Scope.provide(Pool.get(pool), leases) - deepStrictEqual(yield* Pool.stats(pool), { idle: 0, used: 2, pending: 0, min: 2, max: 2 }) + deepStrictEqual(yield* Pool.stats(pool), { idle: 0, used: 2, acquiring: 0, pending: 0, min: 2, max: 2 }) const waiter = yield* Scope.fork(yield* Effect.scope) const fiber = yield* Scope.provide(Pool.get(pool), waiter).pipe(Effect.forkChild({ startImmediately: true })) - deepStrictEqual(yield* Pool.stats(pool), { idle: 0, used: 2, pending: 1, min: 2, max: 2 }) + deepStrictEqual(yield* Pool.stats(pool), { idle: 0, used: 2, acquiring: 0, pending: 1, min: 2, max: 2 }) yield* Scope.close(leases, Exit.void) yield* Fiber.join(fiber) - deepStrictEqual(yield* Pool.stats(pool), { idle: 1, used: 1, pending: 0, min: 2, max: 2 }) + deepStrictEqual(yield* Pool.stats(pool), { idle: 1, used: 1, acquiring: 0, pending: 0, min: 2, max: 2 }) yield* Scope.close(waiter, Exit.void) })) @@ -1433,9 +1476,9 @@ describe("Pool", () => { yield* Scope.provide(Pool.get(pool), lease) yield* Scope.provide(Pool.reserve(pool, "resource"), reservation) yield* Scope.close(lease, Exit.void) - deepStrictEqual(yield* Pool.stats(pool), { idle: 0, used: 1, pending: 0, min: 1, max: 1 }) + deepStrictEqual(yield* Pool.stats(pool), { idle: 0, used: 1, acquiring: 0, pending: 0, min: 1, max: 1 }) yield* Scope.close(reservation, Exit.void) - deepStrictEqual(yield* Pool.stats(pool), { idle: 1, used: 0, pending: 0, min: 1, max: 1 }) + deepStrictEqual(yield* Pool.stats(pool), { idle: 1, used: 0, acquiring: 0, pending: 0, min: 1, max: 1 }) })) it.effect("counts an invalidated item as used until its last borrower returns", () => @@ -1447,9 +1490,9 @@ describe("Pool", () => { yield* Pool.invalidate(pool, item) yield* waitUntil(() => acquired === 2) // The invalidated session is still open, so both count against `max`. - deepStrictEqual(yield* Pool.stats(pool), { idle: 1, used: 1, pending: 0, min: 1, max: 1 }) + deepStrictEqual(yield* Pool.stats(pool), { idle: 1, used: 1, acquiring: 0, pending: 0, min: 1, max: 1 }) yield* Scope.close(lease, Exit.void) - deepStrictEqual(yield* Pool.stats(pool), { idle: 1, used: 0, pending: 0, min: 1, max: 1 }) + deepStrictEqual(yield* Pool.stats(pool), { idle: 1, used: 0, acquiring: 0, pending: 0, min: 1, max: 1 }) })) it.effect("does not count failed acquisitions", () => @@ -1460,7 +1503,7 @@ describe("Pool", () => { size: 2 }) yield* waitUntil(() => pool.state.items.size === 2) - deepStrictEqual(yield* Pool.stats(pool), { idle: 1, used: 0, pending: 0, min: 2, max: 2 }) + deepStrictEqual(yield* Pool.stats(pool), { idle: 1, used: 0, acquiring: 0, pending: 0, min: 2, max: 2 }) })) }) }) diff --git a/packages/sql/pg/src/PgClient.ts b/packages/sql/pg/src/PgClient.ts index 72864435bd..be1ca9b2a6 100644 --- a/packages/sql/pg/src/PgClient.ts +++ b/packages/sql/pg/src/PgClient.ts @@ -73,8 +73,13 @@ export interface PgClient extends Client.Base { /** * Returns a snapshot of connection pool occupancy. A client from `makeClient` * has one session that stays leased for its lifetime, so it always reports - * `{ idle: 0, used: 1, pending: 0, min: 1, max: 1 }` whatever statements run - * on it. + * `{ idle: 0, used: 1, acquiring: 0, pending: 0, min: 1, max: 1 }` regardless + * of statement activity. + * + * **Gotchas** + * + * Zero occupancy does not guarantee that sockets have closed: connection + * finalizers initiate socket shutdown without awaiting its completion. */ readonly poolStats: Effect.Effect } @@ -263,7 +268,7 @@ export const makeClient = ( listenAcquirer: options.acquireForStream ? PgConnection.make(options) : connection.pin, config: options, // The one session is leased for the client's lifetime. - poolStats: Effect.succeed({ idle: 0, used: 1, pending: 0, min: 1, max: 1 }) + poolStats: Effect.succeed({ idle: 0, used: 1, acquiring: 0, pending: 0, min: 1, max: 1 }) })) /** diff --git a/packages/sql/pg/test/PgPool.in-process.test.ts b/packages/sql/pg/test/PgPool.in-process.test.ts index 70e9dac7ff..507b3abcf7 100644 --- a/packages/sql/pg/test/PgPool.in-process.test.ts +++ b/packages/sql/pg/test/PgPool.in-process.test.ts @@ -510,18 +510,18 @@ describe("pool stats", () => { yield* pool.get yield* pool.get yield* pool.get - assert.deepStrictEqual(yield* pool.stats, { idle: 0, used: 1, pending: 0, min: 0, max: 2 }) + assert.deepStrictEqual(yield* pool.stats, { idle: 0, used: 1, acquiring: 0, pending: 0, min: 0, max: 2 }) }))) it.effect("PgClient.make reports the occupancy of its pool", () => Effect.scoped(Effect.gen(function*() { const sql = yield* PgClient.make({ username: "test", maxConnections: 3, stream: readyStream }) - assert.deepStrictEqual(yield* sql.poolStats, { idle: 0, used: 0, pending: 0, min: 0, max: 3 }) + assert.deepStrictEqual(yield* sql.poolStats, { idle: 0, used: 0, acquiring: 0, pending: 0, min: 0, max: 3 }) yield* Effect.scoped(Effect.gen(function*() { yield* sql.reserve - assert.deepStrictEqual(yield* sql.poolStats, { idle: 0, used: 1, pending: 0, min: 0, max: 3 }) + assert.deepStrictEqual(yield* sql.poolStats, { idle: 0, used: 1, acquiring: 0, pending: 0, min: 0, max: 3 }) })) - assert.deepStrictEqual(yield* sql.poolStats, { idle: 1, used: 0, pending: 0, min: 0, max: 3 }) + assert.deepStrictEqual(yield* sql.poolStats, { idle: 1, used: 0, acquiring: 0, pending: 0, min: 0, max: 3 }) })).pipe(Effect.provide(Reactivity.layer))) it.effect("PgClient.makeClient reports its one session as permanently leased", () => @@ -529,7 +529,7 @@ describe("pool stats", () => { const sql = yield* PgClient.makeClient({ username: "test", stream: readyStream }) // The snapshot describes the session, not statement activity, so it does // not change while work runs on it. - const leased = { idle: 0, used: 1, pending: 0, min: 1, max: 1 } + const leased = { idle: 0, used: 1, acquiring: 0, pending: 0, min: 1, max: 1 } assert.deepStrictEqual(yield* sql.poolStats, leased) yield* Effect.scoped(Effect.gen(function*() { yield* sql.reserve