Database Connection Retry Logic (utils/db-retry.ts)

llm-gateway · packages/backend/src/common/utils/db-retry.ts · typescript · MIT
database, lang:typescript, tag:resilience, tag:utility

Determines if a database connection failure is transient and worth retrying. Use this during application startup to handle temporary database unavailability.

// Source: https://github.com/mnfst/llm-gateway | License: MIT | Copyright 2026 MNFST, Inc
// Provided via justclaw.it.com; keep this notice with the code.
// Transport-level failures that mean "the database isn't reachable yet" — the
// only class of error worth retrying at boot (e.g. Postgres still starting).
const TRANSPORT_CODES = new Set([
  'ECONNREFUSED',
  'ECONNRESET',
  'ETIMEDOUT',
  'ENOTFOUND',
  'EAI_AGAIN',
  'EHOSTUNREACH',
  'ENETUNREACH',
  'EPIPE',
]);

// pg sometimes surfaces transport/startup failures as a message without a code.
const TRANSPORT_MESSAGES = [
  'connection terminated',
  'connection refused',
  'timeout expired',
  'the database system is starting up',
  'terminating connection',
  'server closed the connection',
];

/**
 * Decides whether a failed Typ

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key