Skip to content

Rate Limiter

RateLimiter provides a rate limiter middleware that limits the number of requests sent to the server from a particular IP or identifier within a time period.

By default, an in-memory store keeps track of requests. The default in-memory implementation is focused on correctness and may not be the best option for a high number of concurrent requests or a large number of distinct identifiers (>16k).

All core middleware lives in the middleware package:

import "github.com/labstack/echo/v5/middleware"

To add a rate limit to your application, add the RateLimiter middleware. The example below limits the application to 20 requests/sec using the default in-memory store:

e.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(20.0)))
config := middleware.RateLimiterConfig{
Skipper: middleware.DefaultSkipper,
Store: middleware.NewRateLimiterMemoryStoreWithConfig(
middleware.RateLimiterMemoryStoreConfig{Rate: 10, Burst: 30, ExpiresIn: 3 * time.Minute},
),
IdentifierExtractor: func(c *echo.Context) (string, error) {
id := c.RealIP()
return id, nil
},
ErrorHandler: func(c *echo.Context, err error) error {
return c.JSON(http.StatusForbidden, nil)
},
DenyHandler: func(c *echo.Context, identifier string, err error) error {
return c.JSON(http.StatusTooManyRequests, nil)
},
}
e.Use(middleware.RateLimiterWithConfig(config))
var (
// ErrRateLimitExceeded denotes an error raised when the rate limit is exceeded.
ErrRateLimitExceeded = echo.NewHTTPError(http.StatusTooManyRequests, "rate limit exceeded")
// ErrExtractorError denotes an error raised when the extractor function is unsuccessful.
ErrExtractorError = echo.NewHTTPError(http.StatusForbidden, "error while extracting identifier")
)

RateLimiterConfig · github.com/labstack/echo/v5@5196b9b

Fields from package source
FieldTypeDescriptionSource
Skipper Skipper — L39
BeforeFunc BeforeFunc — L40
IdentifierExtractor Extractor IdentifierExtractor uses *echo.Context to extract the identifier for a visitor L42
Store RateLimiterStore Store defines a store for the rate limiter L44
ErrorHandler func(c *echo.Context, err error) error ErrorHandler provides a handler to be called when IdentifierExtractor returns an error L46
DenyHandler func(c *echo.Context, identifier string, err error) error DenyHandler provides a handler to be called when RateLimiter denies access L48
Functions from package source
TypeSource
func RateLimiter(store RateLimiterStore) echo.MiddlewareFunc L86
func RateLimiterWithConfig(config RateLimiterConfig) echo.MiddlewareFunc L119

RateLimiterMemoryStoreConfig · github.com/labstack/echo/v5@5196b9b

Fields from package source
FieldTypeDescriptionSource
Rate float64 — L245
Burst int — L246
ExpiresIn time.Duration — L247
Functions from package source
TypeSource
func NewRateLimiterMemoryStore(rateLimit float64) (store *RateLimiterMemoryStore) L200
func NewRateLimiterMemoryStoreWithConfig(config RateLimiterMemoryStoreConfig) (store *RateLimiterMemoryStore) L225
// DefaultRateLimiterConfig defines default values for RateLimiterConfig.
var DefaultRateLimiterConfig = RateLimiterConfig{
Skipper: DefaultSkipper,
IdentifierExtractor: func(c *echo.Context) (string, error) {
id := c.RealIP()
return id, nil
},
ErrorHandler: func(c *echo.Context, err error) error {
return &echo.HTTPError{
Code: ErrExtractorError.Code,
Message: ErrExtractorError.Message,
Internal: err,
}
},
DenyHandler: func(c *echo.Context, identifier string, err error) error {
return &echo.HTTPError{
Code: ErrRateLimitExceeded.Code,
Message: ErrRateLimitExceeded.Message,
Internal: err,
}
},
}