limiter

package
v0.0.30 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Middleware

func Middleware(m *Manager) server.ContextHandler

Middleware 返回中间件,按 API Path 进行限流

Types

type Config

type Config struct {
	Rate  float64 `yaml:"rate" json:"rate"`   // 每秒产生多少令牌
	Burst int     `yaml:"burst" json:"burst"` // 突发容量
}

Config 限流配置

type Manager

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

Manager 管理一组 limiter(支持按 key 区分,如 API 路径 / 用户ID)

func NewLimiterManager

func NewLimiterManager(config map[string]Config) *Manager

NewLimiterManager 初始化

func (*Manager) Allow

func (m *Manager) Allow(key string) bool

Allow 非阻塞尝试

func (*Manager) GetLimiter

func (m *Manager) GetLimiter(key string) *rate.Limiter

GetLimiter 获取对应 key 的 limiter(不存在则创建)

func (*Manager) Wait

func (m *Manager) Wait(ctx context.Context, key string) error

Wait 阻塞等待(带 context)

Jump to

Keyboard shortcuts

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