tomnomnom / meg

Fetch many paths for many hosts - without killing the hosts
MIT License
1.59k stars 266 forks source link

Change to Token Bucket rate limiting. Change defer semantics since Block() isn't in a hot path. #45

Open adamdrake opened 5 years ago

adamdrake commented 5 years ago

This is an example implementation to address #6. For the moment, it uses the Token Bucket Algorithm to provide rate limiting on a time interval basis with an initial and total capacity (for requests) of 1.

If the solution is agreeable, it's also possible to easily extend this and allow the user more advanced rate limiting with command line arguments for initial and total request capacity which would equate to how many requests a host starts with up front, how many requests can be accumulated in the bucket, etc.

For now, I left the implementation static at an initial and total capacity of 1.

I also changed Block() to defer r.Unlock() instead of having multiple called to unlock the map. While defer is currently slow, that shouldn't be a limiting factor in Block() as the code will probably be limited by network latency.