Documentation
¶
Overview ¶
Package rlimiter is for rate limit on heavy endpoints
import "github.com/vanng822/rlimiter" grbinder.BindVerb(group.Group("/login", rlimiter.GinRateLimit(
rlimiter.NewIPRateLimiter(
&rlimiter.Rate{Window: 10 * time.Second, Limit: 10},
"api.login"),
[]string{"POST"})),
&loginHandler{})
Index ¶
- func AutoBan(rate *Rate, responseStatus int, banDuration time.Duration, statuses ...int) gin.HandlerFunc
- func GetClient() *redis.Client
- func GinRateLimit(limiter GinRateLimiter, methods []string) gin.HandlerFunc
- func SetClient(c *redis.Client)
- func SetGlobalPrefix(prefix string)
- type GinRateLimiter
- type Rate
- type RateLimiter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AutoBan ¶ added in v1.10.0
func AutoBan(rate *Rate, responseStatus int, banDuration time.Duration, statuses ...int) gin.HandlerFunc
* AutoBan is a Gin middleware that automatically bans IP addresses that exceed a specified rate limit or return certain HTTP status codes. It uses Redis to track the number of requests and manage banned IPs.
Parameters: - rate: A pointer to a Rate struct that defines the limit and window for the rate limiter. If nil, a default rate of 100 requests per minute is used. - responseStatus: The HTTP status code to return when an IP is banned. Default is 429 Too Many Requests. - statuses: A variadic list of HTTP status codes that, when returned by the server, will count towards the rate limit. If empty, it defaults to 404 Not Found.
The middleware checks if the client's IP address is currently banned before processing the request. If the IP is banned, it responds with the specified responseStatus. After processing the request, it checks if the response status code matches any of the specified statuses. If it does, it increments the usage count for that IP address. If the usage exceeds the defined rate limit, the IP address is banned for a specified duration.
func GinRateLimit ¶
func GinRateLimit(limiter GinRateLimiter, methods []string) gin.HandlerFunc
func SetGlobalPrefix ¶ added in v1.7.0
func SetGlobalPrefix(prefix string)
SetGlobalPrefix sets a global prefix for all rate limiter keys. This can be useful to avoid key collisions in Redis when using multiple applications or services. The prefix will be automatically suffixed with a colon if it does not already end with one. Remove the global prefix if an empty string is provided.
Types ¶
type GinRateLimiter ¶
type GinRateLimiter interface {
RateLimiter
Key(c *gin.Context) string
}
func NewIPRateLimiter ¶
func NewIPRateLimiter(rate *Rate, prefix string) GinRateLimiter
func NewParamRateLimiter ¶
func NewParamRateLimiter(rate *Rate, prefix, paramName string) GinRateLimiter
func NewPathRateLimiter ¶
func NewPathRateLimiter(rate *Rate, prefix string) GinRateLimiter
type Rate ¶
Rate define window as describe in https://redis.io/commands/incr#pattern-rate-limiter-2
type RateLimiter ¶
func NewRateLimiter ¶
func NewRateLimiter(rate *Rate, prefix string) RateLimiter