| 33 | |
| 34 | /** @internal */ |
| 35 | export const makeWith = <A, E, R>(options: { |
| 36 | readonly acquire: Effect<A, E, R> |
| 37 | readonly min: number |
| 38 | readonly max: number |
| 39 | readonly concurrency?: number | undefined |
| 40 | readonly targetUtilization?: number | undefined |
| 41 | readonly strategy: Strategy<A, E> |
| 42 | }): Effect<Pool<A, E>, never, Scope | R> => |
| 43 | core.uninterruptibleMask((restore) => |
| 44 | core.flatMap(core.context<R | Scope>(), (context) => { |
| 45 | const scope = Context.get(context, fiberRuntime.scopeTag) |
| 46 | const acquire = core.mapInputContext( |
| 47 | options.acquire, |
| 48 | (input) => Context.merge(context, input) |
| 49 | ) as Effect< |
| 50 | A, |
| 51 | E, |
| 52 | Scope |
| 53 | > |
| 54 | const pool = new PoolImpl<A, E>( |
| 55 | scope, |
| 56 | acquire, |
| 57 | options.concurrency ?? 1, |
| 58 | options.min, |
| 59 | options.max, |
| 60 | options.strategy, |
| 61 | Math.min(Math.max(options.targetUtilization ?? 1, 0.1), 1) |
| 62 | ) |
| 63 | const initialize = core.tap(fiberRuntime.forkDaemon(restore(pool.resize)), (fiber) => |
| 64 | scope.addFinalizer(() => core.interruptFiber(fiber))) |
| 65 | const runStrategy = core.tap(fiberRuntime.forkDaemon(restore(options.strategy.run(pool))), (fiber) => |
| 66 | scope.addFinalizer(() => |
| 67 | core.interruptFiber(fiber) |
| 68 | )) |
| 69 | return core.succeed(pool).pipe( |
| 70 | core.zipLeft(scope.addFinalizer(() => |
| 71 | pool.shutdown |
| 72 | )), |
| 73 | core.zipLeft(initialize), |
| 74 | core.zipLeft(runStrategy) |
| 75 | ) |
| 76 | }) |
| 77 | ) |
| 78 | |
| 79 | /** @internal */ |
| 80 | export const make = <A, E, R>(options: { |