Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

valkey-cli auto-exit from subscribed mode #1432

Merged
merged 20 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c21bf9d
Fixed issue with exiting subscribe mode
Nikhil-Manglore Dec 12, 2024
cb71718
fixed issue with the counters when exit subscribe mode
Nikhil-Manglore Dec 18, 2024
926a713
refactored code in valkey-cli.cand fixed the test cases
Nikhil-Manglore Dec 19, 2024
2278fce
fixed typo in the comments
Nikhil-Manglore Dec 19, 2024
0c4a177
Refactored code again and added new tests to the test case
Nikhil-Manglore Dec 19, 2024
398c482
Merge branch 'valkey-io:unstable' into subscribe-mode-bug
Nikhil-Manglore Dec 20, 2024
a047778
Merge branch 'valkey-io:unstable' into subscribe-mode-bug
Nikhil-Manglore Dec 26, 2024
791a80c
Merge branch 'valkey-io:unstable' into subscribe-mode-bug
Nikhil-Manglore Jan 2, 2025
d6c863e
Merge branch 'valkey-io:unstable' into subscribe-mode-bug
Nikhil-Manglore Jan 3, 2025
79aff57
Fixed all issues with subscribe mode, refactored code, and finished i…
Nikhil-Manglore Jan 3, 2025
6743126
Merge branch 'subscribe-mode-bug' of github.com:Nikhil-Manglore/valke…
Nikhil-Manglore Jan 3, 2025
b09ef32
Refactored code, added punsubscribe functionality, and made test case…
Nikhil-Manglore Jan 6, 2025
930b4db
cleaned the code up to make it more readable
Nikhil-Manglore Jan 6, 2025
1849195
Fixed corner cases, added test case and split up existing test cases
Nikhil-Manglore Jan 6, 2025
9f99a51
Refactored code
Nikhil-Manglore Jan 7, 2025
022b2cc
Removed redundant code
Nikhil-Manglore Jan 7, 2025
fe091cf
Removed redundant code
Nikhil-Manglore Jan 8, 2025
9aff4d8
removed if statement and updated comments
Nikhil-Manglore Jan 8, 2025
6aebdb9
Fixed formatting errors
Nikhil-Manglore Jan 8, 2025
fcc2742
Fixed final formatting error
Nikhil-Manglore Jan 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/valkey-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ static struct config {
int shutdown;
int monitor_mode;
int pubsub_mode;
int pubsub_unsharded_count; /* channels and patterns */
int pubsub_sharded_count; /* shard channels */
int blocking_state_aborted; /* used to abort monitor_mode and pubsub_mode. */
int latency_mode;
int latency_dist_mode;
Expand Down Expand Up @@ -2224,6 +2226,7 @@ static int cliReadReply(int output_raw_strings) {
fflush(stdout);
sdsfree(out);
}

return REDIS_OK;
}

Expand Down Expand Up @@ -2402,6 +2405,25 @@ static int cliSendCommand(int argc, char **argv, long repeat) {
config.pubsub_mode = 1;
cliRefreshPrompt();
}

/* Handle pubsub mode */
if (config.last_reply->elements >= 3) {
zuiderkwast marked this conversation as resolved.
Show resolved Hide resolved
char *cmd = config.last_reply->element[0]->str;
int count = config.last_reply->element[2]->integer;

if (strcmp(cmd, "subscribe") == 0 || strcmp(cmd, "unsubscribe") == 0 ||
strcmp(cmd, "psubscribe") == 0 || strcmp(cmd, "punsubscribe") == 0) {
config.pubsub_unsharded_count = count;
} else if (strcmp(cmd, "ssubscribe") == 0 || strcmp(cmd, "sunsubscribe") == 0) {
config.pubsub_sharded_count = count;
}

if (config.pubsub_unsharded_count == 0 && config.pubsub_sharded_count == 0) {
config.pubsub_mode = 0;
cliRefreshPrompt();
}
}

if (--num_expected_pubsub_push > 0) {
continue; /* We need more of these. */
}
Expand Down Expand Up @@ -9493,6 +9515,8 @@ int main(int argc, char **argv) {
config.shutdown = 0;
config.monitor_mode = 0;
config.pubsub_mode = 0;
config.pubsub_unsharded_count = 0;
config.pubsub_sharded_count = 0;
config.blocking_state_aborted = 0;
config.latency_mode = 0;
config.latency_dist_mode = 0;
Expand Down
41 changes: 41 additions & 0 deletions tests/integration/valkey-cli.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,47 @@ if {!$::tls} { ;# fake_redis_node doesn't support TLS
assert_equal "a\n1\nb\n2\nc\n3" [exec {*}$cmdline ZRANGE new_zset 0 -1 WITHSCORES]
}

test {valkey-cli pubsub mode with multiple subscription types} {
set fd [open_cli]

# Subscribe to a regular channel
write_cli $fd "SUBSCRIBE channel1"
assert_match "*subscribe*channel1*" [read_cli $fd]

# Subscribe to a pattern
write_cli $fd "PSUBSCRIBE pattern*"
assert_match "*psubscribe*pattern**" [read_cli $fd]

# Subscribe to a shard channel
write_cli $fd "SSUBSCRIBE schannel1"
assert_match "*ssubscribe*schannel1*" [read_cli $fd]

# Unsubscribe from regular channel
write_cli $fd "UNSUBSCRIBE channel1"
assert_match "*unsubscribe*channel1*" [read_cli $fd]

# Verify still in pubsub mode
catch {run_command $fd "SET key value"} err
assert_match "*ERR*only*SUBSCRIBE*UNSUBSCRIBE*allowed*" $err

# Unsubscribe from pattern
write_cli $fd "PUNSUBSCRIBE pattern*"
assert_match "*punsubscribe*pattern**" [read_cli $fd]

# Verify still in pubsub mode
catch {run_command $fd "GET key"} err
assert_match "*ERR*only*SUBSCRIBE*UNSUBSCRIBE*allowed*" $err

# Unsubscribe from shard channel
write_cli $fd "SUNSUBSCRIBE schannel1"
assert_match "*sunsubscribe*schannel1*" [read_cli $fd]
zuiderkwast marked this conversation as resolved.
Show resolved Hide resolved

# Verify that we've exited pubsub mode
assert_equal "PONG" [run_command $fd "PING"]
zuiderkwast marked this conversation as resolved.
Show resolved Hide resolved

close_cli $fd
}

test "Valid Connection Scheme: redis://" {
set cmdline [valkeycliuri "redis://" [srv host] [srv port]]
assert_equal {PONG} [exec {*}$cmdline PING]
Expand Down
Loading