Git Product home page Git Product logo

throttled-queue's Introduction

@dutu/throttled-queue

throttled-queue is a promise Task Scheduler and Rate Limiter for Node.js

Contents

Changelog

1.2.0

  • added methods pause and start

Installation

npm install --save "git+https://github.com/dutu/throttled-queue.git"

Usage

throttled-queue is using @dutu/rate-limiter

Constructor

import { TokenBucketLimiter, RollingWindowLimiter } from '@dutu/rate-limiter'
const rateLimiter = new TokenBucketLimiter({ bucketSize: 10, tokensPerInterval: 1, interval: 'sec'})
const throttledQueue = new ThrottledQueue({ rateLimiter, maxConcurrent: 1, minDelay: 0, timeout: 0 })
Option Default Description
rateLimiter rate limiter for throttling the jobs
maxConcurrent 1 How many jobs can be executing at the same time. 0 is unlimited
minDelay 0 How long (ms) to wait after launching a job before launching another one
timeout 0 The number of milliseconds a job is given to complete. Jobs that execute for longer than timeout ms will be failed with an error

Methods

add()

Adds a job to the queue

throttledQueue.add({ id : 'someId', priority: 5, timeout: 0 }, async () => {
  // async function/promise
  await delay(5000)
})
Option Default Description
id Job Id
priority 5 Number from 0 to 9. Jobs with lower priority will always be executed first
timeout 0 The number of milliseconds a job is given to complete. Jobs that execute for longer than timeout ms will be failed with an error. 0 means no timeout.

pause(durationMs)

Pauses job execution for specified number of milliseconds. If null is passed, job execution is paused indefinitely.

throttledQueue.pause(500)
throttledQueue.pause(null)

start()

Starts job execution (if it was paused with pause())

throttledQueue.start()

clear()

Clears the queue (deletes all queued jobs)

throttledQueue.clear()

getSize()

Returns queue size (for specified priority)

throttledQueue.getSize()
throttledQueue.getSize({ priority: 5 })

Events

enqueue

throttledQueue.on("enqueue", function (info) {
  // This event is triggered when a job is added to the queue
});

dequeue

throttledQueue.on("dequeue", function (info) {
  // This event is triggered when a job is removed from the queue (for execution)
});

execute

throttledQueue.on("execute", function (error, jobId) {
  // This will be called when a job starts executing
});

done

throttledQueue.on("done", function (error, jobId) {
  // This will be called when a job execution completes successfully
});

failed

throttledQueue.on("failed", function (error, jobId) {
  // This will be called when a job fails
});

throttled-queue's People

Contributors

dutu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.