npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

waiting-queue

v1.0.7

Published

waiting queue

Downloads

11

Readme

waiting-queue

waiting queue
等待队列:等待队列通过控制到处理模块的并发数 缓解压力以防止业务模块雪崩
目标:以此来缓解瞬时请求压力过大 造成大面积拒绝 比如 处理用户整点的闹钟请求

NPM version npm download David deps

use

npm install waiting-queue

create

  • new WaitingQueue(opt);
    • opt.concurrent 处理的并发度, 超过该值 则进入排队
    • opt.process(request) 请求处理函数
  • waitQueue.add(request);
    • request.acquireTimeout 等待处理的超时时间 从进入队列 到进入处理逻辑 默认5s
    • request.requestId (optional) 优化请求,避免重复请求 为空则每个逻辑都会处理 非空 当请求处理完成后(或者失败)则队列中requestId的也直接使用响应
    • request.process(request) 请求处理, 由于有requestId的逻辑 所以request和callback并不一一对应

    request.process优先级大于opt.callback

const WaitingQueue = require('../index').WaitingQueue;

let queue = new WaitingQueue({
    concurrent: 2
});

function process(i) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(i);
        }, 3000);
    });
}
const max = 5;
for(let i = 0; i < max; i++) {
    queue.add({
        id: i,
        process: process
    }, {
        waitTimeout: 1000
    }).then(id => {
        console.log('%s finish', i, id);
    }).catch(error => {
        console.error('%s error:%s', i, error.message);
    });
}