leakybucket

package module
v0.0.0-...-012dc20 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2022 License: Apache-2.0 Imports: 3 Imported by: 0

README

leakybucket Build Status Coverage Status Go Report Card

A size-limited hash-map implementation of the leaky bucket rate-limiting algorithm

Documentation

Index

Constants

View Source
const (
	// DrainPerSecond is the number of tokens per second drained from each bucket
	DrainPerSecond = 1000000000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	MaxEntries int
	// contains filtered or unexported fields
}

Cache is a size-limited hash-map implementation of the leaky bucket rate-limiting algorithm. Entries are roughly 78 bytes each.

func New

func New(maxEntries int) *Cache

New creates a Cache capable of storing up to maxEntries entries.

func (*Cache) Put

func (lb *Cache) Put(key uint64, quantity, limit int64) (int64, bool, bool)

Put attempts to add quantity tokens into the bucket referred to by a uint64 key that has a size limit of limit tokens. Return the current number of tokens in the bucket, whether the key already existed, and whether there was enough room in the bucket to add the requested number of tokens.

func (*Cache) PutString

func (lb *Cache) PutString(ks string, quantity, limit int64) (int64, bool, bool)

PutString attempt to add quantity tokens into the bucket referred to the string ks that has a size limit of limit tokens. Return the current number of tokens in the bucket, whether the key already existed, and whether there was enough room in the bucket to add the requested number of tokens.

Jump to

Keyboard shortcuts

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