ratelimiter

package
v0.0.0-...-d07672d Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2023 License: GPL-3.0 Imports: 15 Imported by: 0

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()

func (*RateLimiter) TokenLimiter

func (r *RateLimiter) TokenLimiter() gin.HandlerFunc

Token 限速器

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

Jump to

Keyboard shortcuts

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