UNPKG

@juspay/neurolink

Version:

The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002

76 lines (75 loc) • 2.25 kB
/** * Token Bucket Rate Limiter for URL Downloads * * Implements a token bucket algorithm to limit concurrent URL downloads. * This prevents DoS attacks from rapid URL download requests. * * Default configuration: 10 downloads per second */ import type { RateLimiterConfig } from "../types/index.js"; /** * Token Bucket Rate Limiter * * Uses a token bucket algorithm where: * - Tokens are consumed when a download is requested * - Tokens are refilled at a fixed rate * - Requests that exceed the limit are queued */ export declare class TokenBucketRateLimiter { private tokens; private config; private queue; private refillTimer; private lastRefillTime; constructor(config?: Partial<RateLimiterConfig>); /** * Start the token refill timer */ private startRefillTimer; /** * Refill tokens based on elapsed time */ private refillTokens; /** * Process queued requests when tokens become available */ private processQueue; /** * Acquire a token for a download * Returns immediately if token is available, otherwise queues the request * * @throws NeuroLinkError if queue is full or request times out */ acquire(): Promise<void>; /** * Get current rate limiter statistics */ getStats(): { availableTokens: number; queueLength: number; maxTokens: number; maxQueueSize: number; }; /** * Reset the rate limiter to initial state */ reset(): void; /** * Stop the rate limiter and clean up resources */ stop(): void; } /** * Global rate limiter instance for URL downloads * Default: 10 downloads per second */ export declare const urlDownloadRateLimiter: TokenBucketRateLimiter; /** * Rate-limited wrapper for async functions * Ensures the function is rate-limited using the provided rate limiter * * @param fn - The async function to wrap * @param rateLimiter - The rate limiter to use (defaults to urlDownloadRateLimiter) * @returns A rate-limited version of the function */ export declare function withRateLimit<T extends unknown[], R>(fn: (...args: T) => Promise<R>, rateLimiter?: TokenBucketRateLimiter): (...args: T) => Promise<R>;