定义于: async-rate-limiter.ts:216
一个创建异步速率限制函数的类。
速率限制是一种简单的方法,它允许一个函数在一个时间窗口内执行最多达到一个限制,然后阻止所有后续调用,直到窗口过去。这可能导致“爆发式”行为,即所有执行立即发生,然后是完全的阻塞。
速率限制器支持两种类型的窗口
与非异步的 RateLimiter 不同,这个异步版本支持从速率限制的函数返回值的,使其非常适合 API 调用和其他异步操作,你希望在 maybeExecute 调用中获得结果,而不是在速率限制函数内部将结果设置到状态变量中。
为了更平滑的执行模式,请考虑使用
速率限制最适合用于硬性的 API 限制或资源约束。对于 UI 更新或平滑频繁的事件,节流或防抖通常能提供更好的用户体验。
状态管理
错误处理
const rateLimiter = new AsyncRateLimiter(
async (id: string) => await api.getData(id),
{
limit: 5,
window: 1000,
windowType: 'sliding',
onError: (error) => {
console.error('API call failed:', error);
},
onReject: (limiter) => {
console.log(`Rate limit exceeded. Try again in ${limiter.getMsUntilNextWindow()}ms`);
}
}
);
// Will execute immediately until limit reached, then block
// Returns the API response directly
const data = await rateLimiter.maybeExecute('123');
const rateLimiter = new AsyncRateLimiter(
async (id: string) => await api.getData(id),
{
limit: 5,
window: 1000,
windowType: 'sliding',
onError: (error) => {
console.error('API call failed:', error);
},
onReject: (limiter) => {
console.log(`Rate limit exceeded. Try again in ${limiter.getMsUntilNextWindow()}ms`);
}
}
);
// Will execute immediately until limit reached, then block
// Returns the API response directly
const data = await rateLimiter.maybeExecute('123');
• TFn extends AnyAsyncFunction
new AsyncRateLimiter<TFn>(fn, initialOptions): AsyncRateLimiter<TFn>
new AsyncRateLimiter<TFn>(fn, initialOptions): AsyncRateLimiter<TFn>
定义于: async-rate-limiter.ts:224
TFn
AsyncRateLimiter<TFn>
fn: TFn;
fn: TFn;
定义于: async-rate-limiter.ts:225
key: string;
key: string;
定义于: async-rate-limiter.ts:220
options: AsyncRateLimiterOptions<TFn>;
options: AsyncRateLimiterOptions<TFn>;
定义于: async-rate-limiter.ts:221
readonly store: Store<Readonly<AsyncRateLimiterState<TFn>>>;
readonly store: Store<Readonly<AsyncRateLimiterState<TFn>>>;
定义于: async-rate-limiter.ts:217
getMsUntilNextWindow(): number
getMsUntilNextWindow(): number
定义于: async-rate-limiter.ts:457
返回下一次执行可能发生前的毫秒数。对于固定窗口,这是当前窗口重置之前的时间;对于滑动窗口,这是最旧的执行过期之前的时间。
number
getRemainingInWindow(): number
getRemainingInWindow(): number
定义于: async-rate-limiter.ts:447
返回当前窗口中允许的剩余执行次数
number
maybeExecute(...args): Promise<undefined | ReturnType<TFn>>
maybeExecute(...args): Promise<undefined | ReturnType<TFn>>
定义于: async-rate-limiter.ts:322
尝试在配置的限制内执行速率限制函数。如果当前窗口中的调用次数超过限制,则会拒绝执行。
错误处理
...Parameters<TFn>
Promise<undefined | ReturnType<TFn>>
一个 Promise,它会解析为函数的返回值,如果错误被 onError 处理,则返回 undefined
如果未配置 onError 处理程序,则为速率限制函数的错误
const rateLimiter = new AsyncRateLimiter(fn, { limit: 5, window: 1000 });
// First 5 calls will return a promise that resolves with the result
const result = await rateLimiter.maybeExecute('arg1', 'arg2');
// Additional calls within the window will return undefined
const result2 = await rateLimiter.maybeExecute('arg1', 'arg2'); // undefined
const rateLimiter = new AsyncRateLimiter(fn, { limit: 5, window: 1000 });
// First 5 calls will return a promise that resolves with the result
const result = await rateLimiter.maybeExecute('arg1', 'arg2');
// Additional calls within the window will return undefined
const result2 = await rateLimiter.maybeExecute('arg1', 'arg2'); // undefined
reset(): void
reset(): void
定义于: async-rate-limiter.ts:468
重置速率限制器状态
void
setOptions(newOptions): void
setOptions(newOptions): void
定义于: async-rate-limiter.ts:249
更新异步速率限制器选项
Partial<AsyncRateLimiterOptions<TFn>>
void
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。