|
| 1 | ++++ |
| 2 | +title = "Rate Limiter Middleware" |
| 3 | +description = "Rate limiter middleware for Echo" |
| 4 | +[menu.main] |
| 5 | + name = "Rate Limiter" |
| 6 | + parent = "middleware" |
| 7 | ++++ |
| 8 | + |
| 9 | +`RateLimiter` provides a Rate Limiter middleware for limiting the amount of requests to the server from a particular IP or id within a time period. |
| 10 | + |
| 11 | +By default an in-memory store is used for keeping track of requests. The default in-memory implementation is focused on correctness and |
| 12 | +may not be the best option for a high number of concurrent requests or a large number of different identifiers (>16k). |
| 13 | + |
| 14 | +### Usage |
| 15 | + |
| 16 | +To add a rate limit to your application simply add the `RateLimiter` middlware. |
| 17 | +The example below will limit the application to 20 requests/sec using the default in-memory store: |
| 18 | + |
| 19 | +```go |
| 20 | +e.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(20))) |
| 21 | +``` |
| 22 | + |
| 23 | +## Custom Configuration |
| 24 | + |
| 25 | +```go |
| 26 | +config := middleware.RateLimiterConfig{ |
| 27 | + Skipper: DefaultSkipper, |
| 28 | + Store: middleware.NewRateLimiterMemoryStore( |
| 29 | + middleware.RateLimiterMemoryStoreConfig{Rate: 10, Burst: 30, ExpiresIn: 3 * time.Minute} |
| 30 | + ) |
| 31 | + IdentifierExtractor: func(ctx echo.Context) (string, error) { |
| 32 | + id := ctx.RealIP() |
| 33 | + return id, nil |
| 34 | + }, |
| 35 | + ErrorHandler: func(context echo.Context, err error) error { |
| 36 | + return context.JSON(http.StatusTooManyRequests, nil) |
| 37 | + }, |
| 38 | + DenyHandler: func(context echo.Context, identifier string) error { |
| 39 | + return context.JSON(http.StatusForbidden, nil) |
| 40 | + }, |
| 41 | +} |
| 42 | + |
| 43 | +e.Use(middleware.RateLimiterWithConfig(config)) |
| 44 | +``` |
| 45 | + |
| 46 | +Note: If you need to implement your own store, be sure to implement the RateLimiterStore interface and pass it to RateLimiterConfig and you're good to go! |
| 47 | + |
| 48 | +## Configuration |
| 49 | + |
| 50 | +```go |
| 51 | +type RateLimiterConfig struct { |
| 52 | + Skipper Skipper |
| 53 | + BeforeFunc BeforeFunc |
| 54 | + // IdentifierExtractor uses echo.Context to extract the identifier for a visitor |
| 55 | + IdentifierExtractor Extractor |
| 56 | + // Store defines a store for the rate limiter |
| 57 | + Store RateLimiterStore |
| 58 | + // ErrorHandler provides a handler to be called when IdentifierExtractor returns a non-nil error |
| 59 | + ErrorHandler func(context echo.Context, err error) error |
| 60 | + // DenyHandler provides a handler to be called when RateLimiter denies access |
| 61 | + DenyHandler func(context echo.Context, identifier string, err error) error |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### Default Configuration |
| 66 | + |
| 67 | +```go |
| 68 | +// DefaultRateLimiterConfig defines default values for RateLimiterConfig |
| 69 | +var DefaultRateLimiterConfig = RateLimiterConfig{ |
| 70 | + Skipper: DefaultSkipper, |
| 71 | + IdentifierExtractor: func(ctx echo.Context) (string, error) { |
| 72 | + id := ctx.RealIP() |
| 73 | + return id, nil |
| 74 | + }, |
| 75 | + ErrorHandler: func(context echo.Context, err error) error { |
| 76 | + return &echo.HTTPError{ |
| 77 | + Code: ErrExtractorError.Code, |
| 78 | + Message: ErrExtractorError.Message, |
| 79 | + Internal: err, |
| 80 | + } |
| 81 | + }, |
| 82 | + DenyHandler: func(context echo.Context, identifier string, err error) error { |
| 83 | + return &echo.HTTPError{ |
| 84 | + Code: ErrRateLimitExceeded.Code, |
| 85 | + Message: ErrRateLimitExceeded.Message, |
| 86 | + Internal: err, |
| 87 | + } |
| 88 | + }, |
| 89 | +} |
| 90 | +``` |
| 91 | +### Errors |
| 92 | + |
| 93 | +```go |
| 94 | +var ( |
| 95 | + // ErrRateLimitExceeded denotes an error raised when rate limit is exceeded |
| 96 | + ErrRateLimitExceeded = echo.NewHTTPError(http.StatusTooManyRequests, "rate limit exceeded") |
| 97 | + // ErrExtractorError denotes an error raised when extractor function is unsuccessful |
| 98 | + ErrExtractorError = echo.NewHTTPError(http.StatusForbidden, "error while extracting identifier") |
| 99 | +) |
| 100 | +``` |
0 commit comments