ratelimiter

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

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

View Source
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

func RegisterMiddleware() web.Option

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 *ratelimit.RedisOptions `json:"redisOptions"`
	// InMemoryOptions store为memory时的配置
	InMemoryOptions *ratelimit.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) KeySkip

func (mid *Config) KeySkip(key string) bool

KeySkip 检查key是否应该被忽略

func (*Config) Name

func (mid *Config) Name() string

func (*Config) RateLimiter

func (mid *Config) RateLimiter(s ratelimit.Store, options *ratelimit.Options) gin.HandlerFunc

RateLimiter is a function to get gin.HandlerFunc. Base Logic is copied from ratelimit.RateLimiter

Jump to

Keyboard shortcuts

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