Skip to content

Commit 14078bc

Browse files
authored
Merge pull request repejota#79 from dfeyer/bugfix-timeout
BUGFIX: Connection::setStreamTimeout must support milliseconds
2 parents 883311c + 8a3986a commit 14078bc

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/Nats/Connection.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,12 @@ private function receive($len = null)
190190
* @return resource
191191
* @throws \Exception Exception raised if connection fails.
192192
*/
193-
private function getStream($address, $timeout = null)
193+
private function getStream($address)
194194
{
195-
if (is_null($timeout)) {
196-
$timeout = intval(ini_get('default_socket_timeout'));
197-
}
198195
$errno = null;
199196
$errstr = null;
200197

201-
$fp = stream_socket_client($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
202-
$timeout = number_format($timeout, 3);
203-
$seconds = floor($timeout);
204-
$microseconds = ($timeout - $seconds) * 1000;
205-
stream_set_timeout($fp, $seconds, $microseconds);
198+
$fp = stream_socket_client($address, $errno, $errstr, null, STREAM_CLIENT_CONNECT);
206199

207200
if (!$fp) {
208201
throw new \Exception($errstr, $errno);
@@ -231,9 +224,13 @@ public function isConnected()
231224
*/
232225
public function connect($timeout = null)
233226
{
227+
if ($timeout === null) {
228+
$timeout = intval(ini_get('default_socket_timeout'));
229+
}
234230

235231
$this->timeout = $timeout;
236-
$this->streamSocket = $this->getStream($this->options->getAddress(), $timeout);
232+
$this->streamSocket = $this->getStream($this->options->getAddress());
233+
$this->setStreamTimeout($timeout);
237234

238235
$msg = 'CONNECT '.$this->options;
239236
$this->send($msg);
@@ -447,9 +444,12 @@ public function wait($quantity = 0)
447444
public function setStreamTimeout($seconds)
448445
{
449446
if ($this->isConnected()) {
450-
if (is_int($seconds)) {
447+
if (is_numeric($seconds)) {
451448
try {
452-
return stream_set_timeout($this->streamSocket, $seconds);
449+
$timeout = number_format($seconds, 3);
450+
$seconds = floor($timeout);
451+
$microseconds = ($timeout - $seconds) * 1000;
452+
return stream_set_timeout($this->streamSocket, $seconds, $microseconds);
453453
} catch (\Exception $e) {
454454
return false;
455455
}

0 commit comments

Comments
 (0)