@@ -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 ;
0 commit comments