ratelimit

package
v1.58.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package ratelimit 提供 Redis 令牌桶分布式限流: 多实例共享配额,原子 Lua 脚本保证正确性。 场景:接口限流(跨实例)、短信发送频率、防刷。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

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

Limiter 分布式限流器。

func NewLimiter

func NewLimiter(rdb *redis.Client, prefix string) *Limiter

NewLimiter 创建限流器。prefix 为 key 前缀(如 "rl:api"),可为空。

func (*Limiter) Allow

func (l *Limiter) Allow(ctx context.Context, key string, rate, burst int) (bool, error)

Allow 请求 1 个令牌:放行返回 true;超额返回 false;错误返回 error。 rate 为每秒补充令牌数,burst 为桶容量(允许突发)。

func (*Limiter) AllowN

func (l *Limiter) AllowN(ctx context.Context, key string, rate, burst, n int) (bool, error)

AllowN 请求 n 个令牌。

func (*Limiter) Reset

func (l *Limiter) Reset(ctx context.Context, key string) error

Reset 清空指定 key 的令牌状态(测试/手动解封)。

Jump to

Keyboard shortcuts

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