{"id":19168124,"url":"https://github.com/masx200/async-task-current-limiter","last_synced_at":"2026-02-09T18:30:48.757Z","repository":{"id":37834306,"uuid":"239731238","full_name":"masx200/async-task-current-limiter","owner":"masx200","description":"异步任务限流器 asynchronous-task-current-limiter","archived":false,"fork":false,"pushed_at":"2023-08-04T00:49:40.000Z","size":1251,"stargazers_count":1,"open_issues_count":3,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-06T04:52:31.969Z","etag":null,"topics":["async-task","limiter"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/masx200.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-11T10:09:04.000Z","updated_at":"2022-03-26T08:04:04.000Z","dependencies_parsed_at":"2024-11-09T09:51:31.240Z","dependency_job_id":null,"html_url":"https://github.com/masx200/async-task-current-limiter","commit_stats":{"total_commits":97,"total_committers":3,"mean_commits":"32.333333333333336","dds":"0.24742268041237114","last_synced_commit":"464a00101dfbf57dea63576789a47f82d1361cd0"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/masx200/async-task-current-limiter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masx200%2Fasync-task-current-limiter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masx200%2Fasync-task-current-limiter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masx200%2Fasync-task-current-limiter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masx200%2Fasync-task-current-limiter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/masx200","download_url":"https://codeload.github.com/masx200/async-task-current-limiter/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masx200%2Fasync-task-current-limiter/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269207445,"owners_count":24378474,"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","status":"online","status_checked_at":"2025-08-07T02:00:09.698Z","response_time":73,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["async-task","limiter"],"created_at":"2024-11-09T09:41:22.070Z","updated_at":"2026-02-09T18:30:48.730Z","avatar_url":"https://github.com/masx200.png","language":"TypeScript","readme":"# async-task-current-limiter\n\n# 介绍\n\n异步任务限流器 `async-task-current-limiter`\n\n为了解决`Error: EMFILE, too many open files`的问题而生.\n\n有太多的文件已打开，已经不能再打开了。\n\n当您尝试一次在系统上打开太多文件时，就会发生这种情况。\n\n由于 `Node` 的异步特性，如果您`fs.readFile`快速连续执行很多或类似的操作，则可以轻松达到系统的 `maxfiles` 限制。\n\n# 安装教程\n\n```shell\nyarn add https://github.com/masx200/async-task-current-limiter.git\n```\n\n# 使用说明\n\n```js\nimport AsyncLimiterClass from \"@masx200/async-task-current-limiter\";\n```\n\n# 示例\n\n创建一个异步限流器对象,设定最大同时执行的异步任务数 30 个\n\n当成函数使用\n\n```js\nconst asynclimiter = AsyncLimiterClass(30);\n```\n\n或者当成类使用\n\n```js\nconst asynclimiter = new AsyncLimiterClass(30);\n```\n\n监听异步限流器的`free`和`full`的事件\n\n```js\nconst listener = (data) =\u003e console.log(JSON.stringify(data));\n\nasynclimiter.target.on(\"free\", listener);\n\nasynclimiter.target.on(\"full\", listener);\n```\n\n用异步限流包装器,包装一个要限流的异步操作函数,\n\n```js\nasync function asyncread() {\n    return await new Promise((s) =\u003e {\n        setTimeout(() =\u003e {\n            s(\"data:\" + Math.random());\n        }, Math.random() * 2000);\n    });\n}\n\nconst limitread = asynclimiter.asyncwrap(asyncread);\n```\n\n进行大批量异步操作的限流\n\n```js\nfor (let i = 0; i \u003c 1000; i++) {\n    setTimeout(() =\u003e {\n        limitread().then(console.log);\n    }, Math.random() * 5000);\n}\n```\n\n# 原理\n\n通过调用被限流的异步函数,将调用传入内部队列。如果活跃调用小于最大并发数，将会被取出直接执行，反之则继续呆在队列中。\n\n当一个异步调用结束的时候，会从队列前取出调用执行。以此来保证异步调用的活跃量不高于限定值。\n\n# API\n\nhttps://github.com/masx200/async-task-current-limiter/blob/master/dist/index.d.ts\n\n## `AsyncLimiterClass(max)`\n\n1.当成函数使用\n\n2.当成类使用\n\n传入参数为限流器设定最大同时执行的任务数\n\n## `asynclimiter.target`\n\n发布订阅的事件目标对象\n\nhttps://github.com/masx200/event-emitter-target\n\n### 事件 'free'\n\n在限流器空闲的时候触发\n\n可监听的参数类型为 `statusdata`接口\n\n### 事件 'full'\n\n在限流器占满的时候触发\n\n可监听的参数类型为 `statusdata`接口\n\n```ts\ninterface statusdata {\n    status: 空闲状态;\n    queue: {\n        max: number;\n        current: number;\n    };\n    limiter: {\n        max: number;\n        current: number;\n    };\n}\n```\n\n## `asynclimiter.asyncwrap(fun)`\n\n异步限流包装器\n\n传入函数必须返回一个`Promise`,\n\n返回一个被限流的异步操作函数\n\n## `asynclimiter.status()`\n\n获取限流器状态的函数,返回'free'或者'full'\n\n## `asynclimiter.limiter.max`\n\n异步限流器的最大同时执行的任务数\n\n## `asynclimiter.limiter.current`\n\n异步限流器的当前同时执行的任务数\n\n## `asynclimiter.queue.max`\n\n异步限流器的异步任务队列总数\n\n## `asynclimiter.queue.current`\n\n异步限流器的异步任务队列中已经执行的任务个数\n\n# 应用解决问题例子\n\n使用异步限流器解决同时打开过多文件的报错\n\n```ts\nimport fs from \"fs\";\nimport AsyncLimiterClass from \"@masx200/async-task-current-limiter\";\nconst asynclimiter = AsyncLimiterClass(50);\n\ndeclare const files: string[];\nconst limitreadfile = asynclimiter.asyncwrap(fs.promises.readFile);\nfiles.forEach(async (file) =\u003e {\n    const buf = await limitreadfile(file);\n    console.log(buf);\n});\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmasx200%2Fasync-task-current-limiter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmasx200%2Fasync-task-current-limiter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmasx200%2Fasync-task-current-limiter/lists"}