MCPcopy
hub / github.com/simstudioai/sim / connectWithRetry

Function connectWithRetry

packages/db/scripts/migrate.ts:134–149  ·  view source on GitHub ↗

* 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

()

Source from the content-addressed store, hash-verified

132 * errors (auth, unknown host config, etc.) still fail fast.
133 */
134async 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

Callers 1

migrate.tsFile · 0.85

Calls 5

backoffWithJitterFunction · 0.90
getPostgresErrorCodeFunction · 0.90
sleepFunction · 0.90
isTransientConnectErrorFunction · 0.85
warnMethod · 0.65

Tested by

no test coverage detected