| 766 | |
| 767 | const cancelEffects = new WeakMap<Pg.PoolClient, Effect.Effect<void> | undefined>() |
| 768 | const makeCancel = (pool: Pg.Pool, client: Pg.PoolClient) => { |
| 769 | if (cancelEffects.has(client)) { |
| 770 | return cancelEffects.get(client)! |
| 771 | } |
| 772 | const processId = (client as any).processID |
| 773 | const eff = processId !== undefined |
| 774 | // query cancelation is best-effort, so we don't fail if it doesn't work |
| 775 | ? Effect.callback<void>((resume) => { |
| 776 | if (pool.ending) return resume(Effect.void) |
| 777 | pool.query(`SELECT pg_cancel_backend(${processId})`, () => { |
| 778 | resume(Effect.void) |
| 779 | }) |
| 780 | }).pipe( |
| 781 | Effect.interruptible, |
| 782 | Effect.timeoutOption(5000) |
| 783 | ) |
| 784 | : undefined |
| 785 | cancelEffects.set(client, eff) |
| 786 | return eff |
| 787 | } |
| 788 | |
| 789 | /** |
| 790 | * Creates a layer from an effect that acquires a `PgClient`, providing both `PgClient` and `SqlClient`. |