Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'reconnect-select' of https://github.com/0/phpredis into…

… 0-reconnect-select
  • Loading branch information...
commit 2e70c5a5a93bbf3cb2d6a1f31a01e6758b21ae11 2 parents bcabe60 + db3c3c5
@nicolasff nicolasff authored
Showing with 61 additions and 2 deletions.
  1. +1 −0  common.h
  2. +29 −2 library.c
  3. +2 −0  redis.c
  4. +29 −0 tests/TestRedis.php
View
1  common.h
@@ -163,6 +163,7 @@ typedef struct {
char *persistent_id;
int serializer;
+ long dbNumber;
char *prefix;
int prefix_len;
View
31 library.c
@@ -38,8 +38,8 @@ PHPAPI int redis_check_eof(RedisSock *redis_sock TSRMLS_DC)
return -1;
eof = php_stream_eof(redis_sock->stream);
- while(eof) {
- if((MULTI == redis_sock->mode) || redis_sock->watching || count++ == 10) { /* too many failures */
+ for (; eof; count++) {
+ if((MULTI == redis_sock->mode) || redis_sock->watching || count == 10) { /* too many failures */
if(redis_sock->stream) { /* close stream if still here */
php_stream_close(redis_sock->stream);
redis_sock->stream = NULL;
@@ -61,6 +61,31 @@ PHPAPI int redis_check_eof(RedisSock *redis_sock TSRMLS_DC)
eof = php_stream_eof(redis_sock->stream);
}
}
+
+ // Reselect the DB.
+ if (count && redis_sock->dbNumber) {
+ char *cmd, *response;
+ int cmd_len, response_len;
+
+ cmd_len = redis_cmd_format_static(&cmd, "SELECT", "d", redis_sock->dbNumber);
+
+ if (redis_sock_write(redis_sock, cmd, cmd_len TSRMLS_CC) < 0) {
+ efree(cmd);
+ return -1;
+ }
+ efree(cmd);
+
+ if ((response = redis_sock_read(redis_sock, &response_len TSRMLS_CC)) == NULL) {
+ return -1;
+ }
+
+ if (strncmp(response, "+OK", 3)) {
+ efree(response);
+ return -1;
+ }
+ efree(response);
+ }
+
return 0;
}
@@ -794,6 +819,7 @@ PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short por
redis_sock->stream = NULL;
redis_sock->status = REDIS_SOCK_STATUS_DISCONNECTED;
redis_sock->watching = 0;
+ redis_sock->dbNumber = 0;
redis_sock->persistent = persistent;
@@ -933,6 +959,7 @@ PHPAPI int redis_sock_disconnect(RedisSock *redis_sock TSRMLS_DC)
return 1;
}
+ redis_sock->dbNumber = 0;
if (redis_sock->stream != NULL) {
if (!redis_sock->persistent) {
redis_sock_write(redis_sock, "QUIT", sizeof("QUIT") - 1 TSRMLS_CC);
View
2  redis.c
@@ -3378,6 +3378,8 @@ PHP_METHOD(Redis, select) {
RETURN_FALSE;
}
+ redis_sock->dbNumber = dbNumber;
+
cmd_len = redis_cmd_format_static(&cmd, "SELECT", "d", dbNumber);
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
View
29 tests/TestRedis.php
@@ -3148,6 +3148,35 @@ public function testPrefix() {
$this->redis->setOption(Redis::OPT_PREFIX, '');
}
+
+ public function testReconnectSelect() {
+ $key = 'reconnect-select';
+ $value = 'Has been set!';
+
+ $original_cfg = $this->redis->config('GET', 'timeout');
+
+ // Make sure the default DB doesn't have the key.
+ $this->redis->select(0);
+ $this->redis->delete($key);
+
+ // Set the key on a different DB.
+ $this->redis->select(5);
+ $this->redis->set($key, $value);
+
+ // Time out after 1 second.
+ $this->redis->config('SET', 'timeout', '1');
+
+ // Wait for the timeout. With Redis 2.4, we have to wait up to 10 s
+ // for the server to close the connection, regardless of the timeout
+ // setting.
+ sleep(11);
+
+ // Make sure we're still using the same DB.
+ $this->assertEquals($value, $this->redis->get($key));
+
+ // Revert the setting.
+ $this->redis->config('SET', 'timeout', $original_cfg['timeout']);
+ }
}
TestSuite::run("Redis_Test");
Please sign in to comment.
Something went wrong with that request. Please try again.