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

Redis storage does is not working - no errors #1140

Closed
twigs67 opened this issue Feb 10, 2023 · 3 comments
Closed

Redis storage does is not working - no errors #1140

twigs67 opened this issue Feb 10, 2023 · 3 comments

Comments

@twigs67
Copy link

twigs67 commented Feb 10, 2023

Rate limiter does not seem to work when using Redis. However, no errors are being thrown to indicate that the connection is unsuccessful.

   ThrottlerModule.forRoot({
      ttl: 60,
      limit: 100,
      storage: new ThrottlerStorageRedisService(
        new Redis({
          port: Number(process.env.REDIS_PORT),
          host: process.env.REDIS_HOST,
        }),
      ),
    }),

Packages:

 "ioredis": "^5.0.2",
"nestjs-throttler-storage-redis": "^0.3.0",
"redis": "^4.0.6",
 "@types/ioredis": "^4.28.10",

Just seeing if anyone has any ideas?

@dmolim
Copy link

dmolim commented Mar 4, 2023

Hi @twigs67, I've just installed and configured the module and it successfully works with the following packages:

"ioredis": "^5.3.1",
"nestjs-throttler-storage-redis": "^0.3.0",
"redis": "3",
"@types/ioredis": "^5.0.0",

Here's my storage configuration:

storage: new ThrottlerStorageRedisService(
  new Redis({
    host: configService.get<string>('REDIS_HOST'),
    port: +configService.get<string>('REDIS_PORT'),
    password: configService.get<string>('REDIS_PASSWORD'),
    db: +configService.get<string>('REDIS_THROTTLER_DB'),
  }),
),

@kkoomen
Copy link
Owner

kkoomen commented Mar 6, 2023

@twigs67 You can check if rate limiter is working by using redis-cli monitor in your terminal after you started redis and then you should see an EVAL, INCR, PTTL and PEXIRE being executed:

Shell 1: redis-server start redis server
Shell 2: yarn start:dev start your project
Shell 3: redis-cli monitor start redis monitoring

Then in shell 4 you can run curl localhost:3000 (assuming your application runs on this address) and then in shell 3 you will see:

1678096891.755335 [0 127.0.0.1:55191] "EVAL" "local totalHits = redis.call(\"INCR\", KEYS[1])\nlocal timeToExpire = redis.call(\"PTTL\", KEYS[1])\nif timeToExpire <= 0\nthen\nredis.call(\"PEXPIRE\", KEYS[1], tonumber(ARGV[1]))\ntimeToExpire = tonumber(ARGV[1])\nend\nreturn { totalHits, timeToExpire }" "1" "8b40547db3793cee1110528738884f6a" "10000"
1678096891.755417 [0 lua] "INCR" "8b40547db3793cee1110528738884f6a"
1678096891.755434 [0 lua] "PTTL" "8b40547db3793cee1110528738884f6a"
1678096891.755443 [0 lua] "PEXPIRE" "8b40547db3793cee1110528738884f6a" "10000"

If you see something to this output, you know the throttler works.

@kkoomen
Copy link
Owner

kkoomen commented May 21, 2023

Closing due to inactivity.

@kkoomen kkoomen closed this as completed May 21, 2023
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