rateLimit

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2024 License: MIT Imports: 7 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 {
	Mode            Mode          // 限流模式
	RedisClient     *redis.Client // redis 客户端
	Period          time.Duration // 限流周期
	CleanupInterval time.Duration // 清理过期限流器的间隔,只在ModeRateLimit模型下使用
	Rate            int           // 每个限流周期允许的最大请求数
	Burst           int           // 令牌桶的最大容量
}

type Limiter

type Limiter interface {
	Allow(ctx context.Context, key string) (bool, error) // 是否允许请求
	Wait(ctx context.Context, key string) error          // 等待请求
}

func NewLimiter

func NewLimiter(opts ...Option) (Limiter, error)

type Mode

type Mode int8
const (
	ModeTimeRate Mode = 1
	ModeRedis    Mode = 2
)

type Option

type Option func(*Config)

func WithBurst

func WithBurst(burst int) Option

WithBurst 设置令牌桶的最大容量

func WithCleanupInterval

func WithCleanupInterval(cleanupInterval time.Duration) Option

WithCleanupInterval 设置清理过期限流器的间隔

func WithMode

func WithMode(mode Mode) Option

func WithPeriod

func WithPeriod(period time.Duration) Option

WithPeriod 设置限流周期

func WithRate

func WithRate(rate int) Option

WithRate 设置限流周期内允许的最大请求数

func WithRedisClient

func WithRedisClient(client *redis.Client) Option

Jump to

Keyboard shortcuts

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