Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🤗 [Question]: Rate limit per path #2924

Closed
3 tasks done
kirankumar-grootan opened this issue Mar 18, 2024 · 5 comments
Closed
3 tasks done

🤗 [Question]: Rate limit per path #2924

kirankumar-grootan opened this issue Mar 18, 2024 · 5 comments

Comments

@kirankumar-grootan
Copy link

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.
Copy link

welcome bot commented Mar 18, 2024

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

@bigboiii300
Copy link

Is anyone know decision?

@sixcolors
Copy link
Member

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

https://docs.gofiber.io/guide/routing#middleware

@sixcolors
Copy link
Member

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")
}

@kirankumar-grootan
Copy link
Author

@sixcolors thanks I did the same way and it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants