{"id":24290534,"url":"https://github.com/dutu/rate-limiter","last_synced_at":"2026-02-26T15:40:31.921Z","repository":{"id":40825304,"uuid":"257840280","full_name":"dutu/rate-limiter","owner":"dutu","description":null,"archived":false,"fork":false,"pushed_at":"2023-01-08T23:18:04.000Z","size":827,"stargazers_count":2,"open_issues_count":4,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-15T00:42:33.973Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dutu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-22T08:32:09.000Z","updated_at":"2023-01-27T20:39:06.000Z","dependencies_parsed_at":"2023-02-08T08:02:27.439Z","dependency_job_id":null,"html_url":"https://github.com/dutu/rate-limiter","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dutu%2Frate-limiter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dutu%2Frate-limiter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dutu%2Frate-limiter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dutu%2Frate-limiter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dutu","download_url":"https://codeload.github.com/dutu/rate-limiter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234195804,"owners_count":18794361,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-16T11:53:11.506Z","updated_at":"2025-09-25T13:31:19.815Z","avatar_url":"https://github.com/dutu.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"rate-limiter\n====\n\n**@dutu/rate-limiter** is an implementation of a rate limiter that supports token bucket, rolling window and fixed window \n\n# Changelog\n\nThe module adheres to [Semantic Versioning](http://semver.org/).\n\nSee [github releases](https://github.com/dutu/rate-limiter/releases), where all notable changes are documented.\n\n# Installation\n\n```\nnpm install --save \"git+https://github.com/dutu/rate-limiter.git\"\n```\n\nor \n\n```\nnpm install --save \"@dutu/rate-limiter\"\n```\n\n# Usage\n\n```js\nimport { RollingWindowLimiter } from '@dutu/rate-limiter'\nimport { TokenBucketLimiter } from '@dutu/rate-limiter'\nimport { FixedWindowLimiter } from '@dutu/rate-limiter'\n```\nor\n```js\nconst RollingWindowLimiter = require('@dutu/rate-limiter').RollingWindowLimiter\nconst TokenBucketLimiter = require('@dutu/rate-limiter').TokenBucketLimiter\nconst FixedWindowLimiter = require('@dutu/rate-limiter').FixedWindowLimiter\n```\n\n## Rate-limiting Algorithms\n\n### Rolling Window\n\n```js\n  const limiter  = new RollingWindowLimiter({ tokensPerInterval, interval, stopped = false })\n```\n\n### Fixed Window\n\n```js\nconst limiter  = new FixedWindowLimiter({ tokensPerInterval, interval, stopped = false })\n```\n\n\u003e The Reservoir Interval starts from the moment `getTokens()` is called for the first time.\n\n\n### Token bucket\n\n```js\nconst limiter  = new TokenBucketLimiter({ bucketSize, tokensPerInterval, interval, stopped = false })\n```\n\n\u003e The rate limiter can be created with initial state 'stopped' and with no tokens, by specifying the parameter `stopped` set to `true` (default is `false`). When stopped, the rate limiter needs to be restarted by calling the method `start()` \n\n## Properties\n\n### `isStopped`\nBoolean value indicating if the rate limiter is stopped. \n\n## Methods\n\n### `tryRemoveTokens(count)`\n\nTries to remove a number af tokens and returns immediately a boolean value indicating if the token removal was successful. \n\n### `getTokens()`\n\nReturns the number of available tokens\n\n### `getDelayForTokens(count = 1)`\n\nReturns the number of milliseconds until the time when `count` tokens will be available. If the tokens are immediately available, it returns `0` (zero).\nIf the rate limiter is stopped and the tokens are not immediately available, the method returns `undefined`.\n\n### `async awaitTokens(count = 1)`\n\nReturns a promise which resolves when `count` tokens become available. If the rate limiter is stopped, the promise resolves after the rate limiter is restarted and the tokens are available.\n\n### `reset()`\n\nReinitializes the rate limiter, the reservoir is set to the initial size. If the limiter has been previously stopped, it is restarted.\n\n### `stop(empty = true)`\n\nStops the rate limiter. Rate limiter will add no more tokens. If parameter `false` is specified, the existing tokens can still be used until exhausted. \n\n### `start()`\n\n(Re)starts the rate limiter. Adding new tokens is resumed.\n\n## Quick examples\n\n```js\nimport { RollingWindowLimiter } from '@dutu/rate-limiter'\n\nasync function testToken() {\n  const limiter  = new RollingWindowLimiter({ tokensPerInterval: 20, interval: 1000 * 10 })\n  debug(limiter.getTokens())\n  debug(limiter.tryRemoveTokens(10))\n  debug(limiter.tryRemoveTokens(32))\n  debug(limiter.getTokens())\n  \n  await new Promise((resolve) =\u003e setTimeout(() =\u003e resolve(), 2000))\n  debug(limiter.tryRemoveTokens(10))\n  debug(limiter.getTokens())\n\n  debug(await limiter.awaitTokens(8))\n  debug(await limiter.awaitTokens(20))\n\n}\n\ntestToken()\n```\n\n# License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdutu%2Frate-limiter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdutu%2Frate-limiter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdutu%2Frate-limiter/lists"}