* Open the migration session before taking the advisory lock, retrying * transient connection failures with bounded backoff. The deploy database can * briefly exhaust every non-superuser connection slot at peak (`53300`); the * migration is a single short-lived session, so waiting out a spike tha
()
| 132 | * errors (auth, unknown host config, etc.) still fail fast. |
| 133 | */ |
| 134 | async function connectWithRetry(): Promise<void> { |
| 135 | for (let attempt = 1; ; attempt++) { |
| 136 | try { |
| 137 | await client`SELECT 1` |
| 138 | return |
| 139 | } catch (error) { |
| 140 | if (!isTransientConnectError(error) || attempt >= CONNECT_MAX_ATTEMPTS) throw error |
| 141 | const delayMs = backoffWithJitter(attempt, null, CONNECT_RETRY_BACKOFF) |
| 142 | console.warn( |
| 143 | `WARN: database unavailable (${getPostgresErrorCode(error)}); ` + |
| 144 | `attempt ${attempt}/${CONNECT_MAX_ATTEMPTS}, retrying in ${Math.round(delayMs)}ms.` |
| 145 | ) |
| 146 | await sleep(delayMs) |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Acquire the cross-process migration lock, failing loudly after the deadline |
no test coverage detected