MCPcopy Create free account
hub / github.com/Effect-TS/effect / resizeLoop

Function resizeLoop

packages/effect/src/Pool.ts:1070–1115  ·  view source on GitHub ↗
(self: Pool<A, E>, failures: number | undefined)

Source from the content-addressed store, hash-verified

1068 self.state.resizeSemaphore.withPermits(1)(resizeLoop(self, failures))
1069
1070const resizeLoop = <A, E>(self: Pool<A, E>, failures: number | undefined): Effect.Effect<void> =>
1071 Effect.suspend(() => {
1072 const impl = self as PoolImpl<A, E>
1073 // The target read below counts every borrower so far.
1074 impl[ResizePending] = false
1075 const active = activeSize(self)
1076 const target = targetSize(self)
1077 if (active >= target) return Effect.void
1078 // After a failure, stop unless a borrower is asleep. The bump makes a
1079 // borrower that relied on this resize, but has not slept yet, check again.
1080 if (failures !== undefined && impl[Failures] !== failures && firstSleepingWaiter(self) === undefined) {
1081 impl[Failures]++
1082 return Effect.void
1083 }
1084 const toAcquire = target - active
1085 // Wake waiters as each item arrives. Waiting for the whole batch would
1086 // leave acquired items idle behind a slow acquisition, with every borrower
1087 // queued behind the waiters.
1088 const acquireOne = Effect.tap(
1089 self.config.strategy === strategyNoop
1090 ? allocate(self)
1091 : Effect.flatMap(
1092 self.config.strategy.reclaim(self),
1093 (item) =>
1094 item
1095 ? Effect.sync(() => {
1096 // A reclaimed item rejoins circulation like a released one.
1097 if (self.state.items.has(item) && !self.state.invalidated.has(item)) addAvailableFront(self, item)
1098 return item
1099 })
1100 : allocate(self)
1101 ),
1102 (item) =>
1103 core.withFiber((fiber) => {
1104 // A new item can only serve as many waiters as it has slots.
1105 if (item.exit._tag === "Success") wakeWaiters(self, fiber, self.config.concurrency)
1106 return internal.void
1107 })
1108 )
1109 return Effect.andThen(
1110 toAcquire === 1
1111 ? acquireOne
1112 : Effect.replicateEffect(acquireOne, toAcquire, { concurrency: toAcquire, discard: true }),
1113 resizeLoop(self, impl[Failures])
1114 )
1115 })
1116
1117const allocate = <A, E>(self: Pool<A, E>): Effect.Effect<PoolItem<A, E>> =>
1118 internal.uninterruptibleMask((restore) =>

Callers 1

resizeFunction · 0.85

Calls 9

activeSizeFunction · 0.85
targetSizeFunction · 0.85
firstSleepingWaiterFunction · 0.85
addAvailableFrontFunction · 0.85
wakeWaitersFunction · 0.85
reclaimMethod · 0.80
allocateFunction · 0.70
hasMethod · 0.65
syncMethod · 0.45

Tested by

no test coverage detected