jhurliman / node-rate-limiter

A generic rate limiter for node.js. Useful for API clients, web crawling, or other tasks that need to be throttled
MIT License
1.51k stars 135 forks source link

Option to create the bucket full #64

Closed bieron closed 3 years ago

bieron commented 4 years ago

Hello.

The reason I need is solely for testing sake, so I'll welcome any other suggestion for how to elegantly create a TokenBucket "ready to spill" without waiting for it or ugly monkeypatching. Still, I think it's a valuable functionality, and it maintains previous behavior.

Feel free to modify this PR of course.

yoshigev commented 3 years ago

We need it also for real life - we are creating a token bucket on demand (per user), and want the user to be able to work with it immediately.

This is similar to https://github.com/jhurliman/node-rate-limiter/issues/18, which propose a different approach which I think is more generic.

yoshigev commented 3 years ago

BTW, I now see that event the code of RateLimiter is manually filling the TokenBucket after creating it (in https://github.com/jhurliman/node-rate-limiter/blob/master/lib/rateLimiter.js#L20-L21).

I guess this means that this functionality should be exposed.

@jhurliman, @SirR4T what do you say? Maybe make the new option part of an "options" argument of the constructor to allow additional options in the future? Or, just expose the "content" member in the Typescript?

jhurliman commented 3 years ago

@yoshigev I am leaning toward the second option of exposing content in the Typescript defs.

jhurliman commented 3 years ago

content is now exposed in the 2.0.0 rewrite