rateLimiting

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2023 License: BSD-2-Clause Imports: 5 Imported by: 6

Documentation

Overview

Package rateLimiting implements the leaky bucket algorithm: https://en.wikipedia.org/wiki/Leaky_bucket

Index

Constants

View Source
const ClientRateLimitErr = "Too many messages sent from ID %v with IP address " +
	"%s in a specific time frame by a user"

ClientRateLimitErr is returned once a user has hit the rate limit.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

type Bucket struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Bucket structure tracks the capacity and rate at which the remaining buckets decrease.

func CreateBucket

func CreateBucket(capacity, leaked uint32, leakDuration time.Duration,
	addToDb UpdateDB) *Bucket

CreateBucket generates a new empty bucket.

func CreateBucketFromDB

func CreateBucketFromDB(capacity, leaked uint32, leakDuration time.Duration,
	inBucket uint32, timestamp int64, updateDB UpdateDB) *Bucket

CreateBucketFromDB creates a bucket from parameters of a stored Bucket.

func CreateBucketFromLeakRatio

func CreateBucketFromLeakRatio(capacity uint32, leakRate float64,
	updateDB UpdateDB) *Bucket

CreateBucketFromLeakRatio generates a new empty bucket.

func CreateBucketFromParams

func CreateBucketFromParams(params *BucketParams, updateDB UpdateDB) *Bucket

CreateBucketFromParams generates a new empty bucket from custom parameters.

func (*Bucket) Add

func (b *Bucket) Add(tokens uint32) (bool, bool)

Add adds the specified number of tokens to the bucket. Returns true if the tokens were added; otherwise, returns false if there was insufficient capacity to do so.

func (*Bucket) AddWithExternalParams

func (b *Bucket) AddWithExternalParams(tokens, capacity, leakedTokens uint32,
	duration time.Duration) (bool, bool)

AddWithExternalParams adds the specified number of tokens to the bucket given external bucket parameters rather than the params specified in the bucket. Returns true if the tokens were added; otherwise, returns false if there was insufficient capacity to do so.

func (*Bucket) AddWithoutOverflow

func (b *Bucket) AddWithoutOverflow(tokens uint32) (bool, bool)

func (*Bucket) Capacity

func (b *Bucket) Capacity() uint32

Capacity returns the max number of tokens allowed in the bucket.

func (*Bucket) IsEmpty

func (b *Bucket) IsEmpty() bool

IsEmpty returns true if the bucket is empty.

func (*Bucket) IsFull

func (b *Bucket) IsFull() bool

IsFull returns true if the bucket is overflowing (i.e. no remaining capacity for additional tokens) .

func (*Bucket) IsFullOrWhitelist

func (b *Bucket) IsFullOrWhitelist() bool

IsFullOrWhitelist returns true if the bucket is overflowing (i.e., no remaining capacity for additional tokens) or if the bucket is whitelisted.

func (*Bucket) IsLocked

func (b *Bucket) IsLocked() bool

IsLocked returns true if the bucket is locked.

func (*Bucket) IsWhitelisted

func (b *Bucket) IsWhitelisted() bool

IsWhitelisted returns true if the bucket is on the whitelist.

func (*Bucket) MarshalJSON

func (b *Bucket) MarshalJSON() ([]byte, error)

MarshalJSON marshals the Bucket into valid JSON. This function adheres to the json.Marshaler interface. Note: it does not include the database function.

func (*Bucket) Remaining

func (b *Bucket) Remaining() uint32

Remaining returns the number of tokens in the bucket.

func (*Bucket) SetAddToDB

func (b *Bucket) SetAddToDB(updateDB UpdateDB)

SetAddToDB should be called after unmarshalling if you need a database function.

func (*Bucket) UnmarshalJSON

func (b *Bucket) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshalls the JSON into the Bucket. This function adheres to the json.Unmarshaler interface. Note: it does not include the database function; call Bucket.SetAddToDB to add it.

type BucketMap

type BucketMap struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

BucketMap structure is a collection of buckets in a map, each with the same capacity and leak rate. The structure contains an optional database backend.

func CreateBucketMap

func CreateBucketMap(capacity, leaked uint32, leakDuration, pollDuration,
	bucketMaxAge time.Duration, db Storage, quit chan struct{}) *BucketMap

CreateBucketMap creates a new BucketMap structure and starts the stale bucket removal thread. The leak rate is calculated by dividing leaked by leakDuration. If a database is being used, then all the stored buckets are reloaded from storage on creation of the map.

NOTE: If db is nil, then the database will not be used. If the quit channel is not provided, then the stale bucket removal service will not start.

func CreateBucketMapFromParams

func CreateBucketMapFromParams(params *MapParams, db Storage,
	quit chan struct{}) *BucketMap

CreateBucketMapFromParams creates a new BucketMap from the buckets MapParams structure.

NOTE: If db is nil, then the database will not be used. If the quit channel is not provided, then the stale bucket removal service will not start.

func (*BucketMap) AddBucket

func (bm *BucketMap) AddBucket(key string, capacity, leaked uint32,
	leakDuration time.Duration) *Bucket

AddBucket adds a new bucket to the map. The leak rate is calculated by dividing leaked by leakDuration.

func (*BucketMap) AddToWhitelist

func (bm *BucketMap) AddToWhitelist(entries []string)

AddToWhitelist adds the list of entries to the bucket map and set them as whitelisted.

func (*BucketMap) DeleteBucket

func (bm *BucketMap) DeleteBucket(key string) error

DeleteBucket removes the bucket with the specified key from the map. If the bucket does not exist, then an error is returned.

func (*BucketMap) LookupBucket

func (bm *BucketMap) LookupBucket(key string) *Bucket

LookupBucket returns the bucket in the map with the specified key. If no bucket exists, then a new one is added to the map and returned.

type BucketParams

type BucketParams struct {
	Key        string  // Unique bucket key
	Capacity   uint32  // Maximum number of tokens the bucket can hold
	Remaining  uint32  // Current number of tokens in the bucket
	LeakRate   float64 // Rate that the bucket leaks tokens at [tokens/ns]
	LastUpdate int64   // Time that the bucket was most recently updated
	Locked     bool    // Prevents auto deletion when stale
	Whitelist  bool    // No limit for adding tokens to bucket
}

BucketParams structure holds all the values to save and restore a Bucket.

type MapParams

type MapParams struct {
	// Capacity for newly created buckets
	Capacity uint32

	// The leak rate is calculated by LeakedTokens / LeakDuration
	LeakedTokens uint32
	LeakDuration time.Duration

	// How often to look for and discard stale buckets
	PollDuration time.Duration

	// Age of stale buckets when discarded
	BucketMaxAge time.Duration
}

MapParams holds the values used for new BucketMaps.

type Storage

type Storage interface {
	// UpsertBucket inserts the BucketParams into Storage with the unique
	// BucketParams key. If a bucket already exists with the same key, its
	// values are updated.
	UpsertBucket(bp *BucketParams)

	// AddToBucket updates the remaining and lastUpdate of the bucket with the
	// given key in Storage. If the bucket does not exist, an error is returned.
	AddToBucket(key string, remaining uint32, lastUpdate int64) error

	// RetrieveBucket returns a BucketParams for the bucket with the given key
	// from Storage. If the bucket does not exist, an error is returned.
	RetrieveBucket(key string) (*BucketParams, error)

	// RetrieveAllBuckets returns an array of all the buckets found in Storage.
	RetrieveAllBuckets() []*BucketParams

	// DeleteBucket deletes the bucket with the given key from Storage. If no
	// bucket is found, an error is returned.
	DeleteBucket(key string) error
}

Storage is the generic interface used by the BucketMap for permanent storage.

type UpdateDB

type UpdateDB func(remaining uint32, lastUpdate int64)

UpdateDB updates the bucket in the database with the remaining tokens and the last update (in unix nano time).

Jump to

Keyboard shortcuts

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