From 9f5f4f815ab37c35d620c76480e0932bd88760ed Mon Sep 17 00:00:00 2001 From: Hamid Tavakoli Date: Wed, 3 Dec 2014 10:42:53 -0800 Subject: [PATCH] Fix #2517,Add pid support for persistent connection in Redis Summary: pfsockopen does not supports id. So I changed it stream_socket_client, to handles ids as well. Closes https://github.com/facebook/hhvm/pull/4291 Reviewed By: @fredemmott Differential Revision: D1693997 Signature: t1:1693997:1417632135:c2b2b2648ee77d911d00e1da801c8698e591c784 --- hphp/system/php/redis/Redis.php | 17 ++++++++---- .../slow/ext_redis/pconnectWithoutPID.php | 22 ++++++++++++++++ .../ext_redis/pconnectWithoutPID.php.expect | 3 +++ .../ext_redis/pconnectWithoutPID.php.skipif | 12 +++++++++ hphp/test/slow/ext_redis/withpersistandid.php | 26 +++++++++++++++++++ .../ext_redis/withpersistandid.php.expect | 3 +++ .../ext_redis/withpersistandid.php.skipif | 12 +++++++++ 7 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 hphp/test/slow/ext_redis/pconnectWithoutPID.php create mode 100644 hphp/test/slow/ext_redis/pconnectWithoutPID.php.expect create mode 100644 hphp/test/slow/ext_redis/pconnectWithoutPID.php.skipif create mode 100644 hphp/test/slow/ext_redis/withpersistandid.php create mode 100644 hphp/test/slow/ext_redis/withpersistandid.php.expect create mode 100644 hphp/test/slow/ext_redis/withpersistandid.php.skipif diff --git a/hphp/system/php/redis/Redis.php b/hphp/system/php/redis/Redis.php index a0dd0c355fba8..628c1b6e1e7f7 100644 --- a/hphp/system/php/redis/Redis.php +++ b/hphp/system/php/redis/Redis.php @@ -1568,9 +1568,7 @@ protected function doConnect($host, $persistent_id, $retry_interval, $persistent = false) { - if (!empty($persistent_id)) { - throw new RedisException("Named persistent connections not supported"); - } + if ($port <= 0) { if ((strlen($host) > 0) && ($host[0] == '/')) { @@ -1587,9 +1585,17 @@ protected function doConnect($host, } if ($persistent) { - $conn = pfsockopen($host, $port, $errno, $errstr, $timeout); + if (!empty($persistent_id)) { + $pid = array('id' => array('persistent_id' => $persistent_id)); + $context = stream_context_create($pid); + $sok = $host . ':' . $port; + $conn = stream_socket_client( + $sok, $errno, $errstr, $timeout, 2, $context); + } else { + $conn = pfsockopen($host, $port, $errno, $errstr, $timeout); + } } else { - $conn = fsockopen($host, $port, $errno, $errstr, $timeout); + $conn = fsockopen($host, $port, $errno, $errstr, $timeout); } $this->last_connect = time(); $this->host = $host; @@ -1597,6 +1603,7 @@ protected function doConnect($host, $this->retry_interval = $retry_interval; $this->timeout_connect = $timeout; $this->persistent = $persistent; + $this->persistent_id = $persistent_id; $this->connection = $conn; $this->dbNumber = 0; $this->commands = []; diff --git a/hphp/test/slow/ext_redis/pconnectWithoutPID.php b/hphp/test/slow/ext_redis/pconnectWithoutPID.php new file mode 100644 index 0000000000000..47a3bb1457146 --- /dev/null +++ b/hphp/test/slow/ext_redis/pconnectWithoutPID.php @@ -0,0 +1,22 @@ + 0 + ); + $r = new Redis(); + $conn = $r->pconnect(REDIS_HOST, REDIS_PORT, $expecting['timeout']); + var_dump($conn); + $authok = REDIS_PASS ? $r->auth(REDIS_PASS) : true; + var_dump($authok); + return $r; +} +$r = NewRedisTestInstance(); +if ($r) echo true; diff --git a/hphp/test/slow/ext_redis/pconnectWithoutPID.php.expect b/hphp/test/slow/ext_redis/pconnectWithoutPID.php.expect new file mode 100644 index 0000000000000..3346a9e960b2f --- /dev/null +++ b/hphp/test/slow/ext_redis/pconnectWithoutPID.php.expect @@ -0,0 +1,3 @@ +bool(true) +bool(true) +1 \ No newline at end of file diff --git a/hphp/test/slow/ext_redis/pconnectWithoutPID.php.skipif b/hphp/test/slow/ext_redis/pconnectWithoutPID.php.skipif new file mode 100644 index 0000000000000..60e41c65d73bd --- /dev/null +++ b/hphp/test/slow/ext_redis/pconnectWithoutPID.php.skipif @@ -0,0 +1,12 @@ + 0, + 'database' => 0 + ); + $r = new Redis(); + $persistentId = REDIS_PORT . $expecting['timeout'] . $expecting['database']; + $conn = $r->pconnect( + REDIS_HOST, REDIS_PORT, $expecting['timeout'], $persistentId); + var_dump($conn); + $authok = REDIS_PASS ? $r->auth(REDIS_PASS) : true; + var_dump($authok); + return $r; +} + +$r = NewRedisTestInstance(); +if ($r) echo true; diff --git a/hphp/test/slow/ext_redis/withpersistandid.php.expect b/hphp/test/slow/ext_redis/withpersistandid.php.expect new file mode 100644 index 0000000000000..3346a9e960b2f --- /dev/null +++ b/hphp/test/slow/ext_redis/withpersistandid.php.expect @@ -0,0 +1,3 @@ +bool(true) +bool(true) +1 \ No newline at end of file diff --git a/hphp/test/slow/ext_redis/withpersistandid.php.skipif b/hphp/test/slow/ext_redis/withpersistandid.php.skipif new file mode 100644 index 0000000000000..60e41c65d73bd --- /dev/null +++ b/hphp/test/slow/ext_redis/withpersistandid.php.skipif @@ -0,0 +1,12 @@ +