how hiredis clients know server down ?

492 views
Skip to first unread message

pad...@gmail.com

unread,
Dec 7, 2014, 3:18:33 AM12/7/14
to redi...@googlegroups.com
Hi.

I need to know a way for hiredis clients to detect server down during command session for clients' safety.


I inserted sleep(30) in the middle of command session of hiredis/example.c. While the client is sleeping,
I stopped the redis-server to which the client has connection. After the client woke up, it crashes. Here's
output to stdout.

  $ ./hiredis-example
  PING: PONG
  SET: OK
  SET (binary API): OK
  GET foo: hello world
  INCR counter: 5
  INCR counter: 6
  Segmentation fault (core dumped)

Segmentation fault cannot be acceptable for a server process. It may be better to interleave connect and
disconnect for every redis command session but this is not perfect.

Let me know a programming pattern or such example of a redis client (by hiredis) to keep working (not to die)
by redis-server down (or error) during client's command session.

Thanks in advance.



paulinohuerta

unread,
Dec 7, 2014, 9:19:12 AM12/7/14
to redi...@googlegroups.com
Hi,

The err field inside the context will be non-zero, when a  function call is not successful.
   
 //example:

 //if exists the definition:    redisContext *c;
 //and it allocate a redis context

 //then,
 for(j=1;j<=10;j++) {
       reply = redisCommand(c,"PING");
       if(c->err == 0){
          printf("PING: %s\n", reply->str);
          freeReplyObject(reply);
       }
       else { printf("Error: %s\n",c->errstr);}
       sleep(4);
 }

Regards,

--Paulino

pad...@gmail.com

unread,
Dec 7, 2014, 9:52:33 AM12/7/14
to redi...@googlegroups.com
Oh. It is so simple. Thank you.

2014年12月7日日曜日 23時19分12秒 UTC+9 paulinohuerta:
Reply all
Reply to author
Forward
0 new messages