Skip to content

Throttle is a rate limiter that implements the sliding window algorithm in under 64 bytes

License

Notifications You must be signed in to change notification settings

microbus-io/throttle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Throttle

Throttle is a rate limiter that implements the sliding window counter algorithm. It uses two counters - one for the current fixed window and one for the previous fixed window - to estimate the load of operations in the sliding window. It uses very little memory (under 64 bytes) but is not 100% accurate. Data suggests 0.003% of requests may be incorrectly categorized.

// Create a new throttle allowing 20 ops/second
th := throttle.New(time.Second, 20)

// Check if op is allowed
if th.Allow() {
    ...
}

Rate limiting is a technique that controls the rate of requests sent or received by a network, server, or other resource. There are a few common algorithms for rate limiting, each with its own pros and cons:

  • Leaky bucket
  • Token bucket - implemented in the standard library
  • Fixed window counter
  • Sliding window log
  • Sliding window counter - this library

Throttle is licensed by Microbus LLC under the Apache License 2.0.

Inspired by "Rate Limiter — Sliding Window Counter".

About

Throttle is a rate limiter that implements the sliding window algorithm in under 64 bytes

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages