jhurliman/node-rate-limiter

How to save / load the limiter objects' status?

Closed this issue · 2 comments

Hi All,
Imagine I wanted to write a command line script in Node that needs to be rate-limited across multiple executions, e.g. a Twitter API client. I call it once, the limiter takes note of it, the script terminates. Then I call it again, the limiter is aware of the previous call, checks the limiting etc.

How do I manage this need with this library? E.g. can I "serialize" in some way the status of the library's objects, so that I can save them and re-load them back every time I run the script? I wouldn't want to use any backend component, as in the choice of Redis for classdojo/rolling-rate-limiter, but would be happy with something simpler and slower, such as file system or environment variables.

Thanks.

This is a good use case, and something I'd be interested in adding to the core library once it's proved out. For TokenBucket the process is simple: to serialize call drip() then save the .content somewhere. To deserialize, create a TokenBucket using the same parameters but then immediately set .content to the saved value. With RateLimiter, if you make the simplifying assumption that you aren't serializing then deserializing within the same "interval", it's just a matter of serializing myRateLimiter.tokenBucket as described above, then initializing a new rate limiter and setting myRateLimiter.tokenBucket = myDeserializedTokenBucket.

Adding support for serializing+deserializing within the same interval requires some additional work as you would need to store timestamp and interval length, then check if you are still in that interval when deserializing.

In 2.0.0 the .content field is explicitly part of the class definition so it should be straightforward to implement your own serialization.