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

Cluster client couldn't reconnect because retryStrategy is not set, but the TS types state that's not allowed #1749

Open
javiergonzalezGenially opened this issue Apr 21, 2023 · 2 comments

Comments

@javiergonzalezGenially
Copy link

javiergonzalezGenially commented Apr 21, 2023

I have a cluster client configured like this:

export function createRedisClusterClient(redisUrls: string[]) {
  return new Cluster(redisUrls, {
    scaleReads: 'master', // writes and reads go to master nodes

    slotsRefreshTimeout: 6000,

    clusterRetryStrategy(times: number) {
      return Math.min(times * 50, 2000);
    },

    redisOptions: {
      maxRetriesPerRequest: null, // retry forever until connection is open again

      reconnectOnError(err) {
        // elastic cache might return READONLY when a replica node is manually promoted to primary
        // in that case we want to reconnect
        const targetError = 'READONLY';
        if (err.message.includes(targetError)) {
          return 2; // reconnect to the master and resend failed command
        }
        return false; // default, do not reconnect
      },
    },
  });
}

note how in redisOptions there's no "retryStrategy" since there's a clusterRetryStrategy and also the TS types say it is not allowed:

redisOptions?: Omit<RedisOptions, "port" | "host" | "path" | "sentinels" | "retryStrategy" | "enableOfflineQueue" | "readOnly">;

But recently I found it couldn't reconnect with this stack trace:

at TCP.callbackTrampoline (node:internal/async_hooks:130:17) |  
-- | -- | --
at TCP.<anonymous> (node:net:687:12) |  
at Socket.emit (node:domain:475:12) |  
at Socket.emit (node:events:390:28) |  
at Object.onceWrapper (node:events:510:26) |  
at Socket.<anonymous> (xxx/node_modules/ioredis/built/redis/event_handler.js:160:20) |  
at close (xxx/node_modules/ioredis/built/redis/event_handler.js:189:14) |  
at EventEmitter.flushQueue (xxx/node_modules/ioredis/built/Redis.js:614:30) |  
at Command.command.reject (xxx/node_modules/ioredis/built/cluster/index.js:362:23) |  
at EventEmitter.handleError (xxx/node_modules/ioredis/built/cluster/index.js:508:38) |  
Error: Too many Cluster redirections. Last error: Error: Connection is closed.

particularly of interest is this line

at Socket.<anonymous> (xxx/node_modules/ioredis/built/redis/event_handler.js:160:20) |  

        if (typeof self.options.retryStrategy !== "function") {
            debug("skip reconnecting because `retryStrategy` is not a function");
            return close();
        }

so my question is, are the types wrong (and therefore retryStrategy should be inside the cluster client options) or is the code wrong and it should have re-tried as clusterRetryStrategy mandates?

Also it confuses me that the docs state

By default, ioredis will try to reconnect when the connection to Redis is lost except when the connection is closed manually by redis.disconnect() or redis.quit().

but that piece of code makes it look like it won't unless that function is there?

@javiergonzalezGenially
Copy link
Author

Also the socket close handler seems to be leaking

(node:6) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added to [Socket]. Use emitter.setMaxListeners() to increase limit

since that also happened in that disconnection, but I guess that might be a totally different issue

@javiergonzalezGenially javiergonzalezGenially changed the title The redis cluster couldn't reconnect because retryStrategy is not set, but the TS types says that's not allowed The redis cluster couldn't reconnect because retryStrategy is not set, but the TS types state that's not allowed Apr 21, 2023
@javiergonzalezGenially javiergonzalezGenially changed the title The redis cluster couldn't reconnect because retryStrategy is not set, but the TS types state that's not allowed Cluster client couldn't reconnect because retryStrategy is not set, but the TS types state that's not allowed Apr 21, 2023
@javiergonzalezGenially
Copy link
Author

After looking more deeply at ioRedis code my hunch is that it couldn't reconnect on the timeout given by
retryDelayOnFailover (100) * maxRedirections (16) which is just 1600ms (which seems quite low)

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

1 participant