p-limit vs express-rate-limit vs limiter vs bottleneck vs rate-limiter-flexible vs ratelimiter
웹 개발에서의 속도 제한 라이브러리
p-limitexpress-rate-limitlimiterbottleneckrate-limiter-flexibleratelimiter유사 패키지:

웹 개발에서의 속도 제한 라이브러리

속도 제한 라이브러리는 웹 애플리케이션에서 API 호출이나 요청의 빈도를 조절하여 서버의 과부하를 방지하고 안정성을 높이는 데 사용됩니다. 이러한 라이브러리는 다양한 방식으로 요청을 관리하고, 특정 시간 내에 허용된 요청 수를 초과하지 않도록 제어합니다. 이를 통해 서비스의 품질을 유지하고, 악의적인 공격으로부터 보호할 수 있습니다.

npm 다운로드 트렌드

3 년

GitHub Stars 순위

통계 세부사항

패키지
다운로드
Stars
크기
Issues
발행일
라이선스
p-limit461,131,1332,94316.1 kB02ヶ月前MIT
express-rate-limit99,847,9223,318158 kB911時間前MIT
limiter23,887,4361,571103 kB52ヶ月前MIT
bottleneck15,603,3862,009-897年前MIT
rate-limiter-flexible4,759,0733,605234 kB124日前ISC
ratelimiter46,514720-117年前MIT

기능 비교: p-limit vs express-rate-limit vs limiter vs bottleneck vs rate-limiter-flexible vs ratelimiter

기본 기능

  • p-limit:

    P-limit는 Promise의 동시 실행 수를 제한하여 비동기 작업을 효율적으로 관리할 수 있는 기능을 제공합니다.

  • express-rate-limit:

    Express-rate-limit는 특정 시간 내에 허용된 요청 수를 초과하지 않도록 간단하게 설정할 수 있는 기능을 제공합니다. Express.js와의 통합이 용이합니다.

  • limiter:

    Limiter는 기본적인 속도 제한 기능을 제공하며, 간단한 사용 사례에 적합합니다. 설정이 간단하여 빠르게 적용할 수 있습니다.

  • bottleneck:

    Bottleneck는 비동기 작업의 동시 실행 수를 조절하고, 요청의 지연을 설정할 수 있는 기능을 제공합니다. 이를 통해 API 호출의 빈도를 조절할 수 있습니다.

  • rate-limiter-flexible:

    Rate-limiter-flexible는 다양한 저장소를 지원하고, 복잡한 속도 제한 로직을 구현할 수 있는 유연성을 제공합니다.

  • ratelimiter:

    Ratelimiter는 간단한 속도 제한 기능을 제공하며, 기본적인 사용 사례에 적합합니다.

유연성

  • p-limit:

    P-limit는 Promise의 동시 실행 수를 유연하게 조절할 수 있으며, 비동기 작업의 효율성을 높일 수 있습니다.

  • express-rate-limit:

    Express-rate-limit는 기본적인 속도 제한 기능을 제공하지만, 유연성은 제한적입니다. 주로 간단한 사용 사례에 적합합니다.

  • limiter:

    Limiter는 간단한 사용 사례에 적합하며, 유연성은 제한적입니다. 복잡한 설정이 필요할 경우 다른 라이브러리를 고려해야 합니다.

  • bottleneck:

    Bottleneck는 다양한 설정을 통해 복잡한 비동기 작업을 유연하게 관리할 수 있습니다. 요청의 우선순위를 설정할 수 있는 기능도 제공합니다.

  • rate-limiter-flexible:

    Rate-limiter-flexible는 다양한 저장소와 함께 사용할 수 있어 유연성이 뛰어납니다. 복잡한 요구 사항을 충족할 수 있습니다.

  • ratelimiter:

    Ratelimiter는 기본적인 속도 제한 기능을 제공하며, 유연성은 제한적입니다.

설정 용이성

  • p-limit:

    P-limit는 간단한 API를 제공하여 설정이 용이합니다. 비동기 작업을 쉽게 관리할 수 있습니다.

  • express-rate-limit:

    Express-rate-limit는 설정이 매우 간단하여, 빠르게 적용할 수 있습니다. Express.js와의 통합이 용이합니다.

  • limiter:

    Limiter는 사용하기 쉽고, 설정이 간단하여 빠르게 적용할 수 있습니다.

  • bottleneck:

    Bottleneck는 다양한 설정 옵션을 제공하지만, 초기 설정이 복잡할 수 있습니다. 그러나 한 번 설정하면 유용하게 사용할 수 있습니다.

  • rate-limiter-flexible:

    Rate-limiter-flexible는 다양한 설정 옵션을 제공하지만, 초기 설정이 다소 복잡할 수 있습니다.

  • ratelimiter:

    Ratelimiter는 기본적인 속도 제한 기능을 제공하며, 설정이 간단합니다.

성능

  • p-limit:

    P-limit는 Promise의 동시 실행 수를 제한하여 비동기 작업의 성능을 최적화할 수 있습니다.

  • express-rate-limit:

    Express-rate-limit는 요청 수를 제한하여 서버의 부하를 줄이고, 성능을 향상시킬 수 있습니다.

  • limiter:

    Limiter는 간단한 속도 제한 기능을 제공하여 성능을 유지할 수 있습니다.

  • bottleneck:

    Bottleneck는 비동기 작업의 동시 실행 수를 조절하여 성능을 최적화할 수 있습니다. 요청의 지연을 설정하여 서버의 부하를 줄일 수 있습니다.

  • rate-limiter-flexible:

    Rate-limiter-flexible는 다양한 저장소를 지원하여 성능을 최적화할 수 있습니다. 복잡한 요구 사항을 충족할 수 있습니다.

  • ratelimiter:

    Ratelimiter는 기본적인 속도 제한 기능을 제공하여 성능을 유지할 수 있습니다.

사용 사례

  • p-limit:

    P-limit는 비동기 작업의 동시 실행 수를 제한해야 할 때 유용합니다.

  • express-rate-limit:

    Express-rate-limit는 Express.js 애플리케이션에서 간단한 속도 제한을 구현할 때 적합합니다.

  • limiter:

    Limiter는 간단한 속도 제한이 필요한 경우에 적합합니다.

  • bottleneck:

    Bottleneck는 API 호출을 최적화하고, 복잡한 비동기 작업을 관리해야 할 때 유용합니다.

  • rate-limiter-flexible:

    Rate-limiter-flexible는 복잡한 속도 제한 로직이 필요한 경우에 적합합니다.

  • ratelimiter:

    Ratelimiter는 기본적인 속도 제한 기능이 필요한 경우에 적합합니다.

선택 방법: p-limit vs express-rate-limit vs limiter vs bottleneck vs rate-limiter-flexible vs ratelimiter

  • p-limit:

    P-limit는 Promise의 동시 실행 수를 제한하는 데 유용합니다. 비동기 작업을 병렬로 처리하면서도 제어가 필요할 때 선택하세요.

  • express-rate-limit:

    Express-rate-limit는 Express.js 애플리케이션에 통합하여 간단하게 속도 제한을 설정할 수 있습니다. 기본적인 속도 제한 기능이 필요할 때 선택하세요.

  • limiter:

    Limiter는 간단한 속도 제한 기능을 제공하며, 사용하기 쉽고 가벼운 라이브러리를 원할 때 적합합니다.

  • bottleneck:

    Bottleneck는 복잡한 비동기 작업을 관리하고, 동시 실행 수를 조절해야 할 때 유용합니다. 특히, API 호출을 최적화하고자 할 때 적합합니다.

  • rate-limiter-flexible:

    Rate-limiter-flexible는 다양한 저장소를 지원하며, 복잡한 속도 제한 로직을 구현할 수 있습니다. 유연한 설정이 필요할 때 적합합니다.

  • ratelimiter:

    Ratelimiter는 간단한 속도 제한 기능을 제공하며, 기본적인 사용 사례에 적합합니다.

p-limit의 README

p-limit

Run multiple promise-returning & async functions with limited concurrency

Works in Node.js and browsers.

Install

npm install p-limit

Usage

import pLimit from 'p-limit';

const limit = pLimit(1);

const input = [
	limit(() => fetchSomething('foo')),
	limit(() => fetchSomething('bar')),
	limit(() => doSomething())
];

// Only one promise is run at once
const result = await Promise.all(input);
console.log(result);

API

pLimit(concurrency) default export

Returns a limit function.

concurrency

Type: number | object
Minimum: 1

Concurrency limit.

You can pass a number or an options object with a concurrency property.

rejectOnClear

Type: boolean
Default: false

Reject pending promises with an AbortError when clearQueue() is called. This is recommended if you await the returned promises, for example with Promise.all, so pending tasks do not remain unresolved after clearQueue().

import pLimit from 'p-limit';

const limit = pLimit({concurrency: 1});

limit(fn, ...args)

Returns the promise returned by calling fn(...args).

fn

Type: Function

Promise-returning/async function.

args

Any arguments to pass through to fn.

Support for passing arguments on to the fn is provided in order to be able to avoid creating unnecessary closures. You probably don't need this optimization unless you're pushing a lot of functions.

Warning: Avoid calling the same limit function inside a function that is already limited by it. This can create a deadlock where inner tasks never run. Use a separate limiter for inner tasks.

limit.map(iterable, mapperFunction)

Process an iterable of inputs with limited concurrency.

The mapper function receives the item value and its index.

Returns a promise equivalent to Promise.all(Array.from(iterable, (item, index) => limit(mapperFunction, item, index))).

If the iterable throws during iteration, the returned promise rejects with that error. Mapper functions that were already scheduled still run, and their results are discarded.

This is a convenience function for processing inputs that arrive in batches. For more complex use cases, see p-map.

limit.activeCount

The number of promises that are currently running.

limit.pendingCount

The number of promises that are waiting to run (i.e. their internal fn was not called yet).

limit.clearQueue()

Discard pending promises that are waiting to run.

This might be useful if you want to tear down the queue at the end of your program's lifecycle or discard any function calls referencing an intermediary state of your app.

Note: This does not cancel promises that are already running.

When rejectOnClear is enabled, pending promises are rejected with an AbortError. This is recommended if you await the returned promises, for example with Promise.all, so pending tasks do not remain unresolved after clearQueue().

limit.concurrency

Get or set the concurrency limit.

limitFunction(fn, options) named export

Returns a function with limited concurrency.

The returned function manages its own concurrent executions, allowing you to call it multiple times without exceeding the specified concurrency limit.

Ideal for scenarios where you need to control the number of simultaneous executions of a single function, rather than managing concurrency across multiple functions.

import {limitFunction} from 'p-limit';

const limitedFunction = limitFunction(async () => {
	return doSomething();
}, {concurrency: 1});

const input = Array.from({length: 10}, limitedFunction);

// Only one promise is run at once.
await Promise.all(input);

fn

Type: Function

Promise-returning/async function.

options

Type: object

concurrency

Type: number
Minimum: 1

Concurrency limit.

rejectOnClear

Type: boolean
Default: false

Reject pending promises with an AbortError when clearQueue() is called. This is recommended if you await the returned promises, for example with Promise.all, so pending tasks do not remain unresolved after clearQueue().

limitedFunction.clearQueue()

Discard pending calls that have not started yet.

When rejectOnClear is enabled, pending promises are rejected with an AbortError. Otherwise, their promises remain unresolved.

This does not cancel calls that are already running.

Recipes

See recipes.md for common use cases and patterns.

FAQ

How is this different from the p-queue package?

This package is only about limiting the number of concurrent executions, while p-queue is a fully featured queue implementation with lots of different options, introspection, and ability to pause the queue.

Related

  • p-throttle - Throttle promise-returning & async functions
  • p-debounce - Debounce promise-returning & async functions
  • p-map - Run promise-returning & async functions concurrently with different inputs
  • p-all - Run promise-returning & async functions concurrently with optional limited concurrency
  • More…