Skip to content

Commit 35064be

Browse files
committed
Add microsecond timeouts to bedrock client
1 parent 289964d commit 35064be

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/Client.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,21 @@ class Client implements LoggerAwareInterface
9090
*/
9191
private $connectionTimeout;
9292

93+
/**
94+
* @var int Microsecond timeout for connecting to the server.
95+
*/
96+
private $connectionTimeoutMicroseconds;
97+
9398
/**
9499
* @var int Timeout for reading the response from the server.
95100
*/
96101
private $readTimeout;
97102

103+
/**
104+
* @var int Microsecond timeout for reading the response from the server.
105+
*/
106+
private $readTimeoutMicroseconds;
107+
98108
/**
99109
* @var int Timeout to pass to bedrock.
100110
*/
@@ -149,7 +159,9 @@ class Client implements LoggerAwareInterface
149159
* array|null mainHostConfigs List of hosts to attempt first
150160
* array|null failovers List of hosts to use as failovers
151161
* int|null connectionTimeout Timeout to use when connecting
162+
* int|null connectionTimeoutMicroseconds Microsecond timeout to use when connecting
152163
* int|null readTimeout Timeout to use when reading
164+
* int|null readTimeoutMicroseconds Microsecond timeout to use when reading
153165
* int|null bedrockTimeout Timeout to use for bedrock commands
154166
* LoggerInterface|null logger Class to use for logging
155167
* StatsInterface|null stats Class to use for statistics tracking
@@ -167,7 +179,9 @@ private function __construct(array $config = [])
167179
$this->mainHostConfigs = $config['mainHostConfigs'];
168180
$this->failoverHostConfigs = $config['failoverHostConfigs'];
169181
$this->connectionTimeout = $config['connectionTimeout'];
182+
$this->connectionTimeoutMicroseconds = $config['connectionTimeoutMicroseconds'];
170183
$this->readTimeout = $config['readTimeout'];
184+
$this->readTimeoutMicroseconds = $config['readTimeoutMicroseconds'];
171185
$this->bedrockTimeout = $config['bedrockTimeout'];
172186
$this->logger = $config['logger'];
173187
$this->stats = $config['stats'];
@@ -241,7 +255,9 @@ public static function configure(array $config)
241255
'mainHostConfigs' => ['localhost' => ['blacklistedUntil' => 0, 'port' => 8888]],
242256
'failoverHostConfigs' => ['localhost' => ['blacklistedUntil' => 0, 'port' => 8888]],
243257
'connectionTimeout' => 1,
258+
'connectionTimeoutMicroseconds' => 0,
244259
'readTimeout' => 120,
260+
'readTimeoutMicroseconds' => 0,
245261
'bedrockTimeout' => 110,
246262
'logger' => new NullLogger(),
247263
'stats' => new NullStats(),
@@ -531,8 +547,8 @@ private function sendRawRequest(string $host, int $port, string $rawRequest)
531547
}
532548

533549
// Configure this socket and try to connect to it
534-
socket_set_option($this->socket, SOL_SOCKET, SO_SNDTIMEO, ['sec' => $this->connectionTimeout, 'usec' => 0]);
535-
socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, ['sec' => $this->readTimeout, 'usec' => 0]);
550+
socket_set_option($this->socket, SOL_SOCKET, SO_SNDTIMEO, ['sec' => $this->connectionTimeout, 'usec' => $this->connectionTimeoutMicroseconds]);
551+
socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, ['sec' => $this->readTimeout, 'usec' => $this->readTimeoutMicroseconds]);
536552
@socket_connect($this->socket, $host, $port);
537553
$socketErrorCode = socket_last_error($this->socket);
538554
if ($socketErrorCode === 115) {

0 commit comments

Comments
 (0)