-
I'm migrating from Fiber to Echo and I can't figure out how Echo rate limiter middleware can limit reqests per minute. With this code, the limit is 5 requests per second. I need 5 per minute... package main
import (
"net/http"
"time"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"golang.org/x/time/rate"
)
var rateLimiter echo.MiddlewareFunc = middleware.RateLimiterWithConfig(middleware.RateLimiterConfig{
Skipper: middleware.DefaultSkipper,
Store: middleware.NewRateLimiterMemoryStoreWithConfig(
middleware.RateLimiterMemoryStoreConfig{Rate: rate.Limit(5), Burst: 0, ExpiresIn: time.Minute},
),
IdentifierExtractor: func(c echo.Context) (string, error) {
return c.RealIP(), nil
},
DenyHandler: func(c echo.Context, identifier string, err error) error {
return c.NoContent(http.StatusTeapot)
},
})
func main() {
e := echo.New()
e.Use(rateLimiter)
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.Logger.Fatal(e.Start(":1323"))
} |
Beta Was this translation helpful? Give feedback.
Answered by
aldas
Oct 20, 2024
Replies: 1 comment 2 replies
-
maybe middleware.RateLimiterMemoryStoreConfig{
Rate: rate.Every(5 * time.Minute / 5),
Burst: 5,
ExpiresIn: 5 * time.Minute,
}, But you should read https://pkg.go.dev/golang.org/x/[email protected]/rate#Limit how limiting works. |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
Firu115
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
maybe
But you should read https://pkg.go.dev/golang.org/x/[email protected]/rate#Limit how limiting works.