/** * PostgreSQL cross-replica per-key request rate limiter — the PG twin of {@link TiDBRateLimiter} * (src/plugins/tidb-rate-limiter.ts). Behaviourally identical: a thin {@link RateGate} over a shared * write-behind aggregating counter; `check` both COUNTS the request and decides, with the count summed * fleet-wide via atomic write-behind aggregation. SAME class surface + SAME SOFT-limiting semantics * (eventually consistent: a replica may briefly admit a few requests over the ceiling at a window * boundary before peers' counts propagate). * * [ref] A12 C1b: the `PgWriteBehindCounter` this file used to define inline now lives in * write-behind-counter.ts as the PG binding of ONE dialect-parameterized counter (the mysql2 twin is * `WriteBehindCounter`); the SQL-dialect deltas are documented and branched there. * * Schema: this store's `rate_limit` table is DISJOINT from the other PG stores' tables, so a * self-contained {@link PG_RATE_LIMITER_SCHEMA} + {@link ensureSchema} is safe (no shadowing). */ import type { Pool, PoolClient } from "pg"; import type { RateDecision, RateGate, RateLimits } from "../observability/rate-limit.js"; import { type CounterDegradeHook } from "./write-behind-counter.js"; import { type IndexSpec } from "./ensure-index.js"; export declare class PgRateLimiter implements RateGate { private readonly counter; /** [ref]:与 TiDB 孪生逐字同语义(限额=比较参数;关断哨兵停/起刷新环)。 */ private limit; private refreshRequested; private refreshIntervalMs; constructor(pool: Pool, limit: number, windowMs?: number, now?: () => number, onDegraded?: CounterDegradeHook, queryTimeoutMs?: number); setLimits(next: RateLimits): void; private syncRefresh; check(key: string): RateDecision; /** The flush loop self-reaps old windows; the periodic sweeper has nothing to do for this backend. */ sweep(): void; flush(): Promise; refresh(): Promise; startRefresh(intervalMs?: number): this; stop(): void; } /** * PG translation of tidb-pool's `rate_limit` DDL (SCHEMA_STATEMENTS): INT→INTEGER, DATETIME(3)→TIMESTAMPTZ(3), * inline `KEY idx_window`→separate `CREATE INDEX`. The composite PK (limit_key, window_bucket) backs the * atomic ON CONFLICT increment. */ export declare const PG_RATE_LIMITER_SCHEMA: string[]; /** S-287:本 store 的索引**声明**(两方言共用一份)。PG 侧由下面的 `ensureSchema` 应用,MySQL 侧由 `tidb-pool.ts` 的中央 `ensureSchema` 应用(那里内联 `KEY` 已在 `CREATE TABLE` 里 ⇒ 新建库探到即零 DDL,存量库缺谁补谁)。加索引以外的 schema 变更仍归运维,见 `plugins/ensure-index.ts` 头注。 */ export declare const RATE_LIMIT_INDEXES: readonly IndexSpec[]; /** Self-contained schema bootstrap for tests (the rate_limit table is disjoint from other stores). */ export declare function ensureSchema(pool: Pool | PoolClient): Promise; //# sourceMappingURL=pg-rate-limiter.d.ts.map