Skip to content

Commit

Permalink
Fix #2517,Add pid support for persistent connection in Redis
Browse files Browse the repository at this point in the history
Summary: pfsockopen does not supports id. So I changed it stream_socket_client, to handles ids as well.
Closes #4291

Reviewed By: @fredemmott

Differential Revision: D1693997

Signature: t1:1693997:1417632135:c2b2b2648ee77d911d00e1da801c8698e591c784
  • Loading branch information
hamidre13 authored and hhvm-bot committed Dec 3, 2014
1 parent c3d5fb8 commit 9f5f4f8
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 5 deletions.
17 changes: 12 additions & 5 deletions hphp/system/php/redis/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] == '/')) {
Expand All @@ -1587,16 +1585,25 @@ 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;
$this->port = $port;
$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 = [];
Expand Down
22 changes: 22 additions & 0 deletions hphp/test/slow/ext_redis/pconnectWithoutPID.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
define('REDIS_HOST', getenv('REDIS_TEST_HOST'));
define('REDIS_PORT', getenv('REDIS_TEST_PORT')
? (int)getenv('REDIS_TEST_PORT')
: Redis::DEFAULT_PORT);
define('REDIS_PASS',getenv('REDIS_TEST_PASS')!==null
? getenv('REDIS_TEST_PASS')
: null);

function NewRedisTestInstance() {
$expecting = array(
'timeout' => 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;
3 changes: 3 additions & 0 deletions hphp/test/slow/ext_redis/pconnectWithoutPID.php.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bool(true)
bool(true)
1
12 changes: 12 additions & 0 deletions hphp/test/slow/ext_redis/pconnectWithoutPID.php.skipif
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

if (!getenv('REDIS_TEST_HOST')) {
echo 'skip';
} else {
// Some people might have this variable set, but don't have the server running
$output = array();
exec("redis-cli ping", $output);
if ($output[0] !== "PONG") {
echo 'skip';
}
}
26 changes: 26 additions & 0 deletions hphp/test/slow/ext_redis/withpersistandid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
define('REDIS_HOST', getenv('REDIS_TEST_HOST'));
define('REDIS_PORT', getenv('REDIS_TEST_PORT')
? (int)getenv('REDIS_TEST_PORT')
: Redis::DEFAULT_PORT);
define('REDIS_PASS', getenv('REDIS_TEST_PASS') !== null
? getenv('REDIS_TEST_PASS')
: null);

function NewRedisTestInstance() {
$expecting = array(
'timeout' => 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;
3 changes: 3 additions & 0 deletions hphp/test/slow/ext_redis/withpersistandid.php.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bool(true)
bool(true)
1
12 changes: 12 additions & 0 deletions hphp/test/slow/ext_redis/withpersistandid.php.skipif
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

if (!getenv('REDIS_TEST_HOST')) {
echo 'skip';
} else {
// Some people might have this variable set, but don't have the server running
$output = array();
exec("redis-cli ping", $output);
if ($output[0] !== "PONG") {
echo 'skip';
}
}

0 comments on commit 9f5f4f8

Please sign in to comment.