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

Nan if register module with default and use Throttle decorator #1089

Closed
2 of 4 tasks
kanopa opened this issue Jul 29, 2022 · 10 comments · Fixed by #1565
Closed
2 of 4 tasks

Nan if register module with default and use Throttle decorator #1089

kanopa opened this issue Jul 29, 2022 · 10 comments · Fixed by #1565
Labels
bug Something isn't working

Comments

@kanopa
Copy link

kanopa commented Jul 29, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When ThrottlerModule.forRoot() is registered with no parameters and @Throttle decorator is set on one of the methods and try to call method where decorator is not set we get Nan in ttlMilliseconds when in storageService trying to add key
const ttlMilliseconds = ttl * 1000;
In memory realisation is we have [Nan]
In https://github.com/kkoomen/nestjs-throttler-storage-redis realisation we have exception

Minimum reproduction code

later

Steps to reproduce

No response

Expected behavior

Ttl must not be Nan, maybe return earlier when ttl is not set or add default value. And need to fix typing from
handleRequest( context: ExecutionContext, limit: number, ttl: number, )

to
handleRequest( context: ExecutionContext, limit?: number, ttl?: number, )

Package version

8.1.3

NestJS version

8.1.3

Node.js version

16.13.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@kanopa kanopa added the bug Something isn't working label Jul 29, 2022
@jmcdo29
Copy link
Member

jmcdo29 commented Jul 29, 2022

Please provide a minimum reproduction repository (Git repository/StackBlitz/CodeSandbox project).

@kanopa
Copy link
Author

kanopa commented Jul 30, 2022

Please provide a minimum reproduction repository (Git repository/StackBlitz/CodeSandbox project).

sure
https://github.com/kanopa/throttler-issue

issue reproduce on default route http://localhost:3000/
image
i think we need add more stronger typing ttl and limit on handle request, because they could be undefined

If use throttler module without redis error not throwing, but in memory storage saving Nan date
image

@jmcdo29
Copy link
Member

jmcdo29 commented Jul 30, 2022

i think we need add more stronger typing ttl and limit on handle request, because they could be undefined

Either that or set defaults that we know will be there. Defaults will be more sensible at the moment as to not need a breaking change.

20 hits per minute is the default of the @Throttle() decorator, should we set the default to that?

If use throttler module without redis error not throwing, but in memory storage saving Nan date

for the redis plugin, please bring that issue up with the package owner. This package is for the main logic and memory storage integration

@kanopa
Copy link
Author

kanopa commented Jul 31, 2022

i think we need add more stronger typing ttl and limit on handle request, because they could be undefined

Either that or set defaults that we know will be there. Defaults will be more sensible at the moment as to not need a breaking change.

20 hits per minute is the default of the @Throttle() decorator, should we set the default to that?

If use throttler module without redis error not throwing, but in memory storage saving Nan date

for the redis plugin, please bring that issue up with the package owner. This package is for the main logic and memory storage integration

Defaults is good but someone may use logic where he set only limit param and if we set defaults its will be breaking change for him.
I think there are two solutions

  1. Add defaults
  2. Typing handleRequest to (context: ExecutionContext, limit?: number, ttl?: number) and handle ttl on in memory storage, maybe set infinity instead of NaN or not store values without ttl

Depending on the solution in this package, I will either create a pull request on the redis package or we will solve the problem here :)

@jmcdo29
Copy link
Member

jmcdo29 commented Jul 31, 2022

I'd like to not have to worry about the handleRequest method, so we'd ensure that there are defaults set by that time. Just want to make sure that what we set are sensible. I know how to handle ensuring they are set and that we don't override though, so that's not an issue

@joZephhh
Copy link

Why not having early return as we had previously ?? (see 2c4b2ff), we just need to put it right after the limit and ttl resolution regarding context :

   // Check if specific limits are set at class or route level, otherwise use global options.
    const limit = routeOrClassLimit || this.options.limit;
    const ttl = routeOrClassTtl || this.options.ttl;

     if (typeof limit === 'undefined' || typeof ttl === 'undefined') {
      return true;
    } 

    return this.handleRequest(context, limit, ttl);

https://github.com/nestjs/throttler/blob/ed3c2e354dee3b40f1b8fa09ac6a3c5685ad7b91/src/throttler.guard.ts#LL50C2-L53C52

wdyt ?

@jmcdo29
Copy link
Member

jmcdo29 commented May 16, 2023

I'll need to find when we removed that and why, but it seems reasonable

@mehdibo
Copy link

mehdibo commented Jun 2, 2023

I am also having this issue, setting a default ttl and limit solves the problem.

I am adding this so people like don't waste a lot of time finding out why this error shows up:

[Nest] 562190  - 06/02/2023, 2:17:47 AM   ERROR [ExceptionsHandler] @ntegral/nestjs-sentry:  ERR Error running script (call to f_f7788d43191d9ad66c866e8c63f3c3e361391849): @user_script:5: ERR value is not an integer or out of range
ReplyError: ERR Error running script (call to f_f7788d43191d9ad66c866e8c63f3c3e361391849): @user_script:5: ERR value is not an integer or out of range
    at parseError (/srv/api/node_modules/redis-parser/lib/parser.js:179:12)
    at parseType (/srv/api/node_modules/redis-parser/lib/parser.js:302:14)

@jmcdo29
Copy link
Member

jmcdo29 commented Jul 6, 2023

Will be fixed in 5.0.0 with #1565

@jmcdo29 jmcdo29 linked a pull request Jul 6, 2023 that will close this issue
12 tasks
@jmcdo29 jmcdo29 mentioned this issue Jul 6, 2023
12 tasks
@PatrickOtero
Copy link

In my case, nest says that "ttl" does not exists anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants