🤗 [Question]: Rate limit per path
kirankumar-grootan opened this issue · comments
Question Description
is it possible to add rate limiting functionality per path e.g. have different rate limiting logic for each path below?
app.Get("/auth", handler.RedirectHandler) - rate limit by 10 for 1 min
app.Get("/auth/", handler.RedirectHandler) - rate limit by 5 for 1 min
app.Get("/auth/realms", handler.RedirectHandler) - rate limit by 25 for 1 min
Code Snippet (optional)
No response
Checklist:
- I agree to follow Fiber's Code of Conduct.
- I have checked for existing issues that describe my questions prior to opening this one.
- I understand that improperly formatted questions may be closed without explanation.
Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord
Is anyone know decision?
Yes. You can use a new rate limiter middleware handler for each route.
Either set a route on app.Use
with the middleware handler (works with groups too), or apply the handler in the app.Get
Here is an example with app.Use
package main
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/limiter"
)
func main() {
app := fiber.New()
// …
// Custom rate limits for specific paths
app.Use("/auth", limiter.New(limiter.Config{
Max: 10, // Requests per minute for /auth
Expiration: 1 * time.Minute,
}))
app.Use("/auth/", limiter.New(limiter.Config{
Max: 5, // Requests per minute for /auth/
Expiration: 1 * time.Minute,
}))
app.Use("/auth/realms", limiter.New(limiter.Config{
Max: 25, // Requests per minute for /auth/realms
Expiration: 1 * time.Minute,
}))
// Add your routes here
app.Get("/auth", handler.RedirectHandler)
app.Get("/auth/", handler.RedirectHandler)
app.Get("/auth/realms", handler.RedirectHandler)
// …
// Start the server
app.Listen(":3000")
}
@sixcolors thanks I did the same way and it works