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

serious bug: remaining times is stuck when use "storage" option with nestjs-redis #416

Closed
liaoliaots opened this issue Mar 24, 2021 · 2 comments

Comments

@liaoliaots
Copy link

liaoliaots commented Mar 24, 2021

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request

Current behavior

export const Throttler: DynamicModule = ThrottlerModule.forRootAsync({
  imports: [Redis],
  inject: [RedisService],
  useFactory(redisService: RedisService): ThrottlerModuleOptions {
    const redisClient = redisService.getClient(REDIS_CLIENT_NAME_THROTTLER);

    return {
      ttl: ms('1m') / 1000,
      limit: 30,
      storage: new ThrottlerStorageRedisService(redisClient),
    };
  },
});

Expected behavior

The throttler should works normally.

Minimal reproduction of the problem with instructions

  1. download from https://github.com/DevAngsy/throttle-bug
  2. npm install
  3. edit redis password according to your redis config
  4. npm run start:dev
  5. send get request to http://127.0.0.1:3000/app/hello 10-20 times, and notice X-RateLimit-Remaining
  6. single-line annotate "storage" option, notice X-RateLimit-Remaining again

Environment

OS Version: Ubuntu 20.04.2 LTS
CPU: Intel® Core™ i5-9600K
NodeJS Version: 14.16.0
NPM Version: 7.6.3
Ubuntu Redis Version: V6
Global Nest CLI Version: 7.5.6
Repo Nest CLI Version: 7.5.6
Repo nestjs-throttler Version: 0.3.0
Repo nestjs-throttler-storage-redis Version: 0.1.11
Repo ioredis Version: 4.24.3
@jmcdo29
Copy link
Member

jmcdo29 commented Mar 24, 2021

Without using the RedisStorage, the module throws an exception after 30 requests, as you've set.

I used this zsh script to test it

throttlerTest() {
  dummy=count
  count=0
  while true
  do
    curl -s --fail http://localhost:3000/app/hello
    if [ $? -ne 0 ]
    then
      echo "We got rate limited"
      curl -v http://localhost:3000/app/hello
      break
    fi
    (($dummy += 1))
  done
  echo "Requests made before limiting:"
  echo $count
}

And got back

❯ throttlerTest
Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!We got rate limited
*   Trying ::1:3000...
* TCP_NODELAY set
* Connected to localhost (::1) port 3000 (#0)
> GET /app/hello HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 429 Too Many Requests
< X-Powered-By: Express
< Retry-After: 60
< Content-Type: application/json; charset=utf-8
< Content-Length: 68
< ETag: W/"44-McWaoNlzt3iglN2odEGCDEADErE"
< Date: Wed, 24 Mar 2021 04:05:14 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
<
* Connection #0 to host localhost left intact
{"statusCode":429,"message":"ThrottlerException: Too Many Requests"}Requests made before limiting:
30

So like you said, it works normally, meaning there's a problem with how the sub-library works and returns the ttls that are used in this line for the header calculation

@kkoomen
Copy link
Collaborator

kkoomen commented Mar 26, 2021

Fixed in kkoomen/nestjs-throttler-storage-redis v0.1.12.

@kkoomen kkoomen closed this as completed Mar 26, 2021
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

3 participants