ratelimiter

package
v1.7.13 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2024 License: Apache-2.0 Imports: 8 Imported by: 2

Documentation

Index

Constants

View Source
const LuaScriptForRedis string = `` /* 1344-byte string literal not displayed */

LuaScriptForRedis script loading for cluster client and ring client for nodes changing. based on links below https://github.com/thunks/thunk-ratelimiter https://github.com/thunks/thunk-ratelimiter/blob/master/ratelimiter.lua

Variables

View Source
var (
	// DefaultRateLimiterConfig is the default rate limit middleware config.
	DefaultRateLimiterConfig = RateLimiterConfig{
		Skipper:                      echo.DefaultSkipper,
		Max:                          100,
		Duration:                     time.Minute * 1,
		Prefix:                       "LIMIT",
		Client:                       nil,
		SkipRateLimiterInternalError: false,
		LimiterKeyGenerator: func(c echo.Context) (string, []int) {
			return c.RealIP() + `@` + c.Method(), nil
		},
	}
)

Functions

func RateLimiter

func RateLimiter() echo.MiddlewareFunc

RateLimiter returns a rate limit middleware.

func RateLimiterWithConfig

func RateLimiterWithConfig(config RateLimiterConfig) echo.MiddlewareFunc

RateLimiterWithConfig returns a RateLimiter middleware with config. See: `RateLimiter()`.

Types

type RateLimiterConfig

type RateLimiterConfig struct {
	// Skipper defines a function to skip middleware.
	Skipper echo.Skipper

	// The max count in duration for no policy, default is 100.
	Max int

	// Count duration for no policy, default is 1 Minute.
	Duration time.Duration

	// Prefix key prefix, default is "LIMIT:".
	Prefix string

	// Use a redis client for limiter, if omit, it will use a memory limiter.
	Client RedisClient

	//If request gets a  internal limiter error, just skip the limiter and let it go to next middleware
	SkipRateLimiterInternalError bool

	LimiterKeyGenerator func(c echo.Context) (limiterKey string, policy []int)
}

RateLimiterConfig defines the config for RateLimiter middleware.

type RedisClient

type RedisClient interface {
	DeleteKey(string) error
	EvalulateSha(string, []string, ...interface{}) (interface{}, error)
	LuaScriptLoad(string) (string, error)
}

RedisClient interface

type Result

type Result struct {
	Total     int           // It Equals Options.Max, or policy max
	Remaining int           // It will always >= -1
	Duration  time.Duration // It Equals Options.Duration, or policy duration
	Reset     time.Time     // The limit record reset time
	Until     time.Duration
}

Result of limiter.Get

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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