You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

arpm

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

arpm

Rate limiting asynchronous generator.

1.1.1
latest
Source
npmnpm
Version published
Weekly downloads
3
200%
Maintainers
1
Weekly downloads
 
Created
Source

arpm

Asynchronous Requests Per Minute

An asynchronous iterator that iterates at a given ticks/minute speed.

npm install arpm will fetch this library/example.

Example

./arpm.js is executable, and will count at a rate of 78/minute. It's main code is reproduced here:

var generate = require('most').generate
var arpm = require('arpm)

var rate = Number.parseInt(process.argv[2]) || 78
var count = 0
generate(arpm(rate)).observe(function () {
  console.log(count++)
})

Most is used to consume/iterate the asynchronous iterator here, however in future versions of JavaScript one could expect to be able to use the new async iteration language syntax to iteration (for await(const n of arpm(rate)){}).

For example usage, arpm.js can also be executed with a single numeric parameter, the request rate:

  • ./arpm.js 200 will count 200 a minute,
  • while ./arpm.js 33 would count 33 a minute.

Explainer

arpm uses one setInterval (by default running at 1Hz) to produce credit for the async generator, while the async generator consumes one credit each iteration, or, if no credit is available, the async generator returns a waiting Promise that will resolve (consuming one credit) as soon as credit becomes available.

Using the Example program, which has a default rate of 78/minute, we can see 0, 1, & 2 outputted once per second. In the following second, 3 & 4 are both outputted. This illustrates the rate behavior of arpm: 78/60min is 1.3 credit / second, so after 3 is rendered, there is still 1.2 credit available for consuming, allowing 4 to immediately fire. With n as the numbers printed out, at tick of 1Hz, we can look at the total credits produced and remaining credit available over time:

ntickaggregate creditsavailable credits
00s1.30.3
112.60.6
223.90.9
334.21.2
434.20.2
545.50.5
656.80.8
768.11.1
868.10.1

One could also imagine a slow rate, say 33/minute, which would build 0.55 credit per 1Hz tick

ntickaggregate creditsavailable credits
0s0.550.55
111.10.1
21.650.65
232.20.2
42.750.75
353.30.3
63.850.8
474.40.4
84.950.95
595.50.5
6106.050.05

Here we see it usually takes two ticks to build a full credit to consume.

Keywords

rate-limiting

FAQs

Package last updated on 10 Oct 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts