Skip to content

Commit acc84cc

Browse files
Merge pull request phpredis#1220 from phpredis/pipe-multi-warning-revert
Pipe multi warning revert
2 parents 42f1c97 + af71d42 commit acc84cc

File tree

4 files changed

+52
-20
lines changed

4 files changed

+52
-20
lines changed

library.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ PHP_REDIS_API int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
14111411
struct timeval tv, read_tv, *tv_ptr = NULL;
14121412
char host[1024], *persistent_id = NULL;
14131413
const char *fmtstr = "%s:%d";
1414-
int host_len, err = 0;
1414+
int host_len, usocket = 0, err = 0;
14151415
php_netstream_data_t *sock;
14161416
int tcp_flag = 1;
14171417

@@ -1430,6 +1430,7 @@ PHP_REDIS_API int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
14301430

14311431
if(redis_sock->host[0] == '/' && redis_sock->port < 1) {
14321432
host_len = snprintf(host, sizeof(host), "unix://%s", redis_sock->host);
1433+
usocket = 1;
14331434
} else {
14341435
if(redis_sock->port == 0)
14351436
redis_sock->port = 6379;
@@ -1466,10 +1467,11 @@ PHP_REDIS_API int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
14661467
return -1;
14671468
}
14681469

1469-
/* set TCP_NODELAY */
1470+
/* Attempt to set TCP_NODELAY if we're not using a unix socket. */
14701471
sock = (php_netstream_data_t*)redis_sock->stream->abstract;
1471-
if (setsockopt(sock->socket, IPPROTO_TCP, TCP_NODELAY, (char *) &tcp_flag, sizeof(int)) < 0) {
1472-
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Can't activate TCP_NODELAY option!");
1472+
if (!usocket) {
1473+
err = setsockopt(sock->socket, IPPROTO_TCP, TCP_NODELAY, (char *) &tcp_flag, sizeof(int));
1474+
PHPREDIS_NOTUSED(err);
14731475
}
14741476

14751477
php_stream_auto_cleanup(redis_sock->stream);

redis.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2605,19 +2605,20 @@ PHP_METHOD(Redis, multi)
26052605
}
26062606

26072607
if (multi_value == PIPELINE) {
2608-
IF_PIPELINE() {
2609-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Already in pipeline mode");
2610-
} else IF_MULTI() {
2608+
/* Cannot enter pipeline mode in a MULTI block */
2609+
IF_MULTI() {
26112610
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Can't activate pipeline in multi mode!");
26122611
RETURN_FALSE;
2613-
} else {
2612+
}
2613+
2614+
/* Enable PIPELINE if we're not already in one */
2615+
IF_ATOMIC() {
26142616
free_reply_callbacks(redis_sock);
26152617
REDIS_ENABLE_MODE(redis_sock, PIPELINE);
26162618
}
26172619
} else if (multi_value == MULTI) {
2618-
IF_MULTI() {
2619-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Already in multi mode");
2620-
} else {
2620+
/* Don't want to do anything if we're alredy in MULTI mode */
2621+
IF_NOT_MULTI() {
26212622
cmd_len = REDIS_SPPRINTF(&cmd, "MULTI", "");
26222623
IF_PIPELINE() {
26232624
PIPELINE_ENQUEUE_COMMAND(cmd, cmd_len);
@@ -2638,8 +2639,10 @@ PHP_METHOD(Redis, multi)
26382639
}
26392640
}
26402641
} else {
2642+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown mode sent to Redis::multi");
26412643
RETURN_FALSE;
26422644
}
2645+
26432646
RETURN_ZVAL(getThis(), 1, 0);
26442647
}
26452648

@@ -2823,21 +2826,22 @@ PHP_METHOD(Redis, pipeline)
28232826
RETURN_FALSE;
28242827
}
28252828

2826-
IF_PIPELINE() {
2827-
php_error_docref(NULL TSRMLS_CC, E_WARNING,
2828-
"Already in pipeline mode");
2829-
} else {
2830-
IF_MULTI() {
2831-
php_error_docref(NULL TSRMLS_CC, E_ERROR,
2832-
"Can't activate pipeline in multi mode!");
2833-
RETURN_FALSE;
2834-
}
2829+
/* User cannot enter MULTI mode if already in a pipeline */
2830+
IF_MULTI() {
2831+
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Can't activate pipeline in multi mode!");
2832+
RETURN_FALSE;
2833+
}
2834+
2835+
/* Enable pipeline mode unless we're already in that mode in which case this
2836+
* is just a NO OP */
2837+
IF_ATOMIC() {
28352838
/* NB : we keep the function fold, to detect the last function.
28362839
* We need the response format of the n - 1 command. So, we can delete
28372840
* when n > 2, the { 1 .. n - 2} commands */
28382841
free_reply_callbacks(redis_sock);
28392842
REDIS_ENABLE_MODE(redis_sock, PIPELINE);
28402843
}
2844+
28412845
RETURN_ZVAL(getThis(), 1, 0);
28422846
}
28432847

tests/RedisClusterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function testWait() { return $this->markTestSkipped(); }
3333
public function testSelect() { return $this->markTestSkipped(); }
3434
public function testReconnectSelect() { return $this->markTestSkipped(); }
3535
public function testMultipleConnect() { return $this->markTestSkipped(); }
36+
public function testDoublePipeNoOp() { return $this->markTestSkipped(); }
3637

3738
/* Load our seeds on construction */
3839
public function __construct() {

tests/RedisTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2554,6 +2554,31 @@ public function testPipelineMultiExec()
25542554
$this->assertEquals(5, count($ret)); // should be 5 atomic operations
25552555
}
25562556

2557+
/* Github issue #1211 (ignore redundant calls to pipeline or multi) */
2558+
public function testDoublePipeNoOp() {
2559+
/* Only the first pipeline should be honored */
2560+
for ($i = 0; $i < 6; $i++) {
2561+
$this->redis->pipeline();
2562+
}
2563+
2564+
/* Set and get in our pipeline */
2565+
$this->redis->set('pipecount','over9000')->get('pipecount');
2566+
2567+
$data = $this->redis->exec();
2568+
$this->assertEquals(Array(true,'over9000'), $data);
2569+
2570+
/* Only the first MULTI should be honored */
2571+
for ($i = 0; $i < 6; $i++) {
2572+
$this->redis->multi();
2573+
}
2574+
2575+
/* Set and get in our MULTI block */
2576+
$this->redis->set('multicount', 'over9000')->get('multicount');
2577+
2578+
$data = $this->redis->exec();
2579+
$this->assertEquals(Array(true, 'over9000'), $data);
2580+
}
2581+
25572582
protected function sequence($mode) {
25582583
$ret = $this->redis->multi($mode)
25592584
->set('x', 42)

0 commit comments

Comments
 (0)