limit

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TokenBurstMultiplier = 10 // 令牌桶的突发流量倍数
	RPMThreshold         = 60 // RPM阈值

)

Variables

This section is empty.

Functions

func GetMaxRate

func GetMaxRate(limiter RateLimiter) int

GetMaxRate 获取限流器的最大速率(rpm)

Types

type CountLimiter

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

func NewCountLimiter

func NewCountLimiter(rate int, rpm int, window time.Duration) *CountLimiter

func (*CountLimiter) Allow

func (l *CountLimiter) Allow(keyPrefix string) bool

func (*CountLimiter) AllowN

func (l *CountLimiter) AllowN(keyPrefix string, n int) bool

func (*CountLimiter) GetCurrentRate

func (l *CountLimiter) GetCurrentRate(keyPrefix string) (int, error)

type MemoryLimiter

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

MemoryLimiter is a memory-based rate limiter that implements the RateLimiter interface. It can be configured to use either a fixed window or token bucket approach.

func NewMemoryLimiter

func NewMemoryLimiter(rate int, rpm int, window time.Duration, useTokenBucket bool) *MemoryLimiter

NewMemoryLimiter creates a new memory-based rate limiter. If useTokenBucket is true, it uses a token bucket approach, otherwise it uses a fixed window approach.

func (*MemoryLimiter) Allow

func (l *MemoryLimiter) Allow(keyPrefix string) bool

Allow checks if a single request is allowed.

func (*MemoryLimiter) AllowN

func (l *MemoryLimiter) AllowN(keyPrefix string, n int) bool

AllowN checks if n requests are allowed.

func (*MemoryLimiter) GetCurrentRate

func (l *MemoryLimiter) GetCurrentRate(keyPrefix string) (int, error)

GetCurrentRate returns the current rate for the given key.

func (*MemoryLimiter) Stop

func (l *MemoryLimiter) Stop()

Stop stops the cleanup goroutine.

type RateLimiter

type RateLimiter interface {
	Allow(keyPrefix string) bool
	AllowN(keyPrefix string, n int) bool
	GetCurrentRate(keyPrefix string) (int, error) // 返回当前已使用的速率
}

RateLimiter 定义了限流器的通用接口

func NewAPILimiter

func NewAPILimiter(rpm int) RateLimiter

NewAPILimiter 根据RPM创建合适的限流器 固定窗口和滑动窗口比较 维度 固定窗口 滑动窗口(高并发内存占用大) 并发处理能力 高(简单计数器) 中(需维护时间片) 限流准确性 低(边界突刺) 高(平滑控制) 实现复杂度 低 高 适用场景 对突刺不敏感的高吞吐场景 对流量敏感的关键业务场景

type SlidingWindowLimiter

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

SlidingWindowLimiter 滑动窗口限流器

func NewSlidingWindowLimiter

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

NewSlidingWindowLimiter 创建新的滑动窗口限流器

func (*SlidingWindowLimiter) Allow

func (l *SlidingWindowLimiter) Allow(keyPrefix string) bool

Allow 检查是否允许一个请求通过

func (*SlidingWindowLimiter) AllowN

func (l *SlidingWindowLimiter) AllowN(keyPrefix string, n int) bool

AllowN 检查是否允许n个请求通过

func (*SlidingWindowLimiter) GetCurrentRate

func (l *SlidingWindowLimiter) GetCurrentRate(keyPrefix string) (int, error)

GetCurrentRate 获取当前速率

type TokenLimiter

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

func NewTokenLimiter

func NewTokenLimiter(rate, rpm, burst int) *TokenLimiter

NewTokenLimiter returns a new TokenLimiter that allows events up to rate and permits bursts of at most burst tokens.

func (*TokenLimiter) Allow

func (lim *TokenLimiter) Allow(keyPrefix string) bool

func (*TokenLimiter) AllowN

func (lim *TokenLimiter) AllowN(keyPrefix string, n int) bool

AllowN reports whether n events may happen at time now. Use this method if you intend to drop / skip events that exceed the rate. Otherwise, use Reserve or Wait.

func (*TokenLimiter) GetCurrentRate

func (lim *TokenLimiter) GetCurrentRate(keyPrefix string) (int, error)

GetCurrentRate 获取当前速率使用情况,返回已使用的速率

Jump to

Keyboard shortcuts

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