ratelimit

package module
v0.0.0-...-facc847 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2017 License: Unlicense Imports: 3 Imported by: 0

README

ratelimit

Go package for rate limiting of functions using a leaky bucket algorithm

Build Status

Installation

Simply run

go get -u github.com/iand/ratelimit

Documentation is at http://godoc.org/github.com/iand/ratelimit

Usage

Example use:

import "github.com/iand/ratelimit"

func main() {
	limiter := ratelimit.PerSecond(5, 100)

	// Following should be executed at a rate of 5 per second
	for i := 0; i < 100; i++ {
		limiter.Do(func() { println("hello") })
	}

	// Blocks until the rate limiter has finished
	limiter.Drain()
}

LICENSE

This is free and unencumbered software released into the public domain. For more information, see http://unlicense.org/ or the accompanying UNLICENSE file.

Documentation

Overview

Package ratelimit implements rate limiting of functions using a leaky bucket algorithm

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Func

type Func func()

type RateLimiter

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

func PerSecond

func PerSecond(rate float64, capacity int) *RateLimiter

PerSecond creates a ratelimiter that executes a maximum number of operations per second

func (*RateLimiter) Do

func (rl *RateLimiter) Do(ctx context.Context, fn Func) bool

Do attempts to queue work for the rate limiter, returns false if it could not be queued. Each function queued will be executed in a separate goroutine so if the functions are long running, this could result in a large number of active goroutines, depending on the rate of the limiter

func (*RateLimiter) Drain

func (rl *RateLimiter) Drain()

Drain runs remaining work to completion and then stops the rate limiter

func (*RateLimiter) Stop

func (rl *RateLimiter) Stop()

Stop turns off the rate limiter immediately, losing any queued work

Jump to

Keyboard shortcuts

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