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

aws elesttc redis cluster connection error with global variable #1853

Open
abuzarhamza opened this issue Jan 30, 2024 · 0 comments
Open

aws elesttc redis cluster connection error with global variable #1853

abuzarhamza opened this issue Jan 30, 2024 · 0 comments

Comments

@abuzarhamza
Copy link

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

@abuzarhamza abuzarhamza changed the title aws elesttic redis clustor connection error with global variable aws elesttc redis cluster connection error with global variable Jan 30, 2024
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