redis / ioredis

🚀 A robust, performance-focused, and full-featured Redis client for Node.js.
MIT License
14.06k stars 1.19k forks source link

aws elesttc redis cluster connection error with global variable #1853

Open abuzarhamza opened 5 months ago

abuzarhamza commented 5 months ago

Hi All

I am getting the following error , when we are truing to get connection Error: Redis is already connecting/connected

here is redisio package node env

ioredis: "5.2.3"
node : v18.12.0

Here is our client

//sample.js
const rc = (callback) => {

    let rc = null,
    r_url = 'xxxx';

    if (global.rc) {
        rc = global.rc;
        return callback(null, rc);
    } else {
       (

        async (callback) => {
            if (!r_url) {
                console.log('redisURL not found');
                return callback();
            }
            // Create a Redis instance. 
            try {
                const [host, port] = redisURL.split(':');
                rc = new Redis.Cluster([
                    {
                        host,
                        port
                    }
                ], {
                    scaleReads: 'all',
                    dnsLookup: (address, callback) => callback(null, address),
                    commandTimeout: 3000, // all timestamps used here are in milliseconds
                    connectTimeout: 5000,
                    disconnectTimeout: 2000,
                    maxRetriesPerRequest: 2,
                    enableOfflineQueue: true,
                    showFriendlyErrorStack: true,
                    autoResendUnfulfilledCommands: true, // If true, client will resend unfulfilled commands(e.g. block commands) in the previous connection when reconnected. defaults to true
                    lazyConnect: false,
                    clusterRetryStrategy(times) {
                        if (times % 2 === 0) {
                            console.error('redisRetryError : Redis reconnect exhausted after 3 retries.');
                            return null;
                        }
                        return 200;
                    },
                    reconnectOnError(err) {
                        const targetError = 'READONLY';
                        if (err.message.includes(targetError)) {
                            // Only reconnect when the error contains "READONLY"
                            return true; // or `return 1;`
                        }
                    },
                });
                await rc.connect();
                if (rc && rc.status === 'ready') {
                    global.rc = rc;
                    console.log('[redisClient] : redis connection ready');
                } else {
                    global.rc = null;
                }
                callback(null, rc);
            } catch(err) {
                console.log(err)
                callback(err);
            }

        }
       )()
    }
};

Now ehn I make Call to redisClient

//CALLER
//GETTING the CLIENT
rc((e, r_conn)=> {
     if (r_conn && r_conn.status === 'ready') {
      //..... Do something like get or set
     } else {
        console.error(e);
     }
})

here we the above we are seeing the above mentioned error? Any suggestion what we are doing wrong here