Help us shape the future of Go! Take the 2026 Go Developer Survey

rlimiter

package module
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: MIT Imports: 8 Imported by: 0

README

rlimiter

Simple redis rate limit

example

// applied only to endpoint login and POST
grbinder.BindVerb(group.Group("/login", rlimiter.GinRateLimit(
  rlimiter.NewIPRateLimiter(
    &rlimiter.Rate{Window: 10 * time.Second, Limit: 10},
    "api.login"),
  []string{"POST"})),
  &loginHandler{})

// Applied to any request method
limiter := rlimiter.GinRateLimit(
  rlimiter.NewIPRateLimiter(
    &rlimiter.Rate{Window: 1 * time.Minute, Limit: 10},
    "api.hardwork"),
  []string{})

r := gin.Default()
r.GET("/hardwork", limiter, func(c *gin.Context) {
})
r.POST("/hardwork", limiter, func(c *gin.Context) {
})
r.PUT("/hardwork", limiter, func(c *gin.Context) {
})

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

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 GetClient added in v1.6.0

func GetClient() *redis.Client

func GinRateLimit

func GinRateLimit(limiter GinRateLimiter, methods []string) gin.HandlerFunc

func SetClient added in v1.6.0

func SetClient(c *redis.Client)

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

type Rate struct {
	Window time.Duration
	Limit  int64
}

Rate define window as describe in https://redis.io/commands/incr#pattern-rate-limiter-2

type RateLimiter

type RateLimiter interface {
	IncrementUsage(key string) (bool, error)
}

func NewRateLimiter

func NewRateLimiter(rate *Rate, prefix string) RateLimiter

Jump to

Keyboard shortcuts

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