export interface CallContext { from: string; target: string; method: string; args: unknown[]; deadline: number; metadata: Record; attempt: number; timeout?: number; isColocated?: boolean; } type NextFunction = () => Promise; type InterceptorFunction = (callCtx: CallContext, next: NextFunction) => Promise; type FinalHandler = (callCtx: CallContext) => Promise; interface Logger { debug(message: string, data: Record): void; warn(message: string, data: Record): void; } interface Metrics { rpcCall?: (durationSecs: number, labels: Record) => void; counter?: (name: string, value: number, labels: Record) => void; histogram?: (name: string, value: number, labels: Record) => void; } /** * Run a call through an interceptor chain. * * Interceptors MAY call next() multiple times (e.g., the retry interceptor * invokes next() on each attempt). Downstream interceptors and the final * handler should therefore be idempotent-safe. */ export declare function runInterceptorChain(interceptors: InterceptorFunction[], callCtx: CallContext, finalHandler: FinalHandler): Promise; /** * Propagates deadlines across service boundaries. * * If gateway sets a 5s timeout, and the users service call takes * 2s, the billing service call from users only gets 3s — not a * fresh 5s. * * This prevents cascading timeouts where each service adds its * own full timeout on top. */ export declare function deadlineInterceptor(defaultTimeoutMs?: number): InterceptorFunction; /** * Logs every RPC call with duration and outcome. * Integrates with the service's structured logger. */ export declare function loggingInterceptor(logger: Logger | null): InterceptorFunction; /** * Implements the circuit breaker pattern. * * States: * CLOSED → Normal operation. Tracks failure rate. * OPEN → Calls fail immediately. Checked periodically. * HALF → Allows one probe call. Success → CLOSED, failure → OPEN. * * Prevents a failing service from consuming resources on the * calling side. Without this, a slow/dead service causes thread * starvation as pending requests pile up. */ interface CircuitBreakerOptions { failureThreshold?: number; /** Percentage-based failure rate threshold (0-100). When set, the breaker * opens if the failure rate within the sliding window exceeds this value * AND the minimum sample count is met. Takes precedence over failureThreshold. */ failureRateThreshold?: number; /** Minimum number of calls in the window before failure rate is evaluated. * Prevents the breaker from opening on a single failure out of 2 calls. */ minimumCallCount?: number; resetTimeoutMs?: number; successThreshold?: number; windowMs?: number; /** Number of concurrent probe requests allowed in half-open state. * Allows multiple requests to test the downstream service before deciding * whether to close or re-open. Default: 1 (legacy behavior). */ halfOpenMaxProbes?: number; } interface CircuitBreakerStats { state: "closed" | "open" | "half-open"; failureCount: number; successCount: number; totalCalls: number; failureRate: number; nextAttemptTime: string | null; } export declare class CircuitBreaker { failureThreshold: number; failureRateThreshold: number; minimumCallCount: number; resetTimeoutMs: number; successThreshold: number; halfOpenMaxProbes: number; private _windowMs; state: "closed" | "open" | "half-open"; failureCount: number; successCount: number; lastFailureTime: number; nextAttemptTime: number; private _failureWindow; private _successWindow; private _probeInFlight; constructor(options?: CircuitBreakerOptions); /** Prune expired entries from sliding windows and return current counts */ private _pruneWindows; /** Check if the breaker should open based on current window state */ private _shouldOpen; createInterceptor(): InterceptorFunction; get stats(): CircuitBreakerStats; } /** * Determine if an error is retryable given an idempotency setting. */ export declare function isRetryable(err: Error, idempotent: boolean | undefined): boolean; interface RetryOptions { maxAttempts?: number; baseDelayMs?: number; maxDelayMs?: number; idempotent?: boolean; serviceName?: string; } export declare function retryInterceptor(options?: RetryOptions): InterceptorFunction; /** * Tracks RPC metrics (call count, latency histogram, error rate). */ export declare function metricsInterceptor(metrics: Metrics | null): InterceptorFunction; interface BulkheadOptions { maxConcurrent?: number; } /** * Limits concurrent outgoing calls per service to prevent * cascade failures. Uses a simple counter — no external deps. */ export declare function bulkheadInterceptor(options?: BulkheadOptions): InterceptorFunction; export {}; //# sourceMappingURL=Interceptors.d.ts.map