(
row: ConnectionRow,
provider: CredentialProvider,
)
| 1618 | }); |
| 1619 | |
| 1620 | const refreshConnectionToken = ( |
| 1621 | row: ConnectionRow, |
| 1622 | provider: CredentialProvider, |
| 1623 | ): Effect.Effect<string | null, StorageFailure | CredentialResolutionError> => |
| 1624 | // Share a single refresh per connection so concurrent resolves of the same |
| 1625 | // connection all await one refresh-token grant (the AS rotates the refresh |
| 1626 | // token; parallel grants would race on a consumed token — v1's refresh |
| 1627 | // deferred-map). The gate is cleared once the refresh settles so a later |
| 1628 | // expiry can refresh again. |
| 1629 | Effect.gen(function* () { |
| 1630 | const key = connectionKey(row); |
| 1631 | const existing = refreshInFlight.get(key); |
| 1632 | if (existing) return yield* existing; |
| 1633 | // `Effect.cached` memoizes the grant onto a deferred: it runs once and |
| 1634 | // replays to every awaiter sharing this entry. |
| 1635 | const memoized = yield* Effect.cached(performTokenRefresh(row, provider)); |
| 1636 | const gated = memoized.pipe( |
| 1637 | Effect.ensuring(Effect.sync(() => refreshInFlight.delete(key))), |
| 1638 | ); |
| 1639 | // Re-check after building (a peer fiber may have registered first while |
| 1640 | // we built ours) so everyone converges on the same shared grant. |
| 1641 | const winner = refreshInFlight.get(key) ?? gated; |
| 1642 | if (winner === gated) refreshInFlight.set(key, gated); |
| 1643 | return yield* winner; |
| 1644 | }); |
| 1645 | |
| 1646 | // Resolve every named input of a connection (`variable → value`). A |
| 1647 | // single-secret connection yields `{ token: <value> }`; an apiKey method with |
no test coverage detected