Documentation ¶
Overview ¶
Simple, thread-safe Go rate-limiter. Inspired by Antti Huima's algorithm on http://stackoverflow.com/a/668327
Example:
// Create a new rate-limiter, allowing up-to 10 calls // per second rl := ratelimit.New(10, time.Second) for i:=0; i<20; i++ { if rl.Limit() { fmt.Println("DOH! Over limit!") } else { fmt.Println("OK") } }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter instances are thread-safe.
func New ¶
func New(rate int, per time.Duration) *RateLimiter
New creates a new rate limiter instance
func (*RateLimiter) Limit ¶
func (rl *RateLimiter) Limit() bool
Limit returns true if rate was exceeded
func (*RateLimiter) Undo ¶
func (rl *RateLimiter) Undo()
Undo reverts the last Limit() call, returning consumed allowance
func (*RateLimiter) UpdateRate ¶
func (rl *RateLimiter) UpdateRate(rate int)
UpdateRate allows to update the allowed rate
Click to show internal directories.
Click to hide internal directories.