better-limiter
A simple and smart rate limiter with promises.
Install
npm install better-limiter
API
/** * Creates a limiter. * @param * @param * @param */ { ... } /** * Enqueue into the limiter and return a promise to be resolved automatically at the right time. * @async * @return */ { ... } /** * Throttle a function call. * @async * @param * @param * @return {Promise<*>} - Promise of the result returned from fn(args). */ { ... } /** * Make a throttled version of function fn. * @param * @return */ { ... } /** * Manually ternimate the interval loop. [This is normally invoked by this.enter() automaticly.] * @param */ { ... } moduleexports = Limiter;
Example
See a full example in example.js
Require and Initiliaze
const Limiter = ; const limt = 30 3; // 3 operations allowed for every 3 seconds.const even_limt = 30 3 true; // 1 operation allowed for every (3.0/3)=1.0 second.
Throttle function calles
// Limit log scrolling speed.for let i = 0; i < 7; ++i limt; even_limt;
Make Throttled Functions and redirect the calls.
const got = ;// redirect got.get() to a throttled version get()const get = limt; for let i = 0; i < 10; ++i ;
Changelog
2.1.0 / 2018-02-11
-
(Compatibility) Updated
simple-semaphore
to be compatible with Node 6! Better JSDoc.2.0.1 / 2017-08-10
-
(README) API document update.
2.0.0 / 2017-08-10
-
(Performance) Performance imporvement with updated
simple-semaphore
.
- (New API)
throttle(fn, ...args)
andmakeThrottledFn(fn)
added.
-
(Deprecate) Renamed
release()
intoterminate(releaseLeftoverTasks)
with minor functionality tweak. -
(JSDoc) Style improvement.
1.0.3 / 2017-08-09
-
README revision and bug-fix.
1.0.0 / Initial Release.
Lisense
Licensed under MIT Copyright (c) 2017-2018 Phoenix Song