Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d11798e

Browse files
* Only attempt to set TCP_NODELAY on TCP sockets and also don't abort
if we can't. * Stop throwing warnings if users double up on pipeline or multi calls reverting to previous behavior.
1 parent e672f40 commit d11798e

2 files changed

Lines changed: 25 additions & 20 deletions

File tree

library.c

Lines changed: 5 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,10 @@ 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+
setsockopt(sock->socket, IPPROTO_TCP, TCP_NODELAY, (char *) &tcp_flag, sizeof(int));
14731474
}
14741475

14751476
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

@@ -2821,21 +2824,22 @@ PHP_METHOD(Redis, pipeline)
28212824
RETURN_FALSE;
28222825
}
28232826

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

0 commit comments

Comments
 (0)