rate

package
v0.0.0-...-b349366 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2020 License: MIT Imports: 4 Imported by: 5

Documentation

Overview

Package rate implements rate counting and rate limiting.

The implementation is based on a "circular" counter that is split into a number of "buckets". The "timestamp" determines the bucket to be used for storing the event counter. This allows us to create multiple Limiters that use the same underlying Counter. It also makes it easier to scale and run in a clustered setup, storing the counter buckets in Redis.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Counter

type Counter interface {
	Add(timestamp time.Time, events uint64) error
	Get(timestamp time.Time, past time.Duration) (events uint64, err error)
}

Counter interface used in rate limiter

func NewCounter

func NewCounter(bucketSize, retention time.Duration) Counter

NewCounter returns a new rate counter with the given bucket size and retention

func NewRedisCounter

func NewRedisCounter(client *redis.Client, key string, bucketSize, retention time.Duration) Counter

NewRedisCounter returns a new redis-based counter

type Limiter

type Limiter interface {
	Limit() (limited bool, err error)
}

Limiter limits events

func NewLimiter

func NewLimiter(counter Counter, duration time.Duration, limit uint64) Limiter

NewLimiter returns a new limiter

Jump to

Keyboard shortcuts

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