ratelimiter

package
v2.17.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2021 License: Apache-2.0 Imports: 8 Imported by: 2

Documentation

Index

Constants

View Source
const LuaScriptForRedis string = `` /* 1249-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/ratelimite.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,
	}
)

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
	//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
}

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
}

Result of limiter.Get

Jump to

Keyboard shortcuts

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