Gathering detailed insights and metrics for @masx200/async-task-current-limiter
Gathering detailed insights and metrics for @masx200/async-task-current-limiter
Gathering detailed insights and metrics for @masx200/async-task-current-limiter
Gathering detailed insights and metrics for @masx200/async-task-current-limiter
异步任务限流器 asynchronous-task-current-limiter
npm install @masx200/async-task-current-limiter
Typescript
Module System
Node Version
NPM Version
65.6
Supply Chain
96.1
Quality
75.7
Maintenance
100
Vulnerability
98.6
License
TypeScript (79.27%)
JavaScript (20.73%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
NOASSERTION License
1 Stars
133 Commits
1 Forks
1 Watchers
5 Branches
1 Contributors
Updated on Mar 26, 2022
Latest Version
2.1.0
Package Id
@masx200/async-task-current-limiter@2.1.0
Unpacked Size
34.43 kB
Size
11.22 kB
File Count
8
NPM Version
8.11.0
Node Version
16.16.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
24
异步任务限流器 async-task-current-limiter
为了解决Error: EMFILE, too many open files
的问题而生.
有太多的文件已打开,已经不能再打开了。
当您尝试一次在系统上打开太多文件时,就会发生这种情况。
由于 Node
的异步特性,如果您fs.readFile
快速连续执行很多或类似的操作,则可以轻松达到系统的 maxfiles
限制。
1yarn add https://github.com/masx200/async-task-current-limiter.git
1import AsyncLimiterClass from "@masx200/async-task-current-limiter";
创建一个异步限流器对象,设定最大同时执行的异步任务数 30 个
当成函数使用
1const asynclimiter = AsyncLimiterClass(30);
或者当成类使用
1const asynclimiter = new AsyncLimiterClass(30);
监听异步限流器的free
和full
的事件
1const listener = (data) => console.log(JSON.stringify(data)); 2 3asynclimiter.target.on("free", listener); 4 5asynclimiter.target.on("full", listener);
用异步限流包装器,包装一个要限流的异步操作函数,
1async function asyncread() { 2 return await new Promise((s) => { 3 setTimeout(() => { 4 s("data:" + Math.random()); 5 }, Math.random() * 2000); 6 }); 7} 8 9const limitread = asynclimiter.asyncwrap(asyncread);
进行大批量异步操作的限流
1for (let i = 0; i < 1000; i++) { 2 setTimeout(() => { 3 limitread().then(console.log); 4 }, Math.random() * 5000); 5}
通过调用被限流的异步函数,将调用传入内部队列。如果活跃调用小于最大并发数,将会被取出直接执行,反之则继续呆在队列中。
当一个异步调用结束的时候,会从队列前取出调用执行。以此来保证异步调用的活跃量不高于限定值。
https://github.com/masx200/async-task-current-limiter/blob/master/dist/index.d.ts
AsyncLimiterClass(max)
1.当成函数使用
2.当成类使用
传入参数为限流器设定最大同时执行的任务数
asynclimiter.target
发布订阅的事件目标对象
https://github.com/masx200/event-emitter-target
在限流器空闲的时候触发
可监听的参数类型为 statusdata
接口
在限流器占满的时候触发
可监听的参数类型为 statusdata
接口
1interface statusdata { 2 status: 空闲状态; 3 queue: { 4 max: number; 5 current: number; 6 }; 7 limiter: { 8 max: number; 9 current: number; 10 }; 11}
asynclimiter.asyncwrap(fun)
异步限流包装器
传入函数必须返回一个Promise
,
返回一个被限流的异步操作函数
asynclimiter.status()
获取限流器状态的函数,返回'free'或者'full'
asynclimiter.limiter.max
异步限流器的最大同时执行的任务数
asynclimiter.limiter.current
异步限流器的当前同时执行的任务数
asynclimiter.queue.max
异步限流器的异步任务队列总数
asynclimiter.queue.current
异步限流器的异步任务队列中已经执行的任务个数
使用异步限流器解决同时打开过多文件的报错
1import fs from "fs"; 2import AsyncLimiterClass from "@masx200/async-task-current-limiter"; 3const asynclimiter = AsyncLimiterClass(50); 4 5declare const files: string[]; 6const limitreadfile = asynclimiter.asyncwrap(fs.promises.readFile); 7files.forEach(async (file) => { 8 const buf = await limitreadfile(file); 9 console.log(buf); 10});
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 0/14 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More