8000 Merge branch '0-reconnect-select' · devsnippet/phpredis@38e2cca · GitHub
[go: up one dir, main page]

Skip to content

Commit 38e2cca

Browse files
committed
Merge branch '0-reconnect-select'
2 parents bcabe60 + 2e70c5a commit 38e2cca

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ typedef struct {
163163
char *persistent_id;
164164

165165
int serializer;
166+
long dbNumber;
166167

167168
char *prefix;
168169
int prefix_len;

library.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ PHPAPI int redis_check_eof(RedisSock *redis_sock TSRMLS_DC)
3838
return -1;
3939

4040
eof = php_stream_eof(redis_sock->stream);
41-
while(eof) {
42-
if((MULTI == redis_sock->mode) || redis_sock->watching || count++ == 10) { /* too many failures */
41+
for (; eof; count++) {
42+
if((MULTI == redis_sock->mode) || redis_sock->watching || count == 10) { /* too many failures */
4343
if(redis_sock->stream) { /* close stream if still here */
4444
php_stream_close(redis_sock->stream);
4545
redis_sock->stream = NULL;
@@ -61,6 +61,31 @@ PHPAPI int redis_check_eof(RedisSock *redis_sock TSRMLS_DC)
6161
eof = php_stream_eof(redis_sock->stream);
6262
}
6363
}
64+
65+
// Reselect the DB.
66+
if (count && redis_sock->dbNumber) {
67+
char *cmd, *response;
68+
int cmd_len, response_len;
69+
70+
cmd_len = redis_cmd_format_static(&cmd, "SELECT", "d", redis_sock->dbNumber);
71+
72+
if (redis_sock_write(redis_sock, cmd, cmd_len TSRMLS_CC) < 0) {
73+
efree(cmd);
74+
return -1;
75+
}
76+
efree(cmd);
77+
78+
if ((response = redis_sock_read(redis_sock, &response_len TSRMLS_CC)) == NULL) {
79+
return -1;
80+
}
81+
82+
if (strncmp(response, "+OK", 3)) {
83+
efree(response);
84+
return -1;
85+
}
86+
efree(response);
87+
}
88+
6489
return 0;
6590
}
6691

@@ -794,6 +819,7 @@ PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short por
794819
redis_sock->stream = NULL;
795820
redis_sock->status = REDIS_SOCK_STATUS_DISCONNECTED;
796821
redis_sock->watching = 0;
822+
redis_sock->dbNumber = 0;
797823

798824
redis_sock->persistent = persistent;
799825

@@ -933,6 +959,7 @@ PHPAPI int redis_sock_disconnect(RedisSock *redis_sock TSRMLS_DC)
933959
return 1;
934960
}
935961

962+
redis_sock->dbNumber = 0;
936963
if (redis_sock->stream != NULL) {
937964
if (!redis_sock->persistent) {
938965
redis_sock_write(redis_sock, "QUIT", sizeof("QUIT") - 1 TSRMLS_CC);

redis.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3378,6 +3378,8 @@ PHP_METHOD(Redis, select) {
33783378
RETURN_FALSE;
33793379
}
33803380

3381+
redis_sock->dbNumber = dbNumber;
3382+
33813383
cmd_len = redis_cmd_format_static(&cmd, "SELECT", "d", dbNumber);
33823384

33833385
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);

tests/TestRedis.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3148,6 +3148,35 @@ public function testPrefix() {
31483148
$this->redis->setOption(Redis::OPT_PREFIX, '');
31493149

31503150
}
3151+
3152+
public function testReconnectSelect() {
3153+
$key = 'reconnect-select';
3154+
$value = 'Has been set!';
3155+
3156+
$original_cfg = $this->redis->config('GET', 'timeout');
3157+
3158+
// Make sure the default DB doesn't have the key.
3159+
$this->redis->select(0);
3160+
$this->redis->delete($key);
3161+
3162+
// Set the key on a different DB.
3163+
$this->redis->select(5);
3164+
$this->redis->set($key, $value);
3165+
3166+
// Time out after 1 second.
3167+
$this->redis->config('SET', 'timeout', '1');
3168+
3169+
// Wait for the timeout. With Redis 2.4, we have to wait up to 10 s
3170+
// for the server to close the connection, regardless of the timeout
3171+
// setting.
3172+
sleep(11);
3173+
3174+
// Make sure we're still using the same DB.
3175+
$this->assertEquals($value, $this->redis->get($key));
3176+
3177+
// Revert the setting.
3178+
$this->redis->config('SET', 'timeout', $original_cfg['timeout']);
3179+
}
31513180
}
31523181

31533182
TestSuite::run("Redis_Test");

0 commit comments

Comments
 (0)
0