rate

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2019 License: MIT Imports: 2 Imported by: 1

README

RateLimit

GoDoc Go Report Card License

This package contains a simple, thread-safe Go rate-limiter. This is a fork of bsm/ratelimit which was originally inspired by Antti Huima's algorithm.

Example
package main

import (
  "github.com/kelindar/rate"
  "log"
)

func main() {
  // Create a new rate-limiter, allowing up-to 10 calls per second
  rl := rate.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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

type Limiter struct {
	// contains filtered or unexported fields
}

Limiter instances are thread-safe.

func New

func New(rate int, per time.Duration) *Limiter

New creates a new rate limiter instance

func (*Limiter) Limit

func (rl *Limiter) Limit() bool

Limit returns true if rate was exceeded

func (*Limiter) Undo

func (rl *Limiter) Undo()

Undo reverts the last Limit() call, returning consumed allowance

func (*Limiter) UpdateRate

func (rl *Limiter) 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