summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2015-02-08 23:28:27 +0100
committerantirez <antirez@gmail.com>2015-02-10 14:51:25 +0100
commit4793ea982a74fad63f79e1f47a1de9d71ce80d70 (patch)
tree746f77069619c9b971904a3cf69b2b0d89905ea7
parent9a4d71e92e27049ba115eb188b4c6c45540521d7 (diff)
downloadredis-4793ea982a74fad63f79e1f47a1de9d71ce80d70.tar.gz
redis-cli: interactive reconnection for latency modes.
--stat mode already used to reconnect automatically if the server is no longer available. This is useful since this is an interactive mode used for debugging, however the same applies to --latency and --latency-dist modes, so now both use the reconnecting command execution as well. The reconnection code was modified to use basic VT100 escape sequences in order to play better with different kinds of output on the screen when the reconnection happens, and to hide the reconnection attempt output when finally the reconnection happens.
-rw-r--r--src/redis-cli.c22
1 files changed, 13 insertions, 9 deletions
diff --git a/src/redis-cli.c b/src/redis-cli.c
index 3eba86ce1..0bf09d38e 100644
--- a/src/redis-cli.c
+++ b/src/redis-cli.c
@@ -674,16 +674,17 @@ static int cliSendCommand(int argc, char **argv, int repeat) {
return REDIS_OK;
}
-/* Send the INFO command, reconnecting the link if needed. */
-static redisReply *reconnectingInfo(void) {
- redisContext *c = context;
+/* Send a command reconnecting the link if needed. */
+static redisReply *reconnectingRedisCommand(redisContext *c, const char *fmt, ...) {
redisReply *reply = NULL;
int tries = 0;
+ va_list ap;
assert(!c->err);
while(reply == NULL) {
while (c->err & (REDIS_ERR_IO | REDIS_ERR_EOF)) {
- printf("Reconnecting (%d)...\r", ++tries);
+ printf("\r\x1b[0K"); /* Cursor to left edge + clear line. */
+ printf("Reconnecting... %d\r", ++tries);
fflush(stdout);
redisFree(c);
@@ -691,12 +692,15 @@ static redisReply *reconnectingInfo(void) {
usleep(1000000);
}
- reply = redisCommand(c,"INFO");
+ va_start(ap,fmt);
+ reply = redisvCommand(c,fmt,ap);
+ va_end(ap);
+
if (c->err && !(c->err & (REDIS_ERR_IO | REDIS_ERR_EOF))) {
fprintf(stderr, "Error: %s\n", c->errstr);
exit(1);
} else if (tries > 0) {
- printf("\n");
+ printf("\r\x1b[0K"); /* Cursor to left edge + clear line. */
}
}
@@ -1056,7 +1060,7 @@ static void latencyMode(void) {
if (!context) exit(1);
while(1) {
start = mstime();
- reply = redisCommand(context,"PING");
+ reply = reconnectingRedisCommand(context,"PING");
if (reply == NULL) {
fprintf(stderr,"\nI/O error\n");
exit(1);
@@ -1206,7 +1210,7 @@ static void latencyDistMode(void) {
if (!context) exit(1);
while(1) {
start = ustime();
- reply = redisCommand(context,"PING");
+ reply = reconnectingRedisCommand(context,"PING");
if (reply == NULL) {
fprintf(stderr,"\nI/O error\n");
exit(1);
@@ -1854,7 +1858,7 @@ static void statMode(void) {
char buf[64];
int j;
- reply = reconnectingInfo();
+ reply = reconnectingRedisCommand(context,"INFO");
if (reply->type == REDIS_REPLY_ERROR) {
printf("ERROR: %s\n", reply->str);
exit(1);