ratelimit

package module
v2.0.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2017 License: MIT Imports: 2 Imported by: 12

README

RateLimit

Build Status GoDoc Go Report Card License

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/ratelimit"
  "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")
    }
  }
}
Documentation

Full documentation is available on GoDoc

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL