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

Commit 9b30536

Browse files
authored
Merge pull request #2 from glensc/connect-timeout
implement connect timeout parameter
2 parents d70f567 + 64d8149 commit 9b30536

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

rdb/Connection.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,28 @@ class Connection extends DatumConverter
2222
private $password;
2323
private $activeTokens;
2424
private $timeout;
25+
private $connectTimeout;
2526
private $ssl;
2627

2728
public $defaultDbName;
2829

30+
/**
31+
* @param array|string $optsOrHost
32+
* @param int $port
33+
* @param string $db
34+
* @param string $apiKey
35+
* @param int $timeout
36+
* @param int $connectTimeout
37+
* @throws RqlDriverError
38+
* @throws \Exception
39+
*/
2940
public function __construct(
3041
$optsOrHost = null,
3142
$port = null,
3243
$db = null,
3344
$apiKey = null,
34-
$timeout = null
45+
$timeout = null,
46+
$connectTimeout = null
3547
) {
3648
if (is_array($optsOrHost)) {
3749
$opts = $optsOrHost;
@@ -67,6 +79,9 @@ public function __construct(
6779
if (isset($opts['timeout'])) {
6880
$timeout = $opts['timeout'];
6981
}
82+
if (isset($opts['connectTimeout'])) {
83+
$connectTimeout = $opts['connectTimeout'];
84+
}
7085
if (isset($opts['ssl'])) {
7186
$ssl = $opts['ssl'];
7287
}
@@ -106,6 +121,7 @@ public function __construct(
106121
$this->user = $user;
107122
$this->password = $password;
108123
$this->timeout = null;
124+
$this->connectTimeout = $connectTimeout ?: ini_get("default_socket_timeout");
109125
$this->ssl = $ssl;
110126

111127
if (isset($db)) {
@@ -430,6 +446,10 @@ private function applyTimeout($timeout)
430446
}
431447
}
432448

449+
/**
450+
* @throws RqlDriverError
451+
* @throws \Exception
452+
*/
433453
private function connect()
434454
{
435455
if ($this->isOpen()) {
@@ -446,12 +466,14 @@ private function connect()
446466
"ssl://" . $this->host . ":" . $this->port,
447467
$errno,
448468
$errstr,
449-
ini_get("default_socket_timeout"),
469+
$this->connectTimeout,
450470
STREAM_CLIENT_CONNECT,
451471
$context
452472
);
453473
} else {
454-
$this->socket = stream_socket_client("tcp://" . $this->host . ":" . $this->port, $errno, $errstr);
474+
$this->socket = stream_socket_client("tcp://" . $this->host . ":" . $this->port, $errno, $errstr,
475+
$this->connectTimeout
476+
);
455477
}
456478
if ($errno != 0 || $this->socket === false) {
457479
$this->socket = null;

rdb/global.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,20 @@
8282

8383
// ------------- Global functions in namespace r -------------
8484

85-
function connect($optsOrHost = null, $port = null, $db = null, $apiKey = null, $timeout = null)
86-
{
87-
return new Connection($optsOrHost, $port, $db, $apiKey, $timeout);
85+
/**
86+
* @param array|string $optsOrHost
87+
* @param int $port
88+
* @param string $db
89+
* @param string $apiKey
90+
* @param int $timeout
91+
* @param int $connectTimeout
92+
* @return Connection
93+
* @throws RqlDriverError
94+
* @throws \Exception
95+
*/
96+
function connect($optsOrHost = null, $port = null, $db = null, $apiKey = null, $timeout = null, $connectTimeout = null)
97+
{
98+
return new Connection($optsOrHost, $port, $db, $apiKey, $timeout, $connectTimeout);
8899
}
89100

90101
function db($dbName)

0 commit comments

Comments
 (0)