Skip to content

Commit 7a03f05

Browse files
committed
Fix timeout connection
1 parent e3d01f0 commit 7a03f05

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/Nats/Connection.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,16 @@ private function receive($len = null)
190190
* @return resource
191191
* @throws \Exception Exception raised if connection fails.
192192
*/
193-
private function getStream($address)
193+
private function getStream($address, $timeout)
194194
{
195195
$errno = null;
196196
$errstr = null;
197197

198-
$fp = stream_socket_client($address, $errno, $errstr, null, STREAM_CLIENT_CONNECT);
198+
$fp = stream_socket_client($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
199+
$timeout = number_format($timeout, 3);
200+
$seconds = floor($timeout);
201+
$microseconds = ($timeout - $seconds) * 1000;
202+
stream_set_timeout($fp, $seconds, $microseconds);
199203

200204
if (!$fp) {
201205
throw new \Exception($errstr, $errno);
@@ -229,7 +233,7 @@ public function connect($timeout = null)
229233
}
230234

231235
$this->timeout = $timeout;
232-
$this->streamSocket = $this->getStream($this->options->getAddress());
236+
$this->streamSocket = $this->getStream($this->options->getAddress(), $timeout);
233237
$this->setStreamTimeout($timeout);
234238

235239
$msg = 'CONNECT '.$this->options;

0 commit comments

Comments
 (0)