fix(effect): stop a queued Pool resize from retrying after the last waiter fails (#11038)

Co-authored-by: Effect Bot <tech-ops@effectful.co>
Co-authored-by: Eugene <147496+eugene1g@users.noreply.github.com>
This commit is contained in:
Enes Kartal
2026-11-29 21:08:40 +00:00
committed by GitHub
co-authored by Effect Bot Eugene
parent 981d2b4293
commit 3727adf737
3 changed files with 47 additions and 16 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"effect": patch
---
`Pool` no longer starts another acquisition after a failed checkout when no borrower is still waiting.
+23 -16
View File
@@ -39,7 +39,7 @@ 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
/** Counts failed acquisitions. */
/** Counts failed acquisitions and resizes stopped after one. */
[Failures]: number
/** Whether a borrower forked a resize that has not read the target yet. */
[ResizePending]: boolean
@@ -657,7 +657,10 @@ const getSlowWith = <A, E, X, R>(
// borrower. A running one may not.
if (impl[ResizePending] || targetSize(self) <= activeSize(self)) return step
impl[ResizePending] = true
return internal.flatMap(Effect.forkIn(Effect.interruptible(resize(self)), state.scope), () => step)
return internal.flatMap(
Effect.forkIn(Effect.interruptible(resize(self, impl[Failures])), state.scope),
() => step
)
})
return loop
})
@@ -1059,16 +1062,23 @@ const invalidatePoolItem = <A, E>(self: Pool<A, E>, poolItem: PoolItem<A, E>): E
)
})
const resize = <A, E>(self: Pool<A, E>): Effect.Effect<void> =>
self.state.resizeSemaphore.withPermits(1)(resizeLoop(self))
// `failures` is the failure count when a borrower requested the resize or the
// previous pass ran. Startup and invalidation refills omit it.
const resize = <A, E>(self: Pool<A, E>, failures?: number): Effect.Effect<void> =>
self.state.resizeSemaphore.withPermits(1)(resizeLoop(self, failures))
const resizeLoop = <A, E>(self: Pool<A, E>): Effect.Effect<void> =>
const resizeLoop = <A, E>(self: Pool<A, E>, failures: number | undefined): Effect.Effect<void> =>
Effect.suspend(() => {
const impl = self as PoolImpl<A, E>
// The target read below counts every borrower so far.
;(self as PoolImpl<A, E>)[ResizePending] = false
impl[ResizePending] = false
const active = activeSize(self)
const target = targetSize(self)
if (active >= target) {
if (active >= target) return Effect.void
// After a failure, stop unless a borrower is asleep. The bump makes a
// borrower that relied on this resize, but has not slept yet, check again.
if (failures !== undefined && impl[Failures] !== failures && firstSleepingWaiter(self) === undefined) {
impl[Failures]++
return Effect.void
}
const toAcquire = target - active
@@ -1096,15 +1106,12 @@ const resizeLoop = <A, E>(self: Pool<A, E>): Effect.Effect<void> =>
return internal.void
})
)
// After a failure, keep acquiring only while a borrower is asleep to take
// the next result. Each failure is delivered to one of them, so this ends.
const next = (failed: boolean) => failed && failureRecipient(self) === undefined ? Effect.void : resizeLoop(self)
return toAcquire === 1
? Effect.flatMap(acquireOne, (item) => next(item.exit._tag === "Failure"))
: Effect.flatMap(
Effect.replicateEffect(acquireOne, toAcquire, { concurrency: toAcquire }),
(items) => next(items.some((_) => _.exit._tag === "Failure"))
)
return Effect.andThen(
toAcquire === 1
? acquireOne
: Effect.replicateEffect(acquireOne, toAcquire, { concurrency: toAcquire, discard: true }),
resizeLoop(self, impl[Failures])
)
})
const allocate = <A, E>(self: Pool<A, E>): Effect.Effect<PoolItem<A, E>> =>
+19
View File
@@ -341,6 +341,25 @@ describe("Pool", () => {
yield* Effect.timeout(Fiber.joinAll(fibers), Duration.seconds(1))
}))
it.effect("stops acquiring once the last waiting borrower has its failure", () =>
Effect.gen(function*() {
let attempts = 0
const pool = yield* Pool.make({
acquire: Effect.suspend(() =>
Effect.delay(++attempts === 1 ? Effect.fail("boom") : Effect.succeed(attempts), "20 millis")
),
size: 1
})
const failed = yield* Effect.forkChild(Effect.flip(Effect.scoped(Pool.get(pool))))
yield* TestClock.adjust("20 millis")
strictEqual(yield* Fiber.join(failed), "boom")
yield* TestClock.adjust("1 second")
strictEqual(attempts, 1)
const next = yield* Effect.forkChild(Effect.scoped(Pool.get(pool)))
yield* TestClock.adjust("20 millis")
strictEqual(yield* Fiber.join(next), 2)
}))
it.effect("compositional retry", () =>
Effect.gen(function*() {
const cond = (i: number) => (i <= 10 ? Effect.fail(i) : Effect.succeed(i))