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: 14 modules, 16 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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