Limit an API client loop to a set number of requests per second without writing your own timer logic.
Smooth out bursts in a high-throughput Go service by throttling an internal loop with a single Take() call before each operation.
This is a small Go library, written by the engineering team at Uber, that helps developers control how fast their programs make requests or perform operations. The specific technique it uses is called a leaky-bucket algorithm, which is a well-known method for smoothing out bursts of activity into a steady, controlled pace. You tell the library how many operations per second you want to allow, and before each operation you call a single function called Take. If you are within your allowed rate, Take returns immediately and you proceed. If you are going too fast, Take pauses your program just long enough to bring you back within the limit. This makes it straightforward to add rate limiting to any loop or repeated action without managing timers yourself. The library is intentionally minimal. It was built to have a simple API and low overhead for high-throughput situations. The README notes that for more complex rate-limiting needs, such as allowing short bursts above the limit or more fine-grained control, Go's standard extended library includes a more full-featured alternative. This package focuses on the common case where you just want a clean per-second cap with minimal code.
← uber-go on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.