@@ -22,16 +22,28 @@ class Connection extends DatumConverter
22
22
private $ password ;
23
23
private $ activeTokens ;
24
24
private $ timeout ;
25
+ private $ connectTimeout ;
25
26
private $ ssl ;
26
27
27
28
public $ defaultDbName ;
28
29
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
+ */
29
40
public function __construct (
30
41
$ optsOrHost = null ,
31
42
$ port = null ,
32
43
$ db = null ,
33
44
$ apiKey = null ,
34
- $ timeout = null
45
+ $ timeout = null ,
46
+ $ connectTimeout = null
35
47
) {
36
48
if (is_array ($ optsOrHost )) {
37
49
$ opts = $ optsOrHost ;
@@ -67,6 +79,9 @@ public function __construct(
67
79
if (isset ($ opts ['timeout ' ])) {
68
80
$ timeout = $ opts ['timeout ' ];
69
81
}
82
+ if (isset ($ opts ['connectTimeout ' ])) {
83
+ $ connectTimeout = $ opts ['connectTimeout ' ];
84
+ }
70
85
if (isset ($ opts ['ssl ' ])) {
71
86
$ ssl = $ opts ['ssl ' ];
72
87
}
@@ -106,6 +121,7 @@ public function __construct(
106
121
$ this ->user = $ user ;
107
122
$ this ->password = $ password ;
108
123
$ this ->timeout = null ;
124
+ $ this ->connectTimeout = $ connectTimeout ?: ini_get ("default_socket_timeout " );
109
125
$ this ->ssl = $ ssl ;
110
126
111
127
if (isset ($ db )) {
@@ -430,6 +446,10 @@ private function applyTimeout($timeout)
430
446
}
431
447
}
432
448
449
+ /**
450
+ * @throws RqlDriverError
451
+ * @throws \Exception
452
+ */
433
453
private function connect ()
434
454
{
435
455
if ($ this ->isOpen ()) {
@@ -446,12 +466,14 @@ private function connect()
446
466
"ssl:// " . $ this ->host . ": " . $ this ->port ,
447
467
$ errno ,
448
468
$ errstr ,
449
- ini_get ( " default_socket_timeout " ) ,
469
+ $ this -> connectTimeout ,
450
470
STREAM_CLIENT_CONNECT ,
451
471
$ context
452
472
);
453
473
} 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
+ );
455
477
}
456
478
if ($ errno != 0 || $ this ->socket === false ) {
457
479
$ this ->socket = null ;
0 commit comments