/** * Quota Service * * Code-first API for defining and managing quotas with weighted distribution. * Uses ProductBuilder for CRUD operations and ProcessorService for execution. * Implements health-aware weighted selection with minimal latency. */ import { IQuotaDefineOptions, IQuotaSchema, IQuotaRunOptions, IQuotaDispatchOptions, IDefinedQuota, IResilienceServiceConfig } from './types'; import { IProductQuota } from '../types/productsBuilder.types'; /** * Error class for quota operations */ export declare class QuotaError extends Error { code: string; details?: unknown; constructor(message: string, code: string, details?: unknown); } /** * Result of a quota run with provider information */ export interface IQuotaRunResult { data: T; provider: string; latency: number; wasHealthy: boolean; retriesUsed: number; } export declare class QuotaService { private config; private productBuilders; private processorCache; private _privateKey; private readonly productEnv; constructor(config: IResilienceServiceConfig & { private_key: string; access_key?: string; }); private resolvePE; /** * Gets or creates a cached ProductBuilder */ private createNewProductBuilder; private getOrCreateProductBuilder; private bootstrapQuotaForRun; private getProductBuilder; /** * Gets or creates a cached ProcessorService for minimal latency */ private getProcessor; define>(options: IQuotaDefineOptions): Promise; create(product: string, quota: IDefinedQuota | IQuotaSchema): Promise; fetch(product: string, tag: string): Promise; fetchAll(product: string): Promise; update(product: string, tag: string, data: Partial): Promise; delete(product: string, tag: string): Promise; /** * Run a quota with health-aware weighted provider selection * * Optimization strategies: * 1. Parallel health status lookups from Redis (O(1) per provider) * 2. Cached processor instances to avoid re-initialization * 3. Pre-computed weighted selection for O(n) provider selection * 4. Exponential backoff only on failures */ run(options: IQuotaRunOptions): Promise>; /** * Parallel health status lookup from Redis - O(n) with parallel I/O */ private getHealthStatusesParallel; /** * Fast weighted selection with health-aware prioritization */ private selectHealthyProvider; /** * Execute provider action with exponential backoff retries */ private executeWithRetries; /** * Dispatch quota for deferred execution via job queue * * Note: For immediate execution, use run() instead. * Dispatch schedules the quota for background processing. */ dispatch(options: IQuotaDispatchOptions): Promise<{ jobId: string; }>; } export default QuotaService;