Documentation
¶
Overview ¶
Package ratelimiter Web API 限流器. 限流方式: - user: 基于用户名 - tenant: 基于租户 - ip: 基于客户端IP 同时支持指定的key,应用于或排除出限流策略,以方便针对指定用户才限流,或某用户不限流. 配置: rateLimiter:
inMemoryOptions: rate: 1s limit: 100 redisOptions: rate: 1s limit: 100 panicOnError: false exclude: # 排除路径,这些路径不限流 - /health - /metrics storeKey: default # if use cache manager, only work for redis now. keyFunc: user # user, tenant, ip includeKeys: # 只对这些 key 应用限流(如果设置,其他 key 不限流) - user1 - user2 excludeKeys: # 这些 key 不限流(优先级高于 includeKeys) - admin - system
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrRedisClientNotFound = errors.New("redis client not found for rate limiter")
ErrRedisClientNotFound is returned when redis client is not found in cache manager
Functions ¶
func RegisterMiddleware ¶
RegisterMiddleware register rate limit middleware
Types ¶
type Config ¶
type Config struct {
// 不需要限流的接口
Exclude []string `json:"exclude"`
// 根据Exclude产生的Path.PathSkipper
Skipper handler.Skipper
// RedisOptions store为redis时的配置
RedisOptions *RedisOptions `json:"redisOptions"`
// InMemoryOptions store为memory时的配置
InMemoryOptions *InMemoryOptions `json:"inMemoryOptions"`
// StoreKey the key of cache manager.
StoreKey string `json:"storeKey"`
// KeyFunc
KeyFunc string `json:"keyFunc"`
// IncludeKeys 这些 key 应用限流(如果设置,其他 key 不限流)
IncludeKeys []string `json:"includeKeys"`
// ExcludeKeys 这些 key 不限流(优先级高于 includeKeys)
ExcludeKeys []string `json:"excludeKeys"`
// contains filtered or unexported fields
}
Config 限流配置
func (*Config) ApplyFunc ¶
func (mid *Config) ApplyFunc(cfg *conf.Configuration) gin.HandlerFunc
func (*Config) RateLimiter ¶
func (mid *Config) RateLimiter(s Store, options *Options) gin.HandlerFunc
RateLimiter is a function to get gin.HandlerFunc.
type InMemoryOptions ¶ added in v0.6.1
type InMemoryOptions struct {
// Rate 限流窗口时长
Rate time.Duration
// Limit 窗口内允许的最大请求数
Limit uint
// Skip 跳过限流的条件判断函数
Skip func(*gin.Context) bool
}
InMemoryOptions 内存存储配置
type Info ¶ added in v0.6.1
type Info struct {
// Limit 限流窗口内允许的最大请求数
Limit uint
// RateLimited 是否被限流
RateLimited bool
// ResetTime 限流窗口重置时间
ResetTime time.Time
// RemainingHits 剩余可用请求数
RemainingHits uint
}
Info 限流结果信息
type Options ¶ added in v0.6.1
type Options struct {
// KeyFunc 从请求中提取限流 key 的函数
KeyFunc func(*gin.Context) string
// ErrorHandler 被限流时的响应处理函数
ErrorHandler func(*gin.Context, Info)
// BeforeResponse 每次请求的响应前处理函数(设置 header 等)
BeforeResponse func(*gin.Context, Info)
}
Options 限流器选项
type RedisOptions ¶ added in v0.6.1
type RedisOptions struct {
// RedisClient Redis 客户端实例
RedisClient *redis.Client
// Rate 限流窗口时长
Rate time.Duration
// Limit 窗口内允许的最大请求数
Limit uint
// Skip 跳过限流的条件判断函数
Skip func(*gin.Context) bool
}
RedisOptions Redis 存储配置
type SafeInMemoryStore ¶ added in v0.6.1
type SafeInMemoryStore struct {
// contains filtered or unexported fields
}
SafeInMemoryStore 是完全线程安全的内存存储实现, 使用分片锁减少高并发下的竞争, 比单一全局锁提供更好的性能。
func NewSafeInMemoryStore ¶ added in v0.6.1
func NewSafeInMemoryStore(options *InMemoryOptions) *SafeInMemoryStore
NewSafeInMemoryStore 创建一个新的线程安全的内存限流存储
type SafeRedisStore ¶ added in v0.6.1
type SafeRedisStore struct {
// contains filtered or unexported fields
}
SafeRedisStore 是使用 Lua 脚本的线程安全的 Redis 存储实现。 使用 Lua 脚本确保原子性,修复了: 1. 竞态条件:Lua 脚本确保 Redis 中原子读取-检查-写入。 2. 窗口滑动:ts 仅在新窗口开始时设置,而非每次请求。
func NewSafeRedisStore ¶ added in v0.6.1
func NewSafeRedisStore(options *RedisOptions) *SafeRedisStore
NewSafeRedisStore 创建一个新的线程安全的 Redis 限流存储