leakybucket

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

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

Go to latest
Published: Dec 20, 2021 License: Apache-2.0 Imports: 2 Imported by: 1 module, 1 package

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

leakybucket

OSS Lifecycle

STATUS: this project has been archived. See https://github.com/honeycombio/home/blob/main/honeycomb-oss-lifecycle-and-practices.md

leakybucket is a go library for implementing rate limits using the leaky bucket algorithm

The rate limit is expressed in terms of a throughput and burst limit - over time the average throughput can't exceed the throughput limit, but in any given time period, traffic may be allowed to exceed the throughput limit in bursts.

This type of rate limit is often seen in network - you might buy 1Mb of bandwidth with a 100Mb burst limit. Over the course of time, your average throughput must be below 1Mb, but if you need to temporarily spike above it, it's fine.

Other implementations

There are quite a few leaky bucket implementations out there, each is slightly different. Some of the alternatives written in go:

Contributions and license

Features, bug fixes and other changes are gladly accepted. Please open issues or a pull request with your change.

All contributions will be released under the Apache License 2.0.

Documentation

Overview

Package leakybucket provides an in-memory implementation of the leaky bucket algorithm for rate limiting.

Summary

The leaky bucket algorithm (wikilink) is often used for rate limits or to create an even stream of traffic from a variable source. This package is only for rate limits.

When configuring a leaky bucket, you specify the allowed throughput (volume and time) and a burst limit.

Throughput limit is the rate at which the bucket drains. In other words, with a full bucket, this is the rate of events that will leak out. The throughput limit should be specified with two parameters, the DrainAmount is the number of entries to leak, and the DrainPeriod is the frequency to drain the bucket. For example, you would specify a throughput limit of 5 entries per second with a DrainAmount of 5 and a DrainPeriod of time.Second

Burst limit is the number of entries you want to allow before starting to reject new entries. This is the size of the bucket - as entries arrive the bucket fills, and when it reaches capacity new entries will be rejected. Burst limit should not be less than the throughput limit (or the throughput limit will never be reached, as the bucket will reject entries when it hits capacity).

When the rate limit is reached and adding a new entry would exceed the burst limit, a BucketOverflow error is returned.

Usage

Create one Bucket for each resource you wish to protect with a rate limit. When a new entry arrives for that resource, attempt to Add() the event to the bucket. If successful, Add() will return nil. If the rate limit has been exceeded, Add() will return a BucketOverflow error.

Buckets have internal locking so Add()ing to a bucket is thread safe.

Example
// Create a new bucket for a rate limit of 10/sec, with a burst limit of 50
b := Bucket{
	Capacity:    50,
	DrainAmount: 10,
	DrainPeriod: time.Second,
}
// override time for the sake of the example. ignore the next 2 lines.
n := newFakeNower()
b.testNower = n

// count successes and failures
var successes, failures int

// Adding the first 50 entries will succeed, the 51st will fail.
// This is us hitting the burst limit.
for i := 0; i < 51; i++ {
	if err := b.Add(); err == nil {
		successes++
	} else {
		failures++
	}
}
fmt.Printf("Successes: %d, Failures: %d\n", successes, failures)

// "wait" 1 second, and we should be able to add 10 more entries to the bucket
n.incrementSec(1)

// Add another 11 - the first 10 will succeed, the 11th will fail.
// This shows the rate limit constraining throughput
for i := 0; i < 11; i++ {
	if err := b.Add(); err == nil {
		successes++
	} else {
		failures++
	}
}
fmt.Printf("Successes: %d, Failures: %d\n", successes, failures)
Output:
Successes: 50, Failures: 1
Successes: 60, Failures: 2

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

type Bucket struct {

	// Capacity of the bucket is how many total spots are available in the bucket.
	// It is the burst limit. A capacity of 0 will reject all Add()s
	Capacity int

	// DrainAmount is the number of spots that open up each drain period. It is
	// the volume portion of the throughput limit. A drain amount of 0 means the
	// bucket doesn't leak, and once capacity is reached, will reject all
	// subsequent events.
	DrainAmount int

	// DrainPeriod is the frequency the bucket drains. It is the time portion of
	// the throughput limit. A drain period of 0 means always drain - every Add()
	// also drains, effectively meaning no throughput limit (so long as Capacity
	// and DrainAmount are greater than zero).
	DrainPeriod time.Duration
	// contains filtered or unexported fields
}

Bucket contains the counters and timers to implement your rate limit.

func (*Bucket) Add

func (b *Bucket) Add() error

Add increment the counter in the bucket. If the bucket overflows, the rate limit has been exceeded and Add() will return a BucketOverflow error.

type BucketOverflow

type BucketOverflow struct{}

BucketOverflow error will be returned when you fail to Add() to a bucket because the rate limit has been reached.

func (BucketOverflow) Error

func (b BucketOverflow) Error() string

Error implements the error interface

Jump to

Keyboard shortcuts

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