| 125 | } |
| 126 | |
| 127 | class PoolImpl<A, E> extends Effectable.Class<A, E, Scope> implements Pool<A, E> { |
| 128 | readonly [PoolTypeId]: Pool.Variance<A, E>[PoolTypeId_] |
| 129 | |
| 130 | isShuttingDown = false |
| 131 | readonly semaphore: Semaphore |
| 132 | readonly items = new Set<PoolItem<A, E>>() |
| 133 | readonly available = new Set<PoolItem<A, E>>() |
| 134 | readonly availableLatch = circular.unsafeMakeLatch(false) |
| 135 | readonly invalidated = new Set<PoolItem<A, E>>() |
| 136 | waiters = 0 |
| 137 | |
| 138 | constructor( |
| 139 | readonly scope: Scope, |
| 140 | readonly acquire: Effect<A, E, Scope>, |
| 141 | readonly concurrency: number, |
| 142 | readonly minSize: number, |
| 143 | readonly maxSize: number, |
| 144 | readonly strategy: Strategy<A, E>, |
| 145 | readonly targetUtilization: number |
| 146 | ) { |
| 147 | super() |
| 148 | this[PoolTypeId] = poolVariance |
| 149 | this.semaphore = circular.unsafeMakeSemaphore(concurrency * maxSize) |
| 150 | } |
| 151 | |
| 152 | readonly allocate: Effect<PoolItem<A, E>> = core.acquireUseRelease( |
| 153 | fiberRuntime.scopeMake(), |
| 154 | (scope) => |
| 155 | this.acquire.pipe( |
| 156 | fiberRuntime.scopeExtend(scope), |
| 157 | core.exit, |
| 158 | core.flatMap((exit) => { |
| 159 | const item: PoolItem<A, E> = { |
| 160 | exit, |
| 161 | finalizer: core.catchAllCause(scope.close(exit), reportUnhandledError), |
| 162 | refCount: 0, |
| 163 | disableReclaim: false |
| 164 | } |
| 165 | this.items.add(item) |
| 166 | this.available.add(item) |
| 167 | return core.as( |
| 168 | exit._tag === "Success" |
| 169 | ? this.strategy.onAcquire(item) |
| 170 | : core.zipRight(item.finalizer, this.strategy.onAcquire(item)), |
| 171 | item |
| 172 | ) |
| 173 | }) |
| 174 | ), |
| 175 | (scope, exit) => exit._tag === "Failure" ? scope.close(exit) : core.void |
| 176 | ) |
| 177 | |
| 178 | get currentUsage() { |
| 179 | let count = this.waiters |
| 180 | for (const item of this.items) { |
| 181 | count += item.refCount |
| 182 | } |
| 183 | return count |
| 184 | } |
nothing calls this directly
no test coverage detected