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

Error: value is not an integer. When used with redis driver #27

Open
izidorio opened this issue Sep 26, 2020 · 4 comments
Open

Error: value is not an integer. When used with redis driver #27

izidorio opened this issue Sep 26, 2020 · 4 comments

Comments

@izidorio
Copy link

When I select the redis drive the following error is returned

warning: 
  WARNING: Adonis has detected an unhandled promise rejection, which may
  cause undesired behavior in production.
  To stop this warning, use catch() on promises or wrap await
  calls inside try/catch.

ReplyError: ERR value is not an integer or out of range
    at JavascriptRedisParser.returnError (/Users/Bento/Projetos/dsci/server/node_modules/ioredis/lib/redis/parser.js:24:25)
    at JavascriptRedisParser.execute (/Users/Bento/Projetos/dsci/server/node_modules/redis-parser/lib/parser.js:572:12)
    at Socket.<anonymous> (/Users/Bento/Projetos/dsci/server/node_modules/ioredis/lib/redis/event_handler.js:107:22)
    at Socket.emit (events.js:210:5)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:290:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:182:23)
From previous event:
    at Command.initPromise (/Users/Bento/Projetos/dsci/server/node_modules/ioredis/lib/command.js:63:18)
    at new Command (/Users/Bento/Projetos/dsci/server/node_modules/ioredis/lib/command.js:51:8)
    at Redis.expire (/Users/Bento/Projetos/dsci/server/node_modules/ioredis/lib/commander.js:131:29)
    at Redis.incrementExpiration (/Users/Bento/Projetos/dsci/server/node_modules/adonis-throttle/src/Drivers/Cache/Redis/index.js:88:16)
    at Throttle.incrementExpiration (/Users/Bento/Projetos/dsci/server/node_modules/adonis-throttle/src/Throttle.js:58:20)
    at ThrottleRequests.handle (/Users/Bento/Projetos/dsci/server/node_modules/adonis-throttle/middleware/ThrottleRequests.js:40:27)
    at MiddlewareBase._resolveMiddleware (/Users/Bento/Projetos/dsci/server/node_modules/@adonisjs/middleware-base/index.js:195:28)
    at Runnable._invoke (/Users/Bento/Projetos/dsci/server/node_modules/co-compose/src/Runnable.js:76:42)
    at /Users/Bento/Projetos/dsci/server/node_modules/co-compose/src/Runnable.js:73:34
    at f (/Users/Bento/Projetos/dsci/server/node_modules/once/once.js:25:25)
    at GuardInit.handle (/Users/Bento/Projetos/dsci/server/node_modules/adonis-guard/src/Middleware/GuardInit.js:28:11)
    at MiddlewareBase._resolveMiddleware (/Users/Bento/Projetos/dsci/server/node_modules/@adonisjs/middleware-base/index.js:195:28)
    at Runnable._invoke (/Users/Bento/Projetos/dsci/server/node_modules/co-compose/src/Runnable.js:76:42)
    at /Users/Bento/Projetos/dsci/server/node_modules/co-compose/src/Runnable.js:73:34
    at f (/Users/Bento/Projetos/dsci/server/node_modules/once/once.js:25:25)
    at AuthInit.handle (/Users/Bento/Projetos/dsci/server/node_modules/@adonisjs/auth/src/Middleware/AuthInit.js:60:11)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Init.handle (/Users/Bento/Projetos/dsci/server/node_modules/adonis-acl/src/Middlewares/Init.js:39:5)
    at async BodyParser.handle (/Users/Bento/Projetos/dsci/server/node_modules/@adonisjs/bodyparser/src/BodyParser/index.js:242:7) {
  name: 'ReplyError',
  message: 'ERR value is not an integer or out of range',
  command: {
    name: 'expire',
    args: [ 'throttle_123215303dce59e556bd5889e9ec5fa2c18895c1', '' ]
  }
}

config/throttle.js

   ...
  driver: Env.get('THROTTLE_DRIVER', 'redis'),

  redis: {
    host: '127.0.0.1',
    port: 6379,
    password: null,
    db: 0,
    keyPrefix: 'throttle_',
  },

Looking at the value of the key directly in the redis I see the value being written

Screen Shot 2020-09-26 at 08 27 25

I'm using
adonis-version: 4.1.0
node: v12.14.0
adonis-throttle: ^2.3.6

Link my boilerplate https://github.com/izidorio/adonis-boilerplate

@allenhark
Copy link

Same issue, any patch?

@ruanitto
Copy link

Edit Redis driver, line 54:

  get(key) {
    // return this.redis.get(key)
    this.redis.get(key, (err, result) => {
      if (err) {
        throw err
      } else {
        return result
      }
    })
  }

seanc added a commit to seanc/adonis-throttle that referenced this issue Nov 16, 2020
@izidorio
Copy link
Author

Thanks @ruanitto @masasron It worked but with the value returned it is a promise the result returned is always undefined
Now is need to close the issue #18

@izidorio
Copy link
Author

izidorio commented Dec 26, 2020

without a solution I started using node-rate-limiter-flexible with @adonisjs/redis

#midlleware

'use strict';

const Redis = use('Redis'); 
const { RateLimiterRedis } = require('rate-limiter-flexible');

class RateLimiter {
 async handle({ request, response }, next, [points = 5, duration = 1]) {
    const rateLimiterRedis = new RateLimiterRedis({
      storeClient: Redis,
      keyPrefix: 'limiter',
      points,
      duration,
    });

    await rateLimiterRedis.consume(`${request.method()}|${request.url()}|${request.ip()}`)
      .then(async (rateLimiterRes) => {
        response.header('X-RateLimit-Remaining', rateLimiterRes);
        await next();
      })
      .catch((rejRes) => {
        if (rejRes instanceof Error) {
          return response.status(500).json({ message: 'Error internal Rate Limiter', error: rejRes });
        }
        const secs = Math.round(rejRes.msBeforeNext / 1000) || 1;

        response.header('Retry-After', String(secs));
        response.status(429).json({ message: 'Stop! Too Many Requests' });
      });
  }
}

module.exports = RateLimiter;

#start/kernel.js

const namedMiddleware = {
  ...
  rate: 'App/Middleware/RateLimiter',
};

#routes

  Route.get('/:file', 'DocumentController.show')
    .middleware('rate:5,1');

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