limiter

package
v1.1.8 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README

限流器

var limiter = NewIPRateLimiter(1, 5, 0)

rateLimiter := limiter.GetLimiter(r.RemoteAddr)
if !rateLimiter.Allow() {
http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
return
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Allow

func Allow() bool

Types

type KeyRateLimiter

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

func NewKeyRateLimiter

func NewKeyRateLimiter(r float64, b int, t int) *KeyRateLimiter

func (*KeyRateLimiter) GetLimiter

func (k *KeyRateLimiter) GetLimiter(key string) *Limiter

type Limiter

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

func NewLimiter

func NewLimiter(r float64, cap int, timeout int) *Limiter

NewLimiter r为放入桶中的速率/秒 cap位桶容量

r 每秒流入桶内的数量

cap 桶容量

timeout 等待时长[毫秒]

func (*Limiter) Allow

func (l *Limiter) Allow() bool

Allow 不等待 直接返回是否允许通过

Jump to

Keyboard shortcuts

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