Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you kill a redis client when there is no connection?

Tags:

node.js

redis

I have a valid server configuration in which redis can't be accessed, but the server can function correctly (I simply strip away features when redis can't be found).

However, I can't manage the connection errors well. I'd like to know when a connection error fails and shutdown the client in that case.

I've found that the connection retry will never stop. And quit() is actually swallowed - "Queueing quit for next server connection." - when called.

Is there a way to kill the client in the case where no connection can be established?

var redis = require("redis"),
    client = redis.createClient();

client.on("error", function(err) {
    logme.error("Bonk. The worker framework cannot connect to redis, which might be ok on a dev server!");
    logme.error("Resque error : "+err);
    client.quit();
});

client.on("idle", function(err) {
    logme.error("Redis queue is idle. Shutting down...");
});

client.on("end", function(err) {
    logme.error("Redis is shutting down. This might be ok if you chose not to run it in your dev environment");
});

client.on("ready", function(err) {
    logme.info("Redis up! Now connecting the worker queue client...");
});
  • ERROR - Resque error : Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
  • ERROR - Redis is shutting down. This might be ok if you chose not to run it in your dev environment
  • ERROR - Resque error : Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
  • ERROR - Resque error : Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
  • ERROR - Resque error : Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
  • ERROR - Resque error : Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED

One thing that is interesting is the fact that the 'end' event gets emitted. Why?

like image 649
Greg Avatar asked May 03 '12 02:05

Greg


People also ask

How do I kill Redis client?

Due to the single-threaded nature of Redis, it is not possible to kill a client connection while it is executing a command. From the client point of view, the connection can never be closed in the middle of the execution of a command.

Do I need to close Redis connection?

Continuously opening connections without closing is not a good practice. This will not only consume your resources but may also lead to program crash. The maximum number of file descriptors that you can open simultaneously is 1024.

Could not connect to Redis at Network is unreachable?

The most common reason for the connection refused error is that the Redis-Server is not started. Redis server should be started to use the Redis client after its installation. It can be done with the following command. Also to run Redis in the background, following command could be used.


2 Answers

The right way to have control on the client's reconnect behaviour is to use a retry_strategy.

Upon disconnection the redisClient will try to reconnect as per the default behaviour. The default behaviour can be overridden by providing a retry_strategy while creating the client.

Example usage of some fine grained control from the documentation.

var client = redis.createClient({
    retry_strategy: function (options) {
        if (options.error && options.error.code === 'ECONNREFUSED') {
            // End reconnecting on a specific error and flush all commands with
            // a individual error
            return new Error('The server refused the connection');
        }
        if (options.total_retry_time > 1000 * 60 * 60) {
            // End reconnecting after a specific timeout and flush all commands
            // with a individual error
            return new Error('Retry time exhausted');
        }
        if (options.attempt > 10) {
            // End reconnecting with built in error
            return undefined;
        }
        // reconnect after
        return Math.min(options.attempt * 100, 3000);
    }
});

Ref: https://www.npmjs.com/package/redis#options-object-properties

For the purpose of killing the client when the connection is lost, we could use the following retry_strategy.

var client = redis.createClient({
    retry_strategy: function (options) {
        return undefined;
    }
});
like image 185
Dhwani Katagade Avatar answered Sep 23 '22 06:09

Dhwani Katagade


You might want to just forcibly end the connection to redis on error with client.end() rather than using client.quit() which waits for the completion of all outstanding requests and then sends the QUIT command which as you know requires a working connection with redis to complete.

like image 43
Dan D. Avatar answered Sep 22 '22 06:09

Dan D.