(self: Pool<A, E>)
| 1115 | }) |
| 1116 | |
| 1117 | const allocate = <A, E>(self: Pool<A, E>): Effect.Effect<PoolItem<A, E>> => |
| 1118 | internal.uninterruptibleMask((restore) => |
| 1119 | core.withFiber((fiber) => { |
| 1120 | const impl = self as PoolImpl<A, E> |
| 1121 | const scope = internal.scopeMakeUnsafe() |
| 1122 | const previousContext = fiber.context |
| 1123 | fiber.setContext(Context.add(impl[AcquireContext], Scope.Scope, scope)) |
| 1124 | let acquiring = true |
| 1125 | impl[Acquiring]++ |
| 1126 | const use = Effect.flatMap(Effect.exit(impl[Acquire]), (exit): Effect.Effect<PoolItem<A, E>> => { |
| 1127 | // Publish completion and the item without yielding between them. |
| 1128 | acquiring = false |
| 1129 | impl[Acquiring]-- |
| 1130 | const item: PoolItemImpl<A, E> = { |
| 1131 | exit, |
| 1132 | finalizer: Effect.ignoreCause(Scope.close(scope, exit), { |
| 1133 | log: "Unhandled", |
| 1134 | message: "Unhandled error in pool finalizer" |
| 1135 | }), |
| 1136 | refCount: 0, |
| 1137 | idleSince: impl[PoolClock].currentTimeMillisUnsafe(), |
| 1138 | disableReclaim: false, |
| 1139 | isAvailable: false, |
| 1140 | availablePrevious: undefined, |
| 1141 | availableNext: undefined, |
| 1142 | release: undefined as any, |
| 1143 | [ReturnLease]: undefined as any |
| 1144 | } |
| 1145 | item[ReturnLease] = makeReturnLease(self, item) |
| 1146 | item.release = constant(internal.suspend(item[ReturnLease])) |
| 1147 | const onAcquire = Effect.suspend(() => |
| 1148 | // A borrower may have removed the item before the callback runs. |
| 1149 | self.state.items.has(item) ? self.config.strategy.onAcquire(item) : Effect.void |
| 1150 | ) |
| 1151 | if (exit._tag === "Success") { |
| 1152 | self.state.items.add(item) |
| 1153 | addAvailable(self, item) |
| 1154 | return self.config.strategy === strategyNoop ? Effect.succeed(item) : Effect.as(onAcquire, item) |
| 1155 | } |
| 1156 | impl[Failures]++ |
| 1157 | // Deliver a waiting borrower its own failure; otherwise retain the slot. |
| 1158 | const waiter = failureRecipient(self) |
| 1159 | if (waiter !== undefined) { |
| 1160 | waiter(exit) |
| 1161 | } else { |
| 1162 | failedAt.set(item, impl[Failures]) |
| 1163 | self.state.items.add(item) |
| 1164 | addAvailable(self, item) |
| 1165 | // Wake one sleeper to take or replace the failure. Wakes from a |
| 1166 | // healthy item beside it may not reach that sleeper. |
| 1167 | wakeWaiters(self, fiber, 1) |
| 1168 | } |
| 1169 | // Do not let cleanup delay a replacement acquisition. |
| 1170 | return Effect.as( |
| 1171 | Effect.forkIn(Effect.andThen(item.finalizer, onAcquire), self.state.scope, { startImmediately: true }), |
| 1172 | item |
| 1173 | ) |
| 1174 | }) |
no test coverage detected