Documentation ¶
Index ¶
- Constants
- Variables
- func BlacklistChecker(c *gin.Context) bool
- func CacheByRawData(cacheExpire time.Duration, forgetTimeout time.Duration, opts ...cache.Option) gin.HandlerFunc
- func CacheByRequestPath(cacheExpire time.Duration, forgetTimeout time.Duration, opts ...cache.Option) gin.HandlerFunc
- func CacheByRequestURI(cacheExpire time.Duration, forgetTimeout time.Duration, opts ...cache.Option) gin.HandlerFunc
- func CacheStats() map[string]any
- func CheckBlacklist(asAPI bool) gin.HandlerFunc
- func CheckPathOr404() gin.HandlerFunc
- func CheckWhitelist(asAPI bool) gin.HandlerFunc
- func CheckWhitelistAnd(checker ForbiddenChecker, asAPI bool) gin.HandlerFunc
- func CheckWhitelistOr(checker ForbiddenChecker, asAPI bool) gin.HandlerFunc
- func CounterStats() map[string]any
- func DefaultLogCondition(c *gin.Context, elapsed time.Duration) bool
- func HTTPCounter(name string) gin.HandlerFunc
- func PurgeBlacklistCache()
- func PurgeWhitelistCache()
- func RecoveryWithLog(stack bool) gin.HandlerFunc
- func UseBlacklistCache(capacity, lifetime uint32) error
- func UseWhitelistCache(capacity, lifetime uint32) error
- func WebLogger(cond LogCondition) gin.HandlerFunc
- func WhitelistChecker(c *gin.Context) bool
- type ForbiddenChecker
- type LogCondition
- type RequestsLimiter
- func (r *RequestsLimiter) Allow() bool
- func (r *RequestsLimiter) Handler() gin.HandlerFunc
- func (r *RequestsLimiter) Limit() int32
- func (r *RequestsLimiter) Limited() uint64
- func (r *RequestsLimiter) Remaining() int32
- func (r *RequestsLimiter) Running() int32
- func (r *RequestsLimiter) SetLimit(n int32)
- func (r *RequestsLimiter) Stats() map[string]int
Constants ¶
const ( XRateLimitLimit = "X-RateLimit-Limit" XRateLimitRemaining = "X-RateLimit-Remaining" XRateLimitReset = "X-RateLimit-Reset" )
Variables ¶
var ( // DefaultLimitMessage 触发限制时的 429 消息 DefaultLimitMessage = "处理中的请求数过多" // DefaultLimitReached 达到限制时执行的 Hook DefaultLimitReached = func(c *gin.Context) { response.APIException(c, http.StatusTooManyRequests, DefaultLimitMessage, nil) } )
Functions ¶
func BlacklistChecker ¶ added in v0.3.2
BlacklistChecker 是否存在于黑名单, true 是黑名单 (黑名单为空时: 放过, false)
func CacheByRawData ¶
func CacheByRawData(cacheExpire time.Duration, forgetTimeout time.Duration, opts ...cache.Option) gin.HandlerFunc
CacheByRawData 以请求参数 Hash 值为键, 缓存请求结果
func CacheByRequestPath ¶
func CacheByRequestPath(cacheExpire time.Duration, forgetTimeout time.Duration, opts ...cache.Option) gin.HandlerFunc
CacheByRequestPath 以请求 URL.Path 为键, 缓存请求结果
func CacheByRequestURI ¶
func CacheByRequestURI(cacheExpire time.Duration, forgetTimeout time.Duration, opts ...cache.Option) gin.HandlerFunc
CacheByRequestURI 以请求 URL 为键, 缓存请求结果
func CacheStats ¶ added in v0.3.4
func CheckWhitelistAnd ¶ added in v0.3.2
func CheckWhitelistAnd(checker ForbiddenChecker, asAPI bool) gin.HandlerFunc
CheckWhitelistAnd 同时校验接口白名单和自定义检查器
func CheckWhitelistOr ¶ added in v0.3.2
func CheckWhitelistOr(checker ForbiddenChecker, asAPI bool) gin.HandlerFunc
CheckWhitelistOr 校验接口白名单或自定义检查器
func CounterStats ¶
func PurgeBlacklistCache ¶ added in v0.3.4
func PurgeBlacklistCache()
PurgeBlacklistCache 配置项变化时需要清空缓存, 由应用端在 Runtime() 调用
func PurgeWhitelistCache ¶ added in v0.3.4
func PurgeWhitelistCache()
PurgeWhitelistCache 配置项变化时需要清空缓存, 由应用端在 Runtime() 调用
func RecoveryWithLog ¶
func RecoveryWithLog(stack bool) gin.HandlerFunc
RecoveryWithLog GinRecovery 及日志 Ref: https://github.com/gin-contrib/zap
func UseBlacklistCache ¶ added in v0.3.4
UseBlacklistCache 重新设置黑名单检查时缓存, 配置变化时可选再次调用, 由应用端 Start() Runtime() 调用
func UseWhitelistCache ¶ added in v0.3.4
UseWhitelistCache 重新设置白名单检查时缓存, 配置变化时可选再次调用, 由应用端 Start() Runtime() 调用
func WebLogger ¶
func WebLogger(cond LogCondition) gin.HandlerFunc
WebLogger Web 日志, 记录错误日志, 推送请求日志到接口
func WhitelistChecker ¶ added in v0.3.2
WhitelistChecker 是否通过了白名单检查, true 是白名单 (白名单为空时: 通过, true)
Types ¶
type ForbiddenChecker ¶ added in v0.3.2
type LogCondition ¶
LogCondition 日志记录条件计算器
type RequestsLimiter ¶ added in v0.4.2
type RequestsLimiter struct {
// contains filtered or unexported fields
}
RequestsLimiter 同时处理的请求数限制 请求处理中 +1, 请求已返回: -1 请求超限返回 429 错误
func NewDefaultRequestsLimiter ¶ added in v0.4.2
func NewDefaultRequestsLimiter() *RequestsLimiter
NewDefaultRequestsLimiter 使用配置文件参数创建限制器 app.Use(middleware.NewDefaultRequestsLimiter().Handler())
func NewRequestsLimiter ¶ added in v0.4.2
func NewRequestsLimiter(limit int32, limitReached gin.HandlerFunc) *RequestsLimiter
func (*RequestsLimiter) Allow ¶ added in v0.4.2
func (r *RequestsLimiter) Allow() bool
func (*RequestsLimiter) Handler ¶ added in v0.4.2
func (r *RequestsLimiter) Handler() gin.HandlerFunc
func (*RequestsLimiter) Limit ¶ added in v0.4.2
func (r *RequestsLimiter) Limit() int32
func (*RequestsLimiter) Limited ¶ added in v0.4.3
func (r *RequestsLimiter) Limited() uint64
func (*RequestsLimiter) Remaining ¶ added in v0.4.2
func (r *RequestsLimiter) Remaining() int32
func (*RequestsLimiter) Running ¶ added in v0.4.2
func (r *RequestsLimiter) Running() int32
func (*RequestsLimiter) SetLimit ¶ added in v0.4.2
func (r *RequestsLimiter) SetLimit(n int32)
func (*RequestsLimiter) Stats ¶ added in v0.4.3
func (r *RequestsLimiter) Stats() map[string]int