Sitelet https://github.com/phpredis/phpredis/commit/e0a88b7b
Skip to content

Commit e0a88b7

Browse files
committed
Issue #2106
Expose the transferred number of bytes
1 parent d73f3f4 commit e0a88b7

5 files changed

Lines changed: 27 additions & 6 deletions

File tree

‎cluster_library.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,8 @@ static int cluster_send_readonly(RedisSock *redis_sock) {
299299

300300
/* Send MULTI to a specific ReidsSock */
301301
static int cluster_send_multi(redisCluster *c, short slot) {
302-
if (cluster_send_direct(SLOT_SOCK(c,slot), RESP_MULTI_CMD,
303-
sizeof(RESP_MULTI_CMD) - 1, TYPE_LINE) == 0)
304-
{
302+
if (cluster_send_direct(SLOT_SOCK(c,slot), ZEND_STRL(RESP_MULTI_CMD), TYPE_LINE) == 0) {
303+
c->flags->txBytes += sizeof(RESP_MULTI_CMD) - 1;
305304
c->cmd_sock->mode = MULTI;
306305
return 0;
307306
}
@@ -1513,6 +1512,9 @@ PHP_REDIS_API int cluster_send_slot(redisCluster *c, short slot, char *cmd,
15131512
/* Point our cluster to this slot and it's socket */
15141513
c->cmd_slot = slot;
15151514
c->cmd_sock = SLOT_SOCK(c, slot);
1515+
if (c->flags->mode != MULTI) {
1516+
c->flags->txBytes = 0;
1517+
}
15161518

15171519
/* Enable multi mode on this slot if we've been directed to but haven't
15181520
* send it to this node yet */
@@ -1527,6 +1529,7 @@ PHP_REDIS_API int cluster_send_slot(redisCluster *c, short slot, char *cmd,
15271529
if (cluster_sock_write(c, cmd, cmd_len, 1) == -1) {
15281530
return -1;
15291531
}
1532+
c->flags->txBytes += cmd_len;
15301533

15311534
/* Check our response */
15321535
if (cluster_check_response(c, &c->reply_type) != 0 ||

‎library.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3128,7 +3128,12 @@ redis_sock_write(RedisSock *redis_sock, char *cmd, size_t sz)
31283128
if (redis_check_eof(redis_sock, 0, 0) == 0 &&
31293129
php_stream_write(redis_sock->stream, cmd, sz) == sz
31303130
) {
3131-
return redis_sock->txBytes = sz;
3131+
if (IS_MULTI(redis_sock)) {
3132+
redis_sock->txBytes += sz;
3133+
} else {
3134+
redis_sock->txBytes = sz;
3135+
}
3136+
return sz;
31323137
}
31333138
return -1;
31343139
}

‎redis_cluster.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,8 @@ PHP_METHOD(RedisCluster, clearlasterror) {
17121712
}
17131713

17141714
PHP_METHOD(RedisCluster, gettransferredbytes) {
1715-
CLUSTER_THROW_EXCEPTION("Not implemented", 0);
1715+
redisCluster *c = GET_CONTEXT();
1716+
RETURN_LONG(c->flags->txBytes);
17161717
}
17171718
/* }}} */
17181719

@@ -1833,6 +1834,8 @@ PHP_METHOD(RedisCluster, multi) {
18331834
/* Flag that we're in MULTI mode */
18341835
c->flags->mode = MULTI;
18351836

1837+
c->flags->txBytes = 0;
1838+
18361839
/* Return our object so we can chain MULTI calls */
18371840
RETVAL_ZVAL(getThis(), 1, 0);
18381841
}

‎tests/RedisClusterTest.php‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public function testTlsConnect() { return $this->markTestSkipped(); }
5050
public function testReset() { return $this->markTestSkipped(); }
5151
public function testInvalidAuthArgs() { return $this->markTestSkipped(); }
5252
public function testScanErrors() { return $this->markTestSkipped(); }
53-
public function testTransferredBytes() { return $this->markTestSkipped(); }
5453

5554
public function testlMove() { return $this->markTestSkipped(); }
5655
public function testlPos() { return $this->marktestSkipped(); }
@@ -759,6 +758,15 @@ public function testConnectionPool() {
759758
ini_set('redis.pconnect.pooling_enabled', $prev_value);
760759
}
761760

761+
public function testTransferredBytes() {
762+
$this->assertTrue($this->redis->ping(''));
763+
$this->assertEquals(strlen("*1\r\n$4\r\nPING\r\n"), $this->redis->getTransferredBytes());
764+
$this->assertEquals(['cluster_enabled' => 1], $this->redis->info('', 'cluster'));
765+
$this->assertEquals(strlen("*2\r\n$4\r\nINFO\r\n$7\r\ncluster\r\n"), $this->redis->getTransferredBytes());
766+
$this->assertEquals([true, true], $this->redis->multi()->ping('')->ping('')->exec());
767+
$this->assertEquals(strlen("*1\r\n$5\r\nMULTI\r\n*1\r\n$4\r\nEXEC\r\n") + 2 * strlen("*2\r\n$4\r\nPING\r\n"), $this->redis->getTransferredBytes());
768+
}
769+
762770
/**
763771
* @inheritdoc
764772
*/

‎tests/RedisTest.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5757,6 +5757,8 @@ public function testTransferredBytes() {
57575757
$this->assertEquals(strlen("*1\r\n$4\r\nPING\r\n"), $this->redis->getTransferredBytes());
57585758
$this->assertEquals(['cluster_enabled' => 0], $this->redis->info('cluster'));
57595759
$this->assertEquals(strlen("*2\r\n$4\r\nINFO\r\n$7\r\ncluster\r\n"), $this->redis->getTransferredBytes());
5760+
$this->assertEquals([true, true], $this->redis->multi()->ping()->ping()->exec());
5761+
$this->assertEquals(strlen("*1\r\n$5\r\nMULTI\r\n*1\r\n$4\r\nEXEC\r\n") + 2 * strlen("*2\r\n$4\r\nPING\r\n"), $this->redis->getTransferredBytes());
57605762
}
57615763

57625764
/**

0 commit comments

Comments
 (0)