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

Silently ignore if connection is not available #761

Open
stanyq4 opened this issue Aug 31, 2022 · 1 comment
Open

Silently ignore if connection is not available #761

stanyq4 opened this issue Aug 31, 2022 · 1 comment

Comments

@stanyq4
Copy link

stanyq4 commented Aug 31, 2022

I am using ioredis with type-cacheable on AWS lambdas, and from time to time, the connection to Redis will time out.

Some of my APIs are time-sensitive, and if the connection to Redis fails - I wanna skip the cache and proceed to DynamoDB as soon as possible.

Here's how Redis client configuration:

const REDIS_CONNECTION_RETRY_COUNT: number = 1;
const redisOptions: RedisOptions = {
  host: config.redis.host || 'localhost',
  port: config.redis.port || 6379,
  retryStrategy: (times): number | undefined => {
    // reconnect after
    if (times > REDIS_CONNECTION_RETRY_COUNT) {
      return undefined;
    }
    return 50;
  },
  showFriendlyErrorStack: true,
  keepAlive: 10000,
  connectTimeout: 1500,
  maxRetriesPerRequest: REDIS_CONNECTION_RETRY_COUNT,
  noDelay: true,
  lazyConnect: false,
};

As you can see, I give 1.5 seconds for connection and retry once, and if that fails - I wanna move on.

I also don't want my logs to indicate errors, so I simply log them as info

redisClient.on('error', (err) => {
  if (err?.code === 'ETIMEDOUT') {
    logger.info('Redis connection timed out, retrying...');
  } else {
    logger.error(err);
  }
  // redisClient.disconnect(false);
});

Which all works great, except, inside ioredis-adapter, if the connection is not open by the time it tries to perform get - it will throw an error

(node:2991) UnhandledPromiseRejectionWarning: Error: Connection is closed.
    at IoRedisAdapter.<anonymous> (/Users/user/Dev/projects/events-streaming/node_modules/@type-cacheable/ioredis-adapter/dist/index.js:42:51)
    at Generator.next (<anonymous>)
    at /Users/user/Dev/projects/events-streaming/node_modules/@type-cacheable/ioredis-adapter/dist/index.js:8:71

Is there a way to catch this error or tell the adapter to skip if the connection is not there?

Thank you

@joshuaslate
Copy link
Owner

Hi,

I have been busy, but hope I can take a look into this soon. If you've got the time, I'd be happy to consider a PR from you for this feature.

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

2 participants