rlimiter

package module
v1.16.0 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 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(opts ...AutoBanOption) gin.HandlerFunc

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 AutoBanOption added in v1.11.0

type AutoBanOption func(*autoBanOption)

func AutoBanWithAutobanPrefix added in v1.12.0

func AutoBanWithAutobanPrefix(prefix string) AutoBanOption

func AutoBanWithAutobannedPrefix added in v1.12.0

func AutoBanWithAutobannedPrefix(prefix string) AutoBanOption

func AutoBanWithBanDuration added in v1.11.0

func AutoBanWithBanDuration(duration time.Duration) AutoBanOption

func AutoBanWithRate added in v1.11.0

func AutoBanWithRate(rate *Rate) AutoBanOption

func AutoBanWithResponseStatus added in v1.11.0

func AutoBanWithResponseStatus(status int) AutoBanOption

func AutoBanWithStatuses added in v1.11.0

func AutoBanWithStatuses(statuses ...int) AutoBanOption

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