Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "expensify/bedrock-php",
"description": "Bedrock PHP Library",
"type": "library",
"version": "2.2.5",
"version": "2.2.6",
"authors": [
{
"name": "Expensify",
Expand Down
44 changes: 43 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,12 +567,54 @@ private function sendRawRequest(string $host, int $port, string $rawRequest)
}

// Configure this socket and try to connect to it
// Use non-blocking mode for connection to avoid timeouts. On non-blocking sockets, socket_connect()
// may return immediately for reasons unclear with EINPROGRESS (error 115), allowing us to use socket_select() to wait
// with a proper timeout. This prevents connection attempts from hanging indefinitely.
socket_set_nonblock($this->socket);
socket_set_option($this->socket, SOL_SOCKET, SO_SNDTIMEO, ['sec' => $this->connectionTimeout, 'usec' => $this->connectionTimeoutMicroseconds]);
socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, ['sec' => $this->readTimeout, 'usec' => $this->readTimeoutMicroseconds]);
$connectStart = microtime(true);
@socket_connect($this->socket, $host, $port);
$connectTime = (microtime(true) - $connectStart) * 1000;
$socketErrorCode = socket_last_error($this->socket);
if ($socketErrorCode === 115) {
$this->logger->info('Bedrock\Client - socket_connect returned error 115, continuing.');
$this->logger->info('Bedrock\Client - socket_connect returned error 115, waiting for connection to complete.', [
'host' => $host,
'connectAttemptTimeMs' => round($connectTime, 3),
]);

// Wait for the socket to be ready for writing after EINPROGRESS
$write = [$this->socket];
$read = [];
$except = [];
$selectStart = microtime(true);
$selectResult = socket_select($read, $write, $except, $this->connectionTimeout, $this->connectionTimeoutMicroseconds);

// Time for socket_select call
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB: Missing empty line before comment

$selectTime = (microtime(true) - $selectStart) * 1000;

if ($selectResult === false) {
$socketError = socket_strerror(socket_last_error($this->socket));
throw new ConnectionFailure("socket_select failed after EINPROGRESS for $host:$port. Error: $socketError");
} elseif ($selectResult === 0) {
throw new ConnectionFailure("Socket not ready for writing within timeout after EINPROGRESS for $host:$port");
} elseif (empty($write)) {
$socketErrorCode = socket_last_error($this->socket);
$socketError = socket_strerror($socketErrorCode);
throw new ConnectionFailure("Socket had error after EINPROGRESS for $host:$port. Error: $socketErrorCode $socketError");
}

// Total time from connect to ready
$totalTime = (microtime(true) - $connectStart) * 1000;

// Set socket back to blocking mode for normal operations
socket_set_block($this->socket);

$this->logger->info('Bedrock\Client - Socket ready for writing after EINPROGRESS.', [
'host' => $host,
'totalConnectionTimeMs' => round($totalTime, 3),
'selectWaitTimeMs' => round($selectTime, 3),
]);
} elseif ($socketErrorCode) {
$socketError = socket_strerror($socketErrorCode);
throw new ConnectionFailure("Could not connect to Bedrock host $host:$port. Error: $socketErrorCode $socketError");
Expand Down