httprateredis

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2025 License: MIT Imports: 7 Imported by: 6

README

httprate-redis

CI workflow GoDoc Widget

Redis backend for github.com/go-chi/httprate, implementing httprate.LimitCounter interface.

See _example/main.go for usage.

Example

package main

import (
	"net/http"

	"github.com/go-chi/chi/v5"
	"github.com/go-chi/chi/v5/middleware"
	"github.com/go-chi/httprate"
	httprateredis "github.com/go-chi/httprate-redis"
)

func main() {
	r := chi.NewRouter()
	r.Use(middleware.Logger)

	r.Use(httprate.Limit(
			5,
			time.Minute,
			httprate.WithKeyByIP(),
			httprateredis.WithRedisLimitCounter(&httprateredis.Config{
				Host: "127.0.0.1", Port: 6379,
			}),
		))

	r.Get("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("This is IP rate-limited by 5 req/min"))
	})

	http.ListenAndServe(":3333", r)
}

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCounter added in v0.5.3

func NewCounter(cfg *Config) *redisCounter

func NewRedisLimitCounter

func NewRedisLimitCounter(cfg *Config) (*redisCounter, error)

func WithRedisLimitCounter

func WithRedisLimitCounter(cfg *Config) httprate.Option

Types

type Config

type Config struct {
	Disabled bool `toml:"disabled"` // default: false

	WindowLength time.Duration `toml:"window_length"` // default: 1m
	ClientName   string        `toml:"client_name"`   // default: ""
	PrefixKey    string        `toml:"prefix_key"`    // default: "httprate"

	// OnError lets you subscribe to all runtime Redis errors. Useful for logging/debugging.
	OnError func(err error)

	// Disable the use of the local in-memory fallback mechanism. When enabled,
	// the system will return HTTP 428 for all requests when Redis is down.
	FallbackDisabled bool `toml:"fallback_disabled"` // default: false

	// Timeout for each Redis command after which we fall back to a local
	// in-memory counter. If Redis does not respond within this duration,
	// the system will use the local counter unless it is explicitly disabled.
	FallbackTimeout time.Duration `toml:"fallback_timeout"` // default: 100ms

	// OnFallbackChange lets subscribe to local in-memory fallback changes.
	OnFallbackChange func(activated bool)

	// Client if supplied will be used and the below fields will be ignored.
	//
	// NOTE: It's recommended to set short dial/read/write timeouts and disable
	// retries on the client, so the local in-memory fallback can activate quickly.
	Client    redis.UniversalClient `toml:"-"`
	Host      string                `toml:"host"`
	Port      uint16                `toml:"port"`
	Password  string                `toml:"password"`   // optional
	DBIndex   int                   `toml:"db_index"`   // default: 0
	MaxIdle   int                   `toml:"max_idle"`   // default: 5
	MaxActive int                   `toml:"max_active"` // default: 10
}

Jump to

Keyboard shortcuts

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