javascript - How to detect client disconnect from redis in nodejs? - Stack Overflow

I am using the library node_redis as the client for a micro-service message client that I am writing. C

I am using the library node_redis as the client for a micro-service message client that I am writing. Clients get messages from their application in their outbox that they need to send to other services. Everything is working great but I am trying to build some resilience on the part of the application that uses the redis client to municate with the redis-server.

My idea is that the redis client-server connection status should be highly available to the clients. I mean that if a connection goes down, I would like to know within the second instead of the default timeout of 300 seconds. Currently I am using the free redislabs tier hosted on AWS, but I should be moving this to run in its own container on my kubernetes cluster.

I need to know the state of client connections in the network because I would like to not send messages when the network conditions are not right and not rely on the error handling to handle this sort of event. Knowing how often and when these high latency events occur will also help me diagnose and improve my network and my microservices.

Note: I wanted to set the connect_timeout value in the client options but this is listed as deprecated.

I am using the library node_redis as the client for a micro-service message client that I am writing. Clients get messages from their application in their outbox that they need to send to other services. Everything is working great but I am trying to build some resilience on the part of the application that uses the redis client to municate with the redis-server.

My idea is that the redis client-server connection status should be highly available to the clients. I mean that if a connection goes down, I would like to know within the second instead of the default timeout of 300 seconds. Currently I am using the free redislabs tier hosted on AWS, but I should be moving this to run in its own container on my kubernetes cluster.

I need to know the state of client connections in the network because I would like to not send messages when the network conditions are not right and not rely on the error handling to handle this sort of event. Knowing how often and when these high latency events occur will also help me diagnose and improve my network and my microservices.

Note: I wanted to set the connect_timeout value in the client options but this is listed as deprecated.

Share Improve this question asked Nov 12, 2017 at 20:10 tensaitensai 1,6963 gold badges18 silver badges23 bronze badges 2
  • Couldn't you use the "retry_strategy" option? There is also an event (reconnecting) being fired when the library tries to reconnect to the redis-server. To be fair this event isn't what you're looking for since it is emitted when the library tries to reconnect and not when the connection has been lost but you could use it together with a custom retry strategy. – Siggy Commented Dec 16, 2017 at 1:47
  • How did you end up dealing with this? – Ed Spencer Commented Jun 21, 2018 at 13:51
Add a ment  | 

1 Answer 1

Reset to default 3

Something like that?

var redis = require('redis');
var client = redis.createClient();
var reconnectAfter = 15000;

client.on( 'error', function () {
  console.log( (new Date()) + " Redis: disconnect");
  setTimeout( connect, reconnectAfter);
});

connect = function(){
  client = redis.createClient();
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745293126a4620981.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信