udoprog / leaky-bucket

A token-based rate limiter based on the leaky bucket algorithm.
Apache License 2.0
93 stars 10 forks source link

Re-implement fast-path and test for numerical limits #28

Closed udoprog closed 6 months ago

udoprog commented 6 months ago

This re-introduces the fast path implementation, but only if Builder::fair is set to false.

The fast path is implemented as a spinning compare exchange over the existing token balance. So if tokens happen to be visible during an acquire, a task can try to acquire it immediately.

This however fails if the state cannot be restored, and the task then falls over to the slower locking path.