rhinobase / hono-rate-limiter

Rate Limit middleware for Hono Server
https://hono-rate-limiter.vercel.app
MIT License
251 stars 12 forks source link

[question] How does hono-rate-limiter work with many different configs? #5

Closed flipvh closed 6 months ago

flipvh commented 6 months ago

Hope its ok to ask a question. I am considering replacing our rate-limiter in our hono project with this one! I am hoping to get a solution that is less verbose as our current code: https://github.com/cellajs/cella/blob/1b4aaf59dc8d888f35e8d8aefc25df4dbae0e6e7/backend/src/middlewares/rate-limiter/index.ts

Suppose i ultimately want about 5 to 10 different rate-limiter configs, to handle specific scenarios in specific routes, what would that look like with hono-rate-limiter? Can I just pass different options to the same instance?

MathurAditya724 commented 6 months ago

The best way could be to define the common props like the store, keyGenerator, etc. Then you could just define the custom config for all the routes that you want to rate limit. You can also provide prefixes to chain multiple limiters together.

app.get(
  "/",
  rateLimiter({
    ...defaults,
    windowMs: 10_000, // 10 seconds
    limit: 10
  }),
  (c) => c.text("Base"),
);

app.get(
  "/another",
  rateLimiter({
    ...defaults,
    windowMs: 60_000, // 1 min
    limit: 30
  }),
  (c) => c.text("Another"),
);
flipvh commented 6 months ago

I like that! Thanks!