Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultLimitBurst = 1
DefaultLimitBurst 是默认的限制突发 DefaultLimitBurst is the default limit burst
var DefaultLimitRatePerSecond = float64(1)
DefaultLimitRatePerSecond 是默认的每秒限制速率 DefaultLimitRatePerSecond is the default limit rate per second
Functions ¶
This section is empty.
Types ¶
type Callback ¶
type Callback interface { // OnLimited 是一个方法,当请求被限流时被调用,接收一个 http.Request 指针作为参数 // OnLimited is a method that is called when a request is rate limited, it takes a pointer to http.Request as a parameter OnLimited(header *http.Request) }
Callback 是一个限流回调接口,用于处理被限流的请求 Callback is a rate limiting callback interface for handling requests that are rate limited
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config 是配置结构体,包含速率、突发、IP白名单、匹配函数和回调 Config is the configuration structure, including rate, burst, IP whitelist, match function and callback
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig 是一个函数,返回一个新的默认配置 DefaultConfig is a function that returns a new default configuration
func NewConfig ¶
func NewConfig() *Config
NewConfig 创建一个新的配置,包含默认的速率、突发、匹配函数、IP白名单和回调 NewConfig creates a new configuration, including default rate, burst, match function, IP whitelist and callback
func (*Config) WithBurst ¶
WithBurst 是一个方法,接收一个整数作为参数,设置配置的突发,并返回配置 WithBurst is a method that takes an integer as a parameter, sets the burst of the configuration, and returns the configuration
func (*Config) WithCallback ¶
WithCallback 是一个方法,接收一个回调作为参数,设置配置的回调,并返回配置 WithCallback is a method that takes a callback as a parameter, sets the callback of the configuration, and returns the configuration
func (*Config) WithIpWhitelist ¶
WithIpWhitelist 是一个方法,接收一个字符串切片作为参数,设置配置的 IP 白名单,并返回配置 WithIpWhitelist is a method that takes a slice of strings as a parameter, sets the IP whitelist of the configuration, and returns the configuration
func (*Config) WithMatchFunc ¶
func (c *Config) WithMatchFunc(fn com.HttpRequestHeaderMatchFunc) *Config
WithMatchFunc 是一个方法,接收一个匹配函数作为参数,设置配置的匹配函数,并返回配置 WithMatchFunc is a method that takes a match function as a parameter, sets the match function of the configuration, and returns the configuration
type IpRateLimiter ¶
type IpRateLimiter struct {
// contains filtered or unexported fields
}
IpRateLimiter 是一个结构体,包含配置和限流器 IpRateLimiter is a struct that contains configuration and rate limiter
func NewIpRateLimiter ¶
func NewIpRateLimiter(config *Config) *IpRateLimiter
NewIpRateLimiter 是一个函数,接收一个 Config 结构体的指针作为参数,返回一个新的 IpRateLimiter 结构体的指针 NewIpRateLimiter is a function that takes a pointer to the Config struct as a parameter and returns a new pointer to the IpRateLimiter struct
func (*IpRateLimiter) GetLimiter ¶
func (rl *IpRateLimiter) GetLimiter(key string) *rate.Limiter
GetLimiter 方法用于根据键获取限流器 The GetLimiter method is used to get the rate limiter based on the key
func (*IpRateLimiter) HandlerFunc ¶
func (rl *IpRateLimiter) HandlerFunc() gin.HandlerFunc
HandlerFunc 返回一个 gin.HandlerFunc,用于处理请求 HandlerFunc returns a gin.HandlerFunc for processing requests
func (*IpRateLimiter) SetBurst ¶
func (rl *IpRateLimiter) SetBurst(burst int)
SetBurst 方法用于设置限流器的突发流量 The SetBurst method is used to set the burst traffic of the rate limiter
func (*IpRateLimiter) SetRate ¶
func (rl *IpRateLimiter) SetRate(rate float64)
SetRate 方法用于设置限流器的速率 The SetRate method is used to set the rate of the rate limiter
func (*IpRateLimiter) Stop ¶
func (rl *IpRateLimiter) Stop()
Stop 方法用于停止 IP 限流器 The Stop method is used to stop the IP rate limiter
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter 是一个结构体,包含配置和限流器 RateLimiter is a struct that contains configuration and rate limiter
func NewRateLimiter ¶
func NewRateLimiter(config *Config) *RateLimiter
NewRateLimiter 是一个函数,接收一个 Config 结构体的指针作为参数,返回一个新的 RateLimiter 结构体的指针 NewRateLimiter is a function that takes a pointer to the Config struct as a parameter and returns a new pointer to the RateLimiter struct
func (*RateLimiter) GetLimiter ¶
func (rl *RateLimiter) GetLimiter() *rate.Limiter
GetLimiter 方法用于获取限流器 The GetLimiter method is used to get the rate limiter
func (*RateLimiter) HandlerFunc ¶
func (rl *RateLimiter) HandlerFunc() gin.HandlerFunc
HandlerFunc 返回一个 gin.HandlerFunc,用于处理请求 HandlerFunc returns a gin.HandlerFunc for processing requests
func (*RateLimiter) SetBurst ¶
func (rl *RateLimiter) SetBurst(burst int)
SetBurst 方法用于设置限流器的突发流量 The SetBurst method is used to set the burst traffic of the rate limiter
func (*RateLimiter) SetRate ¶
func (rl *RateLimiter) SetRate(rate float64)
SetRate 方法用于设置限流器的速率 The SetRate method is used to set the rate of the rate limiter
func (*RateLimiter) Stop ¶
func (rl *RateLimiter) Stop()
Stop 方法用于停止限流器,但在这里没有实现任何功能 The Stop method is used to stop the rate limiter, but it does not implement any functionality here