brayniac / ratelimit

rust ratelimiter
Apache License 2.0
30 stars 5 forks source link

bug: multi-threaded bucket starts full #24

Closed brayniac closed 7 years ago

brayniac commented 7 years ago

There's a bug that was discovered in #19 regarding the multi-threaded implementation starting with a full token bucket (really empty mpsc queue of size capacity). This can cause inaccurate rates at start until all capacity worth of tokens are consumed.

Two possible fixes. Option 1 - fill the mpsc queue with empty tokens on initialization - this causes the senders to be unable to push tokens onto the queue until the limiter has consumed them. Option 2 - use an mpmc queue in the "correct" direction. This will probably make the code easier to read, but at the cost of a dependency.