Skip to content

Commit 2b429b0

Browse files
addressing some comments
Signed-off-by: Sarthak Aggarwal <[email protected]>
1 parent 911c047 commit 2b429b0

File tree

2 files changed

+64
-26
lines changed

2 files changed

+64
-26
lines changed

src/networking.c

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,26 @@
4242
#include <ctype.h>
4343
#include <stdatomic.h>
4444
#include "intset.h"
45-
#include <stdbool.h>
4645

47-
/**
48-
* This struct is used to encapsulate filtering criteria for operations on clients
46+
/* This struct is used to encapsulate filtering criteria for operations on clients
4947
* such as identifying specific clients to kill or retrieve. Each field in the struct
50-
* represents a filter that can be applied based on specific attributes of a client.
51-
*/
48+
* represents a filter that can be applied based on specific attributes of a client. */
5249
typedef struct {
53-
/** A set of client IDs to filter. If NULL, no ID filtering is applied. */
50+
/* A set of client IDs to filter. If NULL, no ID filtering is applied. */
5451
intset *ids;
55-
/** Maximum age (in seconds) of a client connection for filtering.
52+
/* Maximum age (in seconds) of a client connection for filtering.
5653
* Connections younger than this value will not match.
5754
* A value of 0 means no age filtering. */
5855
long long max_age;
59-
/** Address/port of the client. If NULL, no address filtering is applied. */
56+
/* Address/port of the client. If NULL, no address filtering is applied. */
6057
char *addr;
61-
/** Remote address/port of the client. If NULL, no address filtering is applied. */
58+
/* Remote address/port of the client. If NULL, no address filtering is applied. */
6259
char *laddr;
63-
/** Filtering clients by authentication user. If NULL, no user-based filtering is applied. */
60+
/* Filtering clients by authentication user. If NULL, no user-based filtering is applied. */
6461
user *user;
65-
/** Client type to filter. If set to -1, no type filtering is applied. */
62+
/* Client type to filter. If set to -1, no type filtering is applied. */
6663
int type;
67-
/**< Boolean flag to determine if the current client (`me`) should be filtered. 1 means "skip me", 0 means otherwise. */
64+
/* Boolean flag to determine if the current client (`me`) should be filtered. 1 means "skip me", 0 means otherwise. */
6865
int skipme;
6966
} clientFilter;
7067

@@ -73,7 +70,7 @@ static void pauseClientsByClient(mstime_t end, int isPauseClientAll);
7370
int postponeClientRead(client *c);
7471
char *getClientSockname(client *c);
7572
int parseClientFilters(client *c, int i, clientFilter *filter);
76-
bool clientMatchesFilter(client *client, clientFilter client_filter);
73+
int clientMatchesFilter(client *client, clientFilter client_filter);
7774
sds getAllFilteredClientsInfoString(clientFilter *client_filter, int hide_user_data);
7875

7976
int ProcessingEventsWhileBlocked = 0; /* See processEventsWhileBlocked(). */
@@ -3455,7 +3452,7 @@ sds getAllClientsInfoString(int type, int hide_user_data) {
34553452
listNode *ln;
34563453
listIter li;
34573454
client *client;
3458-
sds o = sdsnewlen(SDS_NOINIT, 500);
3455+
sds o = sdsnewlen(SDS_NOINIT, 200 * listLength(server.clients));
34593456
sdsclear(o);
34603457
listRewind(server.clients, &li);
34613458
while ((ln = listNext(&li)) != NULL) {
@@ -3471,7 +3468,7 @@ sds getAllFilteredClientsInfoString(clientFilter *client_filter, int hide_user_d
34713468
listNode *ln;
34723469
listIter li;
34733470
client *client;
3474-
sds o = sdsnewlen(SDS_NOINIT, 200 * listLength(server.clients));
3471+
sds o = sdsnewlen(SDS_NOINIT, 500);
34753472
sdsclear(o);
34763473
listRewind(server.clients, &li);
34773474
while ((ln = listNext(&li)) != NULL) {
@@ -3678,18 +3675,18 @@ int parseClientFilters(client *c, int i, clientFilter *filter) {
36783675
return C_OK;
36793676
}
36803677

3681-
bool clientMatchesFilter(client *client, clientFilter client_filter) {
3678+
int clientMatchesFilter(client *client, clientFilter client_filter) {
36823679
// Check each filter condition and return false if the client does not match.
3683-
if (client_filter.addr && strcmp(getClientPeerId(client), client_filter.addr) != 0) return false;
3684-
if (client_filter.laddr && strcmp(getClientSockname(client), client_filter.laddr) != 0) return false;
3685-
if (client_filter.type != -1 && getClientType(client) != client_filter.type) return false;
3686-
if (client_filter.ids && !intsetFind(client_filter.ids, client->id)) return false;
3687-
if (client_filter.user && client->user != client_filter.user) return false;
3688-
if (client_filter.skipme && client == server.current_client) return false; // Skipme check
3689-
if (client_filter.max_age != 0 && (long long)(commandTimeSnapshot() / 1000 - client->ctime) < client_filter.max_age) return false;
3680+
if (client_filter.addr && strcmp(getClientPeerId(client), client_filter.addr) != 0) return 0;
3681+
if (client_filter.laddr && strcmp(getClientSockname(client), client_filter.laddr) != 0) return 0;
3682+
if (client_filter.type != -1 && getClientType(client) != client_filter.type) return 0;
3683+
if (client_filter.ids && !intsetFind(client_filter.ids, client->id)) return 0;
3684+
if (client_filter.user && client->user != client_filter.user) return 0;
3685+
if (client_filter.skipme && client == server.current_client) return 0; // Skipme check
3686+
if (client_filter.max_age != 0 && (long long)(commandTimeSnapshot() / 1000 - client->ctime) < client_filter.max_age) return 0;
36903687

36913688
// If all conditions are satisfied, the client matches the filter.
3692-
return true;
3689+
return 1;
36933690
}
36943691

36953692
void clientCommand(client *c) {
@@ -3851,12 +3848,12 @@ void clientCommand(client *c) {
38513848

38523849
/* New style syntax: parse options. */
38533850
if (parseClientFilters(c, i, &client_filter) != C_OK) {
3854-
zfree(client_filter.ids); // Free the intset on error
3851+
goto client_kill_done; // Free the intset on error
38553852
return;
38563853
}
38573854
} else {
3858-
zfree(client_filter.ids); // Free the intset on error
38593855
addReplyErrorObject(c, shared.syntaxerr);
3856+
goto client_kill_done; // Free the intset on error
38603857
return;
38613858
}
38623859

@@ -3888,6 +3885,7 @@ void clientCommand(client *c) {
38883885
/* If this client has to be closed, flag it as CLOSE_AFTER_REPLY
38893886
* only after we queued the reply to its output buffers. */
38903887
if (close_this_client) c->flag.close_after_reply = 1;
3888+
client_kill_done:
38913889
zfree(client_filter.ids);
38923890
} else if (!strcasecmp(c->argv[1]->ptr, "unblock") && (c->argc == 3 || c->argc == 4)) {
38933891
/* CLIENT UNBLOCK <id> [timeout|error] */

tests/unit/introspection.tcl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,46 @@ start_server {tags {"introspection"}} {
8181
assert_equal $found_self 1
8282
}
8383

84+
test {CLIENT LIST with multiple IDs and TYPE filter} {
85+
# Create multiple clients
86+
set c1 [valkey_client]
87+
set c2 [valkey_client]
88+
set c3 [valkey_client]
89+
90+
# Fetch their IDs
91+
set id1 [$c1 client id]
92+
set id2 [$c2 client id]
93+
set id3 [$c3 client id]
94+
95+
# Filter by multiple IDs and TYPE
96+
set cl [split [r client list id $id1 $id2 type normal] "\r\n"]
97+
98+
# Assert only c1 and c2 are present and match TYPE=N (NORMAL)
99+
foreach line $cl {
100+
regexp {id=([0-9]+).*flags=([^ ]+)} $line _ client_id flags
101+
assert {[lsearch -exact "$id1 $id2" $client_id] != -1}
102+
assert {[string match *N* $flags]}
103+
}
104+
105+
# Close clients
106+
$c1 close
107+
$c2 close
108+
$c3 close
109+
}
110+
111+
test {CLIENT LIST with filters matching no clients} {
112+
# Create multiple clients
113+
set c1 [valkey_client]
114+
set c2 [valkey_client]
115+
116+
# Use a filter that doesn't match any client (e.g., invalid user)
117+
assert_error "ERR No such user 'invalid_user'" {r client list user invalid_user}
118+
119+
# Close clients
120+
$c1 close
121+
$c2 close
122+
}
123+
84124
test {CLIENT LIST with illegal arguments} {
85125
assert_error "ERR syntax error" {r client list id 10 wrong_arg}
86126

0 commit comments

Comments
 (0)