ratelimiter

package module
v0.0.0-...-0a642bc Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2020 License: MIT Imports: 11 Imported by: 0

README

ratelimiter

A distributed token-bucket-based rate limiter ,using Redis.

##Quickstart

key := "ratelimiter:tokenbucket:test"
redisClient := &RedisClient{
	redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "", // no password set
		DB:       0,  // use default DB
	}),
}
limiter := NewLimiter(redisClient, key, &Config{
	Limit:    1,
	Capacity: 10,
})

if err := limiter.WaitN(context.Background(), 5); err != nil {
	panic(err)
}

##References https://godoc.org/golang.org/x/time/rate

Documentation

Index

Constants

Inf is the infinite rate limit; it allows all events (even if burst is zero).

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Limit defines the maximum frequency of some events.
	// Limit is represented as number of events per second.
	// A zero Limit allows no events.
	Limit Limit

	// the capacity of the bucket
	Capacity int64
}

Config is the bucket configuration.

type Limit

type Limit float64

Limit defines the maximum frequency of some events. Limit is represented as number of events per second. A zero Limit allows no events.

func Every

func Every(interval time.Duration) Limit

Every converts a minimum time interval between events to a Limit.

type Limiter

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

Limiter implements the Token Bucket Algorithm. See https://en.wikipedia.org/wiki/Token_bucket.

func NewLimiter

func NewLimiter(redis Redis, key string, config *Config) *Limiter

NewLimiter returns a new token-bucket rate limiter special for key in redis with the specified bucket configuration.

func (*Limiter) Config

func (b *Limiter) Config() Config

Config returns the bucket configuration in a concurrency-safe way.

func (*Limiter) SetConfig

func (b *Limiter) SetConfig(config *Config)

SetConfig updates the bucket configuration in a concurrency-safe way.

func (*Limiter) TakeN

func (b *Limiter) TakeN(ctx context.Context, n int64) (bool, error)

Take takes amount tokens from the bucket.

func (*Limiter) WaitN

func (b *Limiter) WaitN(ctx context.Context, n int64) (err error)

type Redis

type Redis interface {
	Eval(ctx context.Context, script string, keys []string, args ...interface{}) (interface{}, error)
	EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) (interface{}, error, bool)
}

type RedisClient

type RedisClient struct {
	Client *redis.Client
}

func (*RedisClient) Eval

func (r *RedisClient) Eval(ctx context.Context, script string, keys []string, args ...interface{}) (interface{}, error)

func (*RedisClient) EvalSha

func (r *RedisClient) EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) (interface{}, error, bool)

type Reservation

type Reservation struct {
	OK        bool
	Tokens    int64
	TimeToAct int64 `json:"timeToAct"`
	Update    bool
}

func (*Reservation) DelayFrom

func (r *Reservation) DelayFrom(now time.Time) time.Duration

type Script

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

func NewScript

func NewScript(redis Redis, src string) *Script

func (*Script) Run

func (s *Script) Run(ctx context.Context, keys []string, args ...interface{}) (interface{}, error)

Jump to

Keyboard shortcuts

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