redis

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2022 License: MIT Imports: 7 Imported by: 2

Documentation

Overview

Package redis provides a rate limiter based on redis.

Index

Constants

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

Variables

This section is empty.

Functions

func Client

func Client() *redis.Client

Client indicates the redis client of the rate limiter.

func SetRedis

func SetRedis(config *ConfigRedis) error

SetRedis sets the redis client.

Types

type ConfigRedis

type ConfigRedis struct {
	Host        string `yaml:"host"`
	Port        int    `yaml:"port"`
	Auth        string `yaml:"auth"`
	IdleTimeout int    `yaml:"idle_timeout"`
}

ConfigRedis sets the Redis.

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
}

A Limiter controls how frequently events are allowed to happen.

func NewLimiter

func NewLimiter(r Limit, b int, key string) *Limiter

NewLimiter returns a new Limiter that allows events up to rate r and permits bursts of at most b tokens.

func (*Limiter) Allow

func (lim *Limiter) Allow() bool

Allow is shorthand for AllowN(time.Now(), 1).

func (*Limiter) AllowN

func (lim *Limiter) AllowN(now time.Time, n int) bool

AllowN reports whether n events may happen at time now. Use this method if you intend to drop / skip events that exceed the rate limit. Otherwise use Reserve or Wait.

type Reservation

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

A Reservation holds information about events that are permitted by a Limiter to happen after a delay. A Reservation may be canceled, which may enable the Limiter to permit additional events.

Jump to

Keyboard shortcuts

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