Skip to content

Commit 1725fcc

Browse files
Properly handle CLIENT variants :-)
1 parent 7e7eec6 commit 1725fcc

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

redis_cluster.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2703,6 +2703,24 @@ PHP_METHOD(RedisCluster, client) {
27032703
slot = cluster_cmd_get_slot(c, z_node TSRMLS_CC);
27042704
if(slot<0) RETURN_FALSE;
27052705

2706+
/* Our return type and reply callback is different for all subcommands */
2707+
if (opt_len == 4 && !strncasecmp(opt, "list", 4)) {
2708+
rtype = CLUSTER_IS_ATOMIC(c) ? TYPE_BULK : TYPE_LINE;
2709+
cb = cluster_client_list_resp;
2710+
} else if ((opt_len == 4 && !strncasecmp(opt, "kill", 4)) ||
2711+
(opt_len == 7 && !strncasecmp(opt, "setname", 7)))
2712+
{
2713+
rtype = TYPE_LINE;
2714+
cb = cluster_bool_resp;
2715+
} else if (opt_len == 7 && !strncasecmp(opt, "getname", 7)) {
2716+
rtype = CLUSTER_IS_ATOMIC(c) ? TYPE_BULK : TYPE_LINE;
2717+
cb = cluster_bulk_resp;
2718+
} else {
2719+
php_error_docref(NULL TSRMLS_CC, E_WARNING,
2720+
"Invalid CLIENT subcommand (LIST, KILL, GETNAME, and SETNAME are valid");
2721+
RETURN_FALSE;
2722+
}
2723+
27062724
/* Construct the command */
27072725
if (ZEND_NUM_ARGS() == 3) {
27082726
cmd_len = redis_cmd_format_static(&cmd, "CLIENT", "ss", opt, opt_len,
@@ -2714,27 +2732,20 @@ PHP_METHOD(RedisCluster, client) {
27142732
RETURN_FALSE;
27152733
}
27162734

2717-
rtype = CLUSTER_IS_ATOMIC(c) ? TYPE_BULK : TYPE_LINE;
2735+
/* Attempt to write our command */
27182736
if (cluster_send_slot(c, slot, cmd, cmd_len, rtype TSRMLS_CC)<0) {
27192737
zend_throw_exception(redis_cluster_exception_ce,
27202738
"Unable to send CLIENT command to specific node", 0 TSRMLS_CC);
27212739
efree(cmd);
27222740
RETURN_FALSE;
27232741
}
27242742

2725-
/* Handle client list and anything else differently */
2726-
if (opt_len == 4 && !strncasecmp(opt, "list", 4)) {
2727-
cb = cluster_client_list_resp;
2728-
} else {
2729-
cb = cluster_variant_resp;
2730-
}
2731-
27322743
/* Now enqueue or process response */
27332744
if (CLUSTER_IS_ATOMIC(c)) {
2734-
cluster_client_list_resp(INTERNAL_FUNCTION_PARAM_PASSTHRU, c, NULL);
2745+
cb(INTERNAL_FUNCTION_PARAM_PASSTHRU, c, NULL);
27352746
} else {
27362747
void *ctx = NULL;
2737-
CLUSTER_ENQUEUE_RESPONSE(c, slot, cluster_client_list_resp, ctx);
2748+
CLUSTER_ENQUEUE_RESPONSE(c, slot, cb, ctx);
27382749
}
27392750

27402751
efree(cmd);

tests/RedisClusterTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ public function testWait() { return $this->markTestSkipped(); }
3333
public function testSelect() { return $this->markTestSkipped(); }
3434
public function testReconnectSelect() { return $this->markTestSkipped(); }
3535

36-
/* Skips for now, which need attention */
37-
public function testClient() { return $this->markTestSkipped(); }
38-
3936
/* Load our seeds on construction */
4037
public function __construct() {
4138
$str_nodemap_file = dirname($_SERVER['PHP_SELF']) . '/nodes/nodemap';
@@ -125,6 +122,30 @@ public function testInfo() {
125122
}
126123
}
127124

125+
public function testClient() {
126+
$str_key = 'key-' . rand(1,100);
127+
128+
$this->assertTrue($this->redis->client($str_key, 'setname', 'cluster_tests'));
129+
130+
$arr_clients = $this->redis->client($str_key, 'list');
131+
$this->assertTrue(is_array($arr_clients));
132+
133+
/* Find us in the list */
134+
$str_addr = NULL;
135+
foreach ($arr_clients as $arr_client) {
136+
if ($arr_client['name'] == 'cluster_tests') {
137+
$str_addr = $arr_client['addr'];
138+
break;
139+
}
140+
}
141+
142+
/* We should be in there */
143+
$this->assertFalse(empty($str_addr));
144+
145+
/* Kill our own client! */
146+
$this->assertTrue($this->redis->client($str_key, 'kill', $str_addr));
147+
}
148+
128149
public function testTime() {
129150
$time_arr = $this->redis->time("k:" . rand(1,100));
130151
$this->assertTrue(is_array($time_arr) && count($time_arr) == 2 &&

0 commit comments

Comments
 (0)