typed-scheduler
A Scheduler written in TypeScript for concurrency-limiting and rate-limiting
Docs | GitHub | npm | Yarn
Table of Contents
Installing
npm
$ npm i --save typed-scheduler
Yarn
$ yarn add typed-scheduler
Importing
typed-scheduler
is pre-bundled using the UMD pattern to support the
following module formats.
ES Module
CommonJS
const Scheduler =
Browser Global
AMD
Usage
Example
// rate limit to 2 messages every second// defaults to 3 prioritiesconst scheduler = concurrency: 2 rate: 1000 // queue 120 messages synchronously// scheduler will throttle to 2 messages per secondfor let i = 1; i <= 120; i++ // schedule with normal priority scheduler
priority
The second argument of schedule()
is used to set the priority class of the
scheduled function. The scheduler will handle scheduled functions in FIFO order
within each priority class, and higher priorities will always be handled before
lower priorities. A lower value means a higher priority.
ready()
and idle()
The output of this program demonstrates when each of these methods resolves.
{ console} const scheduler = concurrency: 1 rate: 1000 priorities: 3 for let i = 0; i < 3; i++ for let j = 0; j < 3; j++ // interleave priority classes scheduler scheduler scheduler
Output
0.079s task 0
1.103s task 3
2.109s task 6
2.109s priority 0 ready
3.113s task 1
4.119s task 4
5.119s task 7
5.119s priority 1 ready
6.123s task 2
7.123s task 5
8.129s task 8
8.129s priority 2 ready
9.135s scheduler idle
concurrency
and rate
Note that the following schedulers do not behave identically.
// 2 tasks every second concurrency: 2 rate: 1000 // 1 task every half second concurrency: 1 rate: 500
Using the options { concurrency: 2, rate: 1000 }
, the scheduler will execute
two tasks without delay, then wait a full second after either completes before
executing another task.
Using { concurrency: 1, rate: 500 }
will only execute one task at a time,
then wait for half a second after it completes before executing another task.
API
The complete reference API is available on GitHub Pages.
License
Copyright © 2019 Patrick Roberts
MIT License