chameleonbr / node-red-contrib-redis

Node RED client for Redis with pub/sub, list, lua scripting and other commands support.
MIT License
46 stars 40 forks source link

Always True State #18

Closed Maiquu closed 5 years ago

Maiquu commented 5 years ago

First if statement will always return true due to usingConn[idx] always pointing to a number. This results in opening a new connection for each Redis-out, Redis-cmd, Redis-lua node but not closing them all properly upon their deletion.

function connect(config, force) {
        var options = {};
        var idx = config.id;
        var config_env = _setEnv(config);

        if (force !== undefined || usingConn[idx] === undefined ||
            usingConn[idx] === 0 || usingConn[idx].connector === undefined) { 
            //usingConn[idx].connector is always undefined

            if (config_env.pass !== "") {
                options['password'] = config_env.pass;
            }
            if (config_env.dbase !== "") {
                options['db'] = config_env.dbase;
            }

            var conn = redis.createClient(config_env.port, config_env.host, options);
            conn.on('error', function(err) {
                console.error('[redis]', err);
            });
            if (force !== undefined && force === true) {
                return conn;
            }
            else {
                connection[idx] = conn;
                if (usingConn[idx] === undefined) {
                    usingConn[idx] = 1;
                }
                else {
                    usingConn[idx]++;
                }
            }
        }
        else {
            usingConn[idx]++;
        }
        return connection[idx];
    }
chameleonbr commented 5 years ago

I've changed connection logic, now NodeRED reuses the connection on same flow by default, otherwise if you set block connection, node create a new connection.