limiter

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrLimited = errors.New("rate limit exceeded")

Functions

func Limiter

func Limiter() gin.HandlerFunc

func LocalLimiter

func LocalLimiter() gin.HandlerFunc

func LocalLimiterV2

func LocalLimiterV2() gin.HandlerFunc

func NewDelayLimiter

func NewDelayLimiter(limit Waiter) gin.HandlerFunc

NewDelayLimiter 达到限流阈值后持有相关请求并等到执行

func NewErrorLimiter

func NewErrorLimiter(limit Allower) gin.HandlerFunc

NewErrorLimiter 达到限流阈值后直接报错

func RedisLimiter

func RedisLimiter() gin.HandlerFunc

Types

type Allower

type Allower interface {
	Allow() bool
}

Allower rate.NewLimiter(rate.Every(time.Minute), 1)

type CheckFn

type CheckFn = func(key string, expire int, limit int) (err error)

type LimitBase

type LimitBase struct {
	LimitInterface
	// Expire key 过期时间
	Expire int
	// Limit 周期时间
	Limit int
}

func (*LimitBase) GenerationKey

func (l *LimitBase) GenerationKey(c *gin.Context) string

func (*LimitBase) Process

func (l *LimitBase) Process(check CheckFn, expire int, limit int) gin.HandlerFunc

type LimitInterface

type LimitInterface interface {
	// GenerationKey 根据业务生成key 下面CheckOrMark查询生成
	GenerationKey(c *gin.Context) string

	// CheckOrMark 检查函数,用户可修改具体逻辑,更加灵活
	CheckOrMark(key string, expire int, limit int) error
}

type LocalLimit

type LocalLimit struct {
	LimitBase
	// contains filtered or unexported fields
}

func (*LocalLimit) CheckOrMark

func (l *LocalLimit) CheckOrMark(key string, expire int, limit int) error

type LocalLimitV2

type LocalLimitV2 struct {
	LimitBase
	// contains filtered or unexported fields
}

func (*LocalLimitV2) CheckOrMark

func (l *LocalLimitV2) CheckOrMark(key string, expire int, limit int) error

type RedisLimit

type RedisLimit struct {
	LimitBase
	// contains filtered or unexported fields
}

func (*RedisLimit) CheckOrMark

func (l *RedisLimit) CheckOrMark(key string, expire int, limit int) error

type Waiter

type Waiter interface {
	Wait(ctx context.Context) error
}

Jump to

Keyboard shortcuts

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