@felixgeelhaar/govee-api-client
Version:
Enterprise-grade TypeScript client library for the Govee Developer REST API
83 lines • 2.65 kB
TypeScript
import { Logger } from 'pino';
/**
* Configuration for the sliding window rate limiter
*/
export interface SlidingWindowRateLimiterConfig {
/** Maximum number of requests allowed per window */
maxRequests: number;
/** Window duration in milliseconds */
windowMs: number;
/** Logger instance for debugging */
logger?: Logger;
/** Maximum queue size to prevent memory leaks */
maxQueueSize?: number;
}
/**
* High-performance sliding window rate limiter implementation
*
* Uses a sliding window algorithm to allow bursts up to the rate limit
* while maintaining the average rate over time. More efficient than
* sequential processing as it allows concurrent execution within limits.
*/
export declare class SlidingWindowRateLimiter {
private readonly maxRequests;
private readonly windowMs;
private readonly logger?;
private readonly maxQueueSize;
private readonly requestTimes;
private readonly requestQueue;
private processingQueue;
constructor(config: SlidingWindowRateLimiterConfig);
/**
* Factory method to create a rate limiter for Govee API (95 requests/minute)
*/
static forGoveeApi(logger?: Logger): SlidingWindowRateLimiter;
/**
* Factory method to create a custom rate limiter
*/
static custom(requestsPerMinute: number, logger?: Logger): SlidingWindowRateLimiter;
/**
* Executes a function with rate limiting applied
*
* @param fn - The async function to execute
* @returns Promise that resolves with the function's result
*/
execute<T>(fn: () => Promise<T>): Promise<T>;
/**
* Gets current rate limiter statistics
*/
getStats(): {
currentRequests: number;
maxRequests: number;
queueSize: number;
windowMs: number;
utilizationPercent: number;
canExecuteImmediately: boolean;
nextAvailableSlot: number;
};
/**
* Processes the request queue, executing requests when rate limits allow
*/
private processQueue;
/**
* Synchronously processes all requests that can execute immediately
*/
private processQueueSync;
/**
* Removes expired request timestamps from the sliding window
*/
private cleanupExpiredRequests;
/**
* Checks if a request can be executed immediately
*/
private canExecuteNow;
/**
* Calculates when the next request slot will be available
*/
private getNextAvailableTime;
/**
* Validates the rate limiter configuration
*/
private validateConfig;
}
//# sourceMappingURL=SlidingWindowRateLimiter.d.ts.map