Skip to content
This repository has been archived by the owner on Feb 5, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2 from glensc/connect-timeout
Browse files Browse the repository at this point in the history
implement connect timeout parameter
  • Loading branch information
ed-glen authored Oct 4, 2017
2 parents d70f567 + 64d8149 commit 9b30536
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
28 changes: 25 additions & 3 deletions rdb/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,28 @@ class Connection extends DatumConverter
private $password;
private $activeTokens;
private $timeout;
private $connectTimeout;
private $ssl;

public $defaultDbName;

/**
* @param array|string $optsOrHost
* @param int $port
* @param string $db
* @param string $apiKey
* @param int $timeout
* @param int $connectTimeout
* @throws RqlDriverError
* @throws \Exception
*/
public function __construct(
$optsOrHost = null,
$port = null,
$db = null,
$apiKey = null,
$timeout = null
$timeout = null,
$connectTimeout = null
) {
if (is_array($optsOrHost)) {
$opts = $optsOrHost;
Expand Down Expand Up @@ -67,6 +79,9 @@ public function __construct(
if (isset($opts['timeout'])) {
$timeout = $opts['timeout'];
}
if (isset($opts['connectTimeout'])) {
$connectTimeout = $opts['connectTimeout'];
}
if (isset($opts['ssl'])) {
$ssl = $opts['ssl'];
}
Expand Down Expand Up @@ -106,6 +121,7 @@ public function __construct(
$this->user = $user;
$this->password = $password;
$this->timeout = null;
$this->connectTimeout = $connectTimeout ?: ini_get("default_socket_timeout");
$this->ssl = $ssl;

if (isset($db)) {
Expand Down Expand Up @@ -430,6 +446,10 @@ private function applyTimeout($timeout)
}
}

/**
* @throws RqlDriverError
* @throws \Exception
*/
private function connect()
{
if ($this->isOpen()) {
Expand All @@ -446,12 +466,14 @@ private function connect()
"ssl://" . $this->host . ":" . $this->port,
$errno,
$errstr,
ini_get("default_socket_timeout"),
$this->connectTimeout,
STREAM_CLIENT_CONNECT,
$context
);
} else {
$this->socket = stream_socket_client("tcp://" . $this->host . ":" . $this->port, $errno, $errstr);
$this->socket = stream_socket_client("tcp://" . $this->host . ":" . $this->port, $errno, $errstr,
$this->connectTimeout
);
}
if ($errno != 0 || $this->socket === false) {
$this->socket = null;
Expand Down
17 changes: 14 additions & 3 deletions rdb/global.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,20 @@

// ------------- Global functions in namespace r -------------

function connect($optsOrHost = null, $port = null, $db = null, $apiKey = null, $timeout = null)
{
return new Connection($optsOrHost, $port, $db, $apiKey, $timeout);
/**
* @param array|string $optsOrHost
* @param int $port
* @param string $db
* @param string $apiKey
* @param int $timeout
* @param int $connectTimeout
* @return Connection
* @throws RqlDriverError
* @throws \Exception
*/
function connect($optsOrHost = null, $port = null, $db = null, $apiKey = null, $timeout = null, $connectTimeout = null)
{
return new Connection($optsOrHost, $port, $db, $apiKey, $timeout, $connectTimeout);
}

function db($dbName)
Expand Down

0 comments on commit 9b30536

Please sign in to comment.