Add CLIENT INFO and CLIENT LIST [id]. (#8113)

* Add CLIENT INFO subcommand.

The output is identical to CLIENT LIST but provides a single line for
the current client only.

* Add CLIENT LIST ID [id...].

Co-authored-by: Itamar Haber <itamar@redislabs.com>
This commit is contained in:
Yossi Gottlieb 2020-12-07 14:24:05 +02:00 committed by GitHub
parent ec951cdc15
commit bccbc5509a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 3 deletions

View File

@ -2331,6 +2331,7 @@ void clientCommand(client *c) {
if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"help")) {
const char *help[] = {
"ID -- Return the ID of the current connection.",
"INFO -- Return information about the current client connection.",
"GETNAME -- Return the name of the current connection.",
"KILL <ip:port> -- Kill connection made from <ip:port>.",
"KILL <option> <value> [option value ...] -- Kill connections. Options are:",
@ -2341,6 +2342,7 @@ void clientCommand(client *c) {
" SKIPME (yes|no) -- Skip killing current connection (default: yes).",
"LIST [options ...] -- Return information about client connections. Options:",
" TYPE (normal|master|replica|pubsub) -- Return clients of specified type.",
" ID id [id ...] -- Return clients of specified IDs only.",
"PAUSE <timeout> -- Suspend all Redis clients for <timout> milliseconds.",
"REPLY (on|off|skip) -- Control the replies sent to the current connection.",
"SETNAME <name> -- Assign the name <name> to the current connection.",
@ -2354,21 +2356,46 @@ NULL
} else if (!strcasecmp(c->argv[1]->ptr,"id") && c->argc == 2) {
/* CLIENT ID */
addReplyLongLong(c,c->id);
} else if (!strcasecmp(c->argv[1]->ptr,"info") && c->argc == 2) {
/* CLIENT INFO */
sds o = catClientInfoString(sdsempty(), c);
o = sdscatlen(o,"\n",1);
addReplyVerbatim(c,o,sdslen(o),"txt");
sdsfree(o);
} else if (!strcasecmp(c->argv[1]->ptr,"list")) {
/* CLIENT LIST */
int type = -1;
sds o = NULL;
if (c->argc == 4 && !strcasecmp(c->argv[2]->ptr,"type")) {
type = getClientTypeByName(c->argv[3]->ptr);
if (type == -1) {
addReplyErrorFormat(c,"Unknown client type '%s'",
(char*) c->argv[3]->ptr);
return;
}
}
} else if (c->argc > 3 && !strcasecmp(c->argv[2]->ptr,"id")) {
int j;
o = sdsempty();
for (j = 3; j < c->argc; j++) {
long long cid;
if (getLongLongFromObjectOrReply(c, c->argv[j], &cid,
"Invalid client ID")) {
sdsfree(o);
return;
}
client *cl = lookupClientByID(cid);
if (cl) {
o = catClientInfoString(o, cl);
o = sdscatlen(o, "\n", 1);
}
}
} else if (c->argc != 2) {
addReply(c,shared.syntaxerr);
return;
}
sds o = getAllClientsInfoString(type);
if (!o)
o = getAllClientsInfoString(type);
addReplyVerbatim(c,o,sdslen(o),"txt");
sdsfree(o);
} else if (!strcasecmp(c->argv[1]->ptr,"reply") && c->argc == 3) {

View File

@ -1304,7 +1304,8 @@ static int cliSendCommand(int argc, char **argv, long repeat) {
(!strcasecmp(argv[1],"nodes") ||
!strcasecmp(argv[1],"info"))) ||
(argc >= 2 && !strcasecmp(command,"client") &&
!strcasecmp(argv[1],"list")) ||
(!strcasecmp(argv[1],"list") ||
!strcasecmp(argv[1],"info"))) ||
(argc == 3 && !strcasecmp(command,"latency") &&
!strcasecmp(argv[1],"graph")) ||
(argc == 2 && !strcasecmp(command,"latency") &&

View File

@ -3,6 +3,16 @@ start_server {tags {"introspection"}} {
r client list
} {*addr=*:* fd=* age=* idle=* flags=N db=9 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=* argv-mem=* obl=0 oll=0 omem=0 tot-mem=* events=r cmd=client*}
test {CLIENT LIST with IDs} {
set myid [r client id]
set cl [split [r client list id $myid] "\r\n"]
assert_match "id=$myid*" [lindex $cl 0]
}
test {CLIENT INFO} {
r client info
} {*addr=*:* fd=* age=* idle=* flags=N db=9 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=* argv-mem=* obl=0 oll=0 omem=0 tot-mem=* events=r cmd=client*}
test {MONITOR can log executed commands} {
set rd [redis_deferring_client]
$rd monitor