Skip to content

Commit c1411c4

Browse files
authored
Merge pull request #29 from utopia-php/feat-client-updates
Add reconnection
2 parents b9c9c2e + c38b5c6 commit c1411c4

1 file changed

Lines changed: 29 additions & 23 deletions

File tree

src/Client.php

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -197,19 +197,12 @@ public function connect(): self
197197
if ($this->client->isConnected()) {
198198
return $this;
199199
}
200-
201-
// Validate connection parameters before attempting connection
202-
$validateConnectionParams = function () {
203-
if (empty($this->host)) {
204-
throw new Exception('MongoDB host cannot be empty');
205-
}
206-
if ($this->port <= 0 || $this->port > 65535) {
207-
throw new Exception('MongoDB port must be between 1 and 65535');
208-
}
209-
};
210-
211-
$validateConnectionParams();
212-
200+
if (empty($this->host)) {
201+
throw new Exception('MongoDB host cannot be empty');
202+
}
203+
if ($this->port <= 0 || $this->port > 65535) {
204+
throw new Exception('MongoDB port must be between 1 and 65535');
205+
}
213206
if (!$this->client->connect($this->host, $this->port)) {
214207
throw new Exception("Failed to connect to MongoDB at {$this->host}:{$this->port}");
215208
}
@@ -261,17 +254,15 @@ public function query(array $command, ?string $db = null): stdClass|array|int
261254
if (is_array($sessionData) && isset($sessionData['id'])) {
262255
$command['lsid'] = $sessionData['id'];
263256
$rawId = $sessionData['id']->id ?? null;
264-
$sessionId = $rawId instanceof \MongoDB\BSON\Binary
265-
? bin2hex($rawId->getData())
266-
: $rawId;
267257
} else {
268258
$command['lsid'] = $sessionData;
269259
$rawId = $sessionData->id ?? null;
270-
$sessionId = $rawId instanceof \MongoDB\BSON\Binary
271-
? bin2hex($rawId->getData())
272-
: $rawId;
273260
}
274261

262+
$sessionId = $rawId instanceof \MongoDB\BSON\Binary
263+
? bin2hex($rawId->getData())
264+
: $rawId;
265+
275266
// Add transaction parameters if session is in transaction
276267
if ($sessionId && isset($this->sessions[$sessionId]) &&
277268
$this->sessions[$sessionId]['state'] === self::TRANSACTION_IN_PROGRESS) {
@@ -346,7 +337,6 @@ public function query(array $command, ?string $db = null): stdClass|array|int
346337
$message = pack('V*', 21 + strlen($sections), $this->id, 0, 2013, 0) . "\0" . $sections;
347338
$result = $this->send($message);
348339

349-
// Update causal consistency timestamps from response
350340
$this->updateCausalConsistency($result);
351341

352342
// Update session last use time if session was provided
@@ -367,7 +357,23 @@ public function query(array $command, ?string $db = null): stdClass|array|int
367357
*/
368358
public function send(mixed $data): stdClass|array|int
369359
{
370-
$this->client->send($data);
360+
// Check if connection is alive, connect if not
361+
if (!$this->client->isConnected()) {
362+
$this->connect();
363+
}
364+
365+
$result = $this->client->send($data);
366+
367+
// If send fails, try to reconnect once
368+
if ($result === false) {
369+
$this->close();
370+
$this->connect();
371+
$result = $this->client->send($data);
372+
if ($result === false) {
373+
throw new Exception('Failed to send data to MongoDB after reconnection attempt');
374+
}
375+
}
376+
371377
return $this->receive();
372378
}
373379

@@ -397,8 +403,8 @@ private function receive(): stdClass|array|int
397403
if ($this->client instanceof CoroutineClient) {
398404
Coroutine::sleep(0.001); // 1ms for coroutines
399405
} else {
400-
\usleep($sleepTime); // Microsecond precision for sync client
401-
$sleepTime = \min($sleepTime * 1.2, 10000); // Cap at 10ms for faster checking
406+
\usleep((int)$sleepTime); // Microsecond precision for sync client
407+
$sleepTime = (int)\min($sleepTime * 1.2, 10000); // Cap at 10ms for faster checking
402408
}
403409
continue;
404410
}

0 commit comments

Comments
 (0)