Documentation ¶
Index ¶
Constants ¶
View Source
const ( ConstDefaultLimiteRate = 10 ConstDefaultLimiteCapacity = 50 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FeatureOpts ¶
type FeatureOpts struct {
EnableMetrics bool `json:"enableMetrics,omitempty" yaml:"enableMetrics,omitempty"` // 开启 metrics 记录
}
func NewDefaultFeatureOpts ¶
func NewDefaultFeatureOpts() *FeatureOpts
type IpRateLimiter ¶
type IpRateLimiter struct {
// contains filtered or unexported fields
}
func NewIpRateLimiter ¶
func NewIpRateLimiter(limiterConf *IpRateLimiterConfig, featureOpts *FeatureOpts) *IpRateLimiter
func (*IpRateLimiter) Stop ¶
func (r *IpRateLimiter) Stop()
func (*IpRateLimiter) TokenLimiter ¶
func (r *IpRateLimiter) TokenLimiter() gin.HandlerFunc
Token 限速器
type IpRateLimiterConfig ¶
type IpRateLimiterConfig struct { Rate uint32 `json:"rate,omitempty" yaml:"rate,omitempty"` Capacity uint32 `json:"capacity,omitempty" yaml:"capacity,omitempty"` WhiteList []string `json:"whiteList,omitempty" yaml:"whiteList,omitempty"` HttpRequestHeaderMatchFunc func(h *http.Request) bool `json:"-" yaml:"-"` // 匹配请求头,指定特定的请求 Header 中信息做匹配,如果匹配,启动限速器。 Logger *zap.SugaredLogger `json:"-" yaml:"-"` }
func NewDefaultIpRateLimiterConfig ¶
func NewDefaultIpRateLimiterConfig() *IpRateLimiterConfig
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
func NewRateLimiter ¶
func NewRateLimiter(limiterConf *RateLimiterConfig, featureOpts *FeatureOpts) *RateLimiter
每秒钟处理10个请求,并且允许同时处理5个请求 也就是说,每秒钟处理10个请求,但是允许同时处理5个请求,超过5个请求的请求会被限制 ratelimit.NewBucketWithRate(10, 5)
func (*RateLimiter) Stop ¶
func (r *RateLimiter) Stop()
type RateLimiterConfig ¶
type RateLimiterConfig struct { Rate uint32 `json:"rate,omitempty" yaml:"rate,omitempty"` Capacity uint32 `json:"capacity,omitempty" yaml:"capacity,omitempty"` Logger *zap.SugaredLogger `json:"-" yaml:"-"` HttpRequestHeaderMatchFunc func(h *http.Request) bool `json:"-" yaml:"-"` // 匹配请求头,指定特定的请求 Header 中信息做匹配,如果匹配,启动限速器。 }
func NewDefaultRateLimiterConfig ¶
func NewDefaultRateLimiterConfig() *RateLimiterConfig
Click to show internal directories.
Click to hide internal directories.