-
Notifications
You must be signed in to change notification settings - Fork 61
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
Throttle on only a single route #1900
Comments
@Canoir it was your company's concern too, right? |
Okay, I think I see the request here. Instead of being forced to use the I think the In the As for the initial "How can I implement this?", so long as there's a config object passed to the |
I agree with you and about the name: config , I think that is great too, our need is exactly what you said, but your other answer make me a bigger question, what will happen if we pass ttl: 0 and limit 0 to global config? I mean if the guard only passes cause of 0 then there is a little bit shitty but solution to our problem even now, right? |
The idea I suggested was just to make sure that there was a default config so that the |
It took me an hour to realize that I need to have a default config block, even if I'm not using it. I'd say it should have a default if not specified. |
So, I'm not sure if I'm just doing something wrong but:
How would I mark only certain routes to be throttled then? |
@dominic-schmid currently it would be like this:
For all routes to be affected, there is another way to apply the ThrottlerGuard globally. Otherwise, it is only going to affect specific routes marked with the ThrottlerGuard. This issue is concerning step 1 - it would be better if there was a preconfigured "default" rather than needing to specify a dummy configuration if you are using custom limit/ttl for each route. |
Having same problem - I do use module with default configuration, however there are routes that I would like to use different rate limit that is defined in module. Currently I cannot do it without overriding default configurations... Any ideas how to workaround it? |
@jvvcn that's kind of the idea of what should be done. Is there something you'd rather be able to do? |
I was removed old messages isn't the solution Now I think I found the solution. 🧐However, you must set up throttling in ThrottlerModule.forRoot([{
ttl: 0,
limit: 0,
}]), And never forget to add this to {
provide: APP_GUARD,
useClass: ThrottlerGuard
}, And finally your import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
import { APP_GUARD } from '@nestjs/core';
@Module({
imports: [
ThrottlerModule.forRoot([{
ttl: 0,
limit: 0,
}]),
],
controllers: [
AppController,
],
providers: [
{
provide: APP_GUARD,
useClass: ThrottlerGuard
},
],
}) Then, the target route in your controller that you need will look like this. import { Controller, Get, Res } from '@nestjs/common';
import { Response } from 'express';
import { Throttle } from '@nestjs/throttler';
@Controller('animal')
export class AnimalController
{
@Throttle({ default: { limit: 3, ttl: (5 * 60 * 1000) } })
@Get("cats")
async GetCats( @Res() res: Response )
{
return res.status(200).json([ ...catList ])
}
} |
Is there an existing issue that is already proposing this?
Is your feature request related to a problem? Please describe it
My challenge is how to apply throttling to a specific route without setting up a global throttle. My concern arises from the necessity to log every single request from every single user (in memory) when establishing a global throttling system, to ascertain whether the new request necessitates throttling. How can I accomplish this?
Describe the solution you'd like
Globally config it like:
and use it like this on the route:
Notes:
Teachability, documentation, adoption, migration strategy
No response
What is the motivation / use case for changing the behavior?
For more customizations and more control for throttling and saving more space.
The text was updated successfully, but these errors were encountered: