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

Multiple rate limits #30

Open
steveiliop56 opened this issue Oct 13, 2024 · 4 comments
Open

Multiple rate limits #30

steveiliop56 opened this issue Oct 13, 2024 · 4 comments

Comments

@steveiliop56
Copy link

Hello,

I am trying to make an app where I have 2 routes, /route1, /route2. I would like to rate limit them both with different limits/expirations. While this works by adding the rateLimiter, I can only get the expiration/limit data from the first rateLimiter instance. So, is there any way I can have multiple rate limiters running on one app and being able to get the rate limit status from each one? For example rateLimiter1.get, rateLimiter2.get.

@MathurAditya724
Copy link
Member

To be clear, have you placed 2 different rate limiters on these routes? Cause rateLimiter is just a middleware you can put it on a route basis too. You can check out the example here - #6

@steveiliop56
Copy link
Author

steveiliop56 commented Oct 14, 2024

Yeah I have created 2 rate limiters with middlewares and they work perfectly. The issue is with this part of the example code:

export const app = new Hono<{
  Variables: {
    rateLimit: RateLimitInfo;
    rateLimitStore: {
      get?: (key: string) => Promisify<RateLimitInfo | undefined>;
      resetKey: (key: string) => Promisify<void>;
    };
  };
}>();

Can I define 2 rate limits here? Basically I want to have an API route called /rate-limit that gets the rate limit statuses from both rate limits and returns them.

@MathurAditya724
Copy link
Member

For that you can just update the name of these properties using requestPropertyName and requestStorePropertyName, you can use these to define custom names to the other rate limiter.

@steveiliop56
Copy link
Author

Doesn't seem to be working, I have made a type like this:

type RateLimitStore = {
  get?: (key: string) => Promisify<RateLimitInfo | undefined>;
  resetKey: (key: string) => Promisify<void>;
};

export type HonoType = {
  Variables: {
    apiLimiter: RateLimitInfo;
    apiLimiterStore: RateLimitStore;
    subdomainLimiter: RateLimitInfo;
    subdomainLimiterStore: RateLimitStore;
  };
};

Configured hono:

const app = new Hono<HonoType>().basePath("/api");

And created my rate limiters:

app.use(
      "*",
      rateLimiter({
        windowMs: config.rateLimit.apiLimitExpiration * 60 * 1000,
        limit: config.rateLimit.apiLimit,
        standardHeaders: "draft-6",
        keyGenerator: () => "",
        message: { error: "Rate limit exceeded" },
        requestPropertyName: "apiLimiter",
        requestStorePropertyName: "apiLimiterStore",
      }),
);
app.use(
      "create",
      rateLimiter({
        windowMs: config.rateLimit.subdomainLimitExpiration * 60 * 1000,
        limit: config.rateLimit.subdomainLimit,
        standardHeaders: "draft-6",
        keyGenerator: () => "",
        message: { error: "Subdomain creation limit exceeded" },
        requestPropertyName: "subdomainLimiter",
        requestStorePropertyName: "subdomainLimiterStore",
      }),
);

Then I created this route for testing:

app.get("rate-limit", (c) => {
      const apiRateLimit = c.var;

      console.log(apiRateLimit);

      return c.json(
        {
          ok: true,
        },
        200,
      );
});

And when I make a request I see this:

{
  apiLimiter: {
    limit: 50,
    used: 1,
    remaining: 49,
    resetTime: 2024-10-15T15:10:16.261Z,
  },
  apiLimiterStore: {
    getKey: [AsyncFunction: get],
    resetKey: [AsyncFunction: resetKey],
  },
}

What am I doing wrong?

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

No branches or pull requests

2 participants