rate

package
v1.9.9 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Identifier a distinguish between different limiters
	Identifier string
	// TickerInterval duration of report/fetch increment to/from redis
	TickerInterval time.Duration
	// Limit the expected rate
	Limit struct {
		// Upper the expected maximum speed
		Upper uint32
		// Lower used when lose redis
		Lower uint16
	}
	// Log setup logger, if enable and no logger set, zap.NewProduction() will used.
	Log struct {
		Enable bool
		Logger *zap.Logger
	}
}

Config how setup Limiter

type Limiter

type Limiter interface {
	// Close the limiter
	Close() error
	// UpdateRate dynamic update rate, err will returned if parameters set zero
	UpdateRate(limitUpper uint32, limitLower uint16) error
	// Allow whether event can happen at time now
	Allow()
}

Limiter a distributed speed limiter

func NewLimiter

func NewLimiter(conf *Config, redis Redis) (Limiter, error)

NewLimiter create a new instance of Limiter

type Redis

type Redis interface {
	// Close the redis
	Close() error
	// IncrBy incr by value
	IncrBy(key string, value int64) *redis.IntCmd
	// Get by key
	Get(key string) *redis.StringCmd
}

Redis a single or cluster redis instance

func NewClusterRedis

func NewClusterRedis(endpoints []string, password string, tls *tls.Config) (Redis, error)

NewClusterRedis create a cluster redis client

func NewSingleRedis

func NewSingleRedis(endpoint, password string, db uint16, tls *tls.Config) (Redis, error)

NewSingleRedis create a single redis client

Jump to

Keyboard shortcuts

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