| 319 | * |
| 320 | * @category constructors |
| 321 | * @since 4.0.0 |
| 322 | */ |
| 323 | export const makeWithStrategy = <A, E, R>(options: { |
| 324 | readonly acquire: Effect.Effect<A, E, R> |
| 325 | readonly min: number |
| 326 | readonly max: number |
| 327 | readonly concurrency?: number | undefined |
| 328 | readonly targetUtilization?: number | undefined |
| 329 | readonly strategy: Strategy<A, E> |
| 330 | }): Effect.Effect<Pool<A, E>, never, Scope.Scope | R> => |
| 331 | Effect.uninterruptibleMask(Effect.fnUntraced(function*(restore) { |
| 332 | const services = yield* Effect.context<R | Scope.Scope>() |
| 333 | const scope = Context.get(services, Scope.Scope) |
| 334 | const acquire = Effect.updateContext( |
| 335 | options.acquire, |
| 336 | (input) => Context.merge(services, input) |
| 337 | ) as Effect.Effect<A, E, Scope.Scope> |
| 338 | const concurrency = options.concurrency ?? 1 |
| 339 | |
| 340 | const config: Config<A, E> = { |
| 341 | acquire, |
| 342 | concurrency, |
| 343 | minSize: options.min, |
| 344 | maxSize: options.max, |
| 345 | strategy: options.strategy, |
| 346 | targetUtilization: Math.min(Math.max(options.targetUtilization ?? 1, 0.1), 1) |
| 347 | } |
| 348 | const state: State<A, E> = { |
| 349 | scope, |
| 350 | isShuttingDown: false, |
| 351 | semaphore: Semaphore.makeUnsafe(concurrency * options.max), |
| 352 | resizeSemaphore: Semaphore.makeUnsafe(1), |
| 353 | items: new Set(), |
| 354 | available: new Set(), |
| 355 | availableLatch: Latch.makeUnsafe(false), |
| 356 | invalidated: new Set(), |
| 357 | waiters: 0 |
| 358 | } |
| 359 | const self: Pool<A, E> = { |
| 360 | [TypeId]: TypeId, |
| 361 | config, |
| 362 | state, |
| 363 | pipe() { |
| 364 | return pipeArguments(this, arguments) |
| 365 | } |
| 366 | } |
| 367 | yield* Scope.addFinalizer(scope, shutdown(self)) |
| 368 | yield* Effect.tap( |
| 369 | Effect.forkDetach(restore(resize(self))), |
| 370 | (fiber) => Scope.addFinalizer(scope, Fiber.interrupt(fiber)) |
| 371 | ) |
| 372 | yield* Effect.tap( |
| 373 | Effect.forkDetach(restore(options.strategy.run(self))), |
| 374 | (fiber) => Scope.addFinalizer(scope, Fiber.interrupt(fiber)) |
| 375 | ) |
| 376 | return self |
| 377 | })) |
| 378 | |