Database Shard Retry Handler (shared/retry.ts)

ChatbotX · packages/database/src/sharding/shared/retry.ts · typescript · MIT
database, lang:typescript, tag:retry, tag:error-handling, tag:backend

Provides robust retry logic with exponential backoff for database shard connections encountering transient network or PostgreSQL errors.

// Source: https://github.com/ChatbotXIO/ChatbotX | License: MIT | Copyright 2024-present AhaChat LLC
// Provided via justclaw.it.com; keep this notice with the code.
import { ShardUnreachableError } from "./errors"

export interface RetryOptions {
  baseDelayMs?: number
  maxDelayMs?: number
  maxRetries?: number
}

const DEFAULT_OPTIONS: Required<RetryOptions> = {
  maxRetries: 3,
  baseDelayMs: 100,
  maxDelayMs: 2000,
}

const RETRYABLE_PG_CODES = new Set([
  "08000",
  "08003",
  "08006",
  "08001",
  "08004",
  "57P01",
  "57P02",
  "57P03",
  "40001",
  "40P01",
])

const RETRYABLE_ERROR_CODES = new Set([
  "ECONNRESET",
  "ECONNREFUSED",
  "ETIMEDOUT",
  "ENOTFOUND",
  "EAI_AGAIN",
])

function isRetryableError(error: unknown): boolean {
  if (error instanceof ShardUnreachableError

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

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

Get a free API key