| 408 | // `Pool.use` cannot retry checkout without wrapping `f` or bound its wait, |
| 409 | // so it is used only when neither applies and no connection awaits retirement. |
| 410 | const use = <A, E, R>( |
| 411 | f: (connection: PgConnection.PgConnection) => Effect.Effect<A, E, R> |
| 412 | ): Effect.Effect<A, E | SqlError, R> => |
| 413 | Effect.suspend(() => { |
| 414 | if (connectRetry !== undefined || acquireTimeout !== undefined || deadConnections.size > 0) { |
| 415 | return Effect.scoped(Effect.flatMap(get, f)) |
| 416 | } |
| 417 | const run = (connection: PgConnection.PgConnection) => |
| 418 | connects.has(connection) ? Effect.andThen(reportConnect(connection), f(connection)) : f(connection) |
| 419 | if (connectionTTL === undefined) { |
| 420 | return borrow("any", (leave) => |
| 421 | Pool.use(pool, (connection) => { |
| 422 | leave() |
| 423 | return run(connection) |
| 424 | })) |
| 425 | } |
| 426 | // An expired session is rejected inside the lease and replaced after it: |
| 427 | // requesting the replacement while holding the lease can deadlock a |
| 428 | // size-one pool. The callback keeps its own scope, as on the scoped path. |
| 429 | return borrow("any", (leave) => |
| 430 | Effect.flatMap( |
| 431 | Pool.use(pool, (connection): Effect.Effect<A | typeof rejected, E, R> => { |
| 432 | if (expired(connection)) return Effect.as(Pool.invalidate(pool, connection), rejected) |
| 433 | leave() |
| 434 | return Effect.scoped(run(connection)) |
| 435 | }), |
| 436 | (a): Effect.Effect<A, E | SqlError, R> => |
| 437 | a === rejected |
| 438 | ? Effect.scoped(Effect.flatMap(get, (connection) => { |
| 439 | leave() |
| 440 | return f(connection) |
| 441 | })) |
| 442 | : Effect.succeed(a) |
| 443 | )) |
| 444 | }) |
| 445 | |
| 446 | const pgPool: PgPool = { |
| 447 | [TypeId]: TypeId, |