import type { CircuitObservation, CircuitStateStore, CircuitWindow, SharedCircuitState } from '../ops/circuit-breaker.js'; import type { MigrationResult, SchemaMigration } from '../utils/schema-migrations.js'; import { type PostgresLikeClient } from './client.js'; import type { PostgresMigrateOptions } from './migrations.js'; /** Options for the Postgres circuit store. */ export interface PostgresCircuitStateStoreOptions { /** Table name, optionally schema-qualified. Defaults to `nexus_circuits`. */ table?: string; /** Clock for probe leases. Defaults to `Date.now`, the same clock the breaker uses. */ now?: () => number; } /** The schema, as statements: every migration's, in order. */ export declare function circuitStoreMigration(options?: Pick): string[]; /** * The versioned schema, which `migrate()` and `nexus db migrate` apply. Version 1 is the 2.0 table; * version 2 adds the shared failure window, so a breaker counts every worker's calls. */ export declare function circuitStoreMigrations(options?: Pick): SchemaMigration[]; /** * Shared circuit state in Postgres. * * Both operations the breaker needs to be atomic are single statements: a transition is written only * when it is at least as new as the stored one, and a probe is claimed only when no other worker holds * an unexpired claim. One row per provider, so a sync reads a handful of rows. */ export declare class PostgresCircuitStateStore implements CircuitStateStore { private readonly client; private readonly options; private readonly table; private readonly windowTable; private readonly now; constructor(client: PostgresLikeClient, options?: PostgresCircuitStateStoreOptions); /** * Applies this store's pending migrations, recorded in `nexus_schema_migrations`, under a lock. * Never runs implicitly. Safe on a database an older release created: what exists is kept. */ migrate(options?: PostgresMigrateOptions): Promise; /** Every provider's shared circuit state. */ read(): Promise; /** * Writes a transition, unless a newer one is already stored. Releases the provider's probe claim. */ write(state: SharedCircuitState): Promise; /** * Claims the right to probe a provider for `ttlMs`, in one statement. Resolves true for the * claimant, including when it already holds the claim. */ claimProbe(providerName: string, owner: string, ttlMs: number): Promise; /** * Adds one worker's calls to a provider's shared window, then sums it. The add, the trim of buckets * that rolled out, and the streak update are one statement, so two workers reporting at once both * count. Needs version 2 of the schema. */ observe(providerName: string, observation: CircuitObservation): Promise; }