README

RateLimit Build Status

Simple, thread-safe Go rate-limiter. Inspired by Antti Huima's algorithm on http://stackoverflow.com/a/668327

Example
package main

import (
  "github.com/bsm/redeo"
  "log"
)

func main() {
  // 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")
    }
  }
}
Licence
Copyright (c) 2015 Black Square Media

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Expand ▾ Collapse ▴

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
    }

      RateLimit 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

            Source Files