| 390 | * @since 4.0.0 |
| 391 | */ |
| 392 | export const makeWithStrategy = <A, E, R>(options: { |
| 393 | readonly acquire: Effect.Effect<A, E, R> |
| 394 | readonly min: number |
| 395 | readonly max: number |
| 396 | readonly concurrency?: number | undefined |
| 397 | readonly targetUtilization?: number | undefined |
| 398 | readonly strategy: Strategy<A, E> |
| 399 | }): Effect.Effect<Pool<A, E>, never, Scope.Scope | R> => |
| 400 | Effect.uninterruptibleMask(Effect.fnUntraced(function*(restore) { |
| 401 | const services = yield* Effect.context<R | Scope.Scope>() |
| 402 | // A child scope keeps the pool usable in later finalizers of the outer scope |
| 403 | const scope = Scope.forkUnsafe(Context.get(services, Scope.Scope)) |
| 404 | const acquire = Effect.updateContext( |
| 405 | options.acquire, |
| 406 | (input) => Context.merge(services, input) |
| 407 | ) as Effect.Effect<A, E, Scope.Scope> |
| 408 | const concurrency = options.concurrency ?? 1 |
| 409 | |
| 410 | const config: Config<A, E> = { |
| 411 | acquire, |
| 412 | concurrency, |
| 413 | isFixed: options.min === options.max, |
| 414 | minSize: options.min, |
| 415 | maxSize: options.max, |
| 416 | strategy: options.strategy, |
| 417 | targetUtilization: Math.min(Math.max(options.targetUtilization ?? 1, 0.1), 1) |
| 418 | } |
| 419 | const state: State<A, E> = { |
| 420 | scope, |
| 421 | isShuttingDown: false, |
| 422 | usage: 0, |
| 423 | resizeSemaphore: Semaphore.makeUnsafe(1), |
| 424 | items: new Set(), |
| 425 | availableHead: undefined, |
| 426 | availableTail: undefined, |
| 427 | invalidated: new Set(), |
| 428 | waiters: new Set() |
| 429 | } |
| 430 | const self: PoolImpl<A, E> = { |
| 431 | [TypeId]: TypeId, |
| 432 | [Acquire]: options.acquire as Effect.Effect<A, E, Scope.Scope>, |
| 433 | [AcquireContext]: services as Context.Context<Scope.Scope>, |
| 434 | [PoolClock]: yield* Clock, |
| 435 | [Failures]: 0, |
| 436 | [ResizePending]: false, |
| 437 | [Acquiring]: 0, |
| 438 | config, |
| 439 | state, |
| 440 | pipe() { |
| 441 | return pipeArguments(this, arguments) |
| 442 | } |
| 443 | } |
| 444 | yield* Scope.addFinalizer(scope, shutdown(self)) |
| 445 | if (config.minSize > 0) { |
| 446 | yield* Effect.tap( |
| 447 | Effect.forkDetach(restore(resize(self)), { startImmediately: true }), |
| 448 | (fiber) => Scope.addFinalizer(scope, Fiber.interrupt(fiber)) |
| 449 | ) |