-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
Comments
To be clear, have you placed 2 different rate limiters on these routes? Cause |
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 |
For that you can just update the name of these properties using |
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? |
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 therateLimiter
, I can only get the expiration/limit data from the firstrateLimiter
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 examplerateLimiter1.get
,rateLimiter2.get
.The text was updated successfully, but these errors were encountered: