limiter

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Limit

func Limit(opts ...Option) middleware.Middleware

Limit 返回一个限流中间件

Types

type GCRALimiter

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

GCRALimiter 一种适用于缓解流量突发的限流器 GCRA (Generic Cell Rate Algorithm) 通用单元速率算法

func NewGCRALimiter

func NewGCRALimiter(rate float64, burst int) *GCRALimiter

NewGCRALimiter 创建GCRA限流器

func (*GCRALimiter) Allow

func (l *GCRALimiter) Allow(key string) (bool, int)

Allow 实现Limiter接口

type KeyFunc

type KeyFunc func(ctx context.Context) string

KeyFunc 定义键生成函数类型

type Limiter

type Limiter interface {
	// Allow 判断请求是否被允许,返回是否允许和剩余配额
	Allow(key string) (bool, int)
}

Limiter 限流器接口

type Option

type Option func(*options)

Option 是限流中间件的选项

func WithFailureHandler

func WithFailureHandler(handler func(ctx context.Context, req interface{}) (interface{}, error)) Option

WithFailureHandler 设置失败处理函数

func WithKeyFunc

func WithKeyFunc(keyFunc KeyFunc) Option

WithKeyFunc 设置键生成函数

func WithLimiter

func WithLimiter(limiter Limiter) Option

WithLimiter 设置限流器实现

func WithLogger

func WithLogger(logger log.Logger) Option

WithLogger 设置日志记录器

type SlidingWindowLimiter

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

SlidingWindowLimiter 滑动窗口限流器

func NewSlidingWindowLimiter

func NewSlidingWindowLimiter(rate int, window time.Duration) *SlidingWindowLimiter

NewSlidingWindowLimiter 创建滑动窗口限流器

func (*SlidingWindowLimiter) Allow

func (l *SlidingWindowLimiter) Allow(key string) (bool, int)

Allow 实现Limiter接口

type TokenBucketLimiter

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

TokenBucketLimiter 令牌桶限流器

func NewTokenBucketLimiter

func NewTokenBucketLimiter(rate, capacity float64) *TokenBucketLimiter

NewTokenBucketLimiter 创建令牌桶限流器

func (*TokenBucketLimiter) Allow

func (l *TokenBucketLimiter) Allow(key string) (bool, int)

Allow 实现Limiter接口

Jump to

Keyboard shortcuts

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