mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-06 03:59:09 +08:00
Expose in-flight acquisitions in pool statistics (#10660)
This commit is contained in:
@@ -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).
|
||||
@@ -30,11 +30,13 @@ const TypeId = "~effect/Pool"
|
||||
const Acquire = Symbol()
|
||||
const AcquireContext = Symbol()
|
||||
const PoolClock = Symbol()
|
||||
const Acquiring = Symbol()
|
||||
|
||||
interface PoolImpl<A, E> extends Pool<A, E> {
|
||||
readonly [Acquire]: Effect.Effect<A, E, Scope.Scope>
|
||||
readonly [AcquireContext]: Context.Context<Scope.Scope>
|
||||
readonly [PoolClock]: Clock
|
||||
[Acquiring]: number
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,8 +102,10 @@ export interface Config<A, E> {
|
||||
* **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<A, E> {
|
||||
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 = <A, E, R>(options: {
|
||||
[Acquire]: options.acquire as Effect.Effect<A, E, Scope.Scope>,
|
||||
[AcquireContext]: services as Context.Context<Scope.Scope>,
|
||||
[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 = <A, E>(self: Pool<A, E>): Effect.Effect<Stats> =>
|
||||
return {
|
||||
idle,
|
||||
used,
|
||||
acquiring: (self as PoolImpl<A, E>)[Acquiring],
|
||||
pending: self.state.waiters.size,
|
||||
min: self.config.minSize,
|
||||
max: self.config.maxSize
|
||||
@@ -1060,7 +1074,12 @@ const allocate = <A, E>(self: Pool<A, E>): Effect.Effect<PoolItem<A, E>> =>
|
||||
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<A, E> = {
|
||||
exit,
|
||||
finalizer: Effect.ignoreCause(Scope.close(scope, exit), {
|
||||
@@ -1103,6 +1122,7 @@ const allocate = <A, E>(self: Pool<A, E>): Effect.Effect<PoolItem<A, E>> =>
|
||||
restore(use) as Effect.Effect<PoolItem<A, E>>,
|
||||
(exit) => {
|
||||
fiber.setContext(previousContext)
|
||||
if (acquiring) impl[Acquiring]--
|
||||
return exit._tag === "Failure" ? internal.scopeCloseUnsafe(scope, exit) : undefined
|
||||
},
|
||||
true
|
||||
|
||||
@@ -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<void>()
|
||||
const gate = yield* Deferred.make<void>()
|
||||
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<void>()
|
||||
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 })
|
||||
}))
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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<Pool.Stats>
|
||||
}
|
||||
@@ -263,7 +268,7 @@ export const makeClient = <R = never>(
|
||||
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 })
|
||||
}))
|
||||
|
||||
/**
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user