(self: Pool<A, E>, failures: number | undefined)
| 1068 | self.state.resizeSemaphore.withPermits(1)(resizeLoop(self, failures)) |
| 1069 | |
| 1070 | const 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 | |
| 1117 | const allocate = <A, E>(self: Pool<A, E>): Effect.Effect<PoolItem<A, E>> => |
| 1118 | internal.uninterruptibleMask((restore) => |
no test coverage detected