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
20 changes: 10 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions src/Database/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Utopia\Database;

use Swoole\Database\DetectsLostConnections;

class Connection
{
/**
* @var array<string>
*/
protected static array $errors = [
'Max connect timeout reached'
];

/**
* Check if the given throwable was caused by a database connection error.
*
* @param \Throwable $e
* @return bool
*/
public static function hasError(\Throwable $e): bool
{
/** @phpstan-ignore-next-line can't find static method */
if (DetectsLostConnections::causedByLostConnection($e)) {
return true;
}

$message = $e->getMessage();
foreach (static::$errors as $needle) {
if (\mb_strpos($message, $needle) !== false) {
return true;
}
}

return false;
}
}
4 changes: 1 addition & 3 deletions src/Database/PDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Utopia\Database;

use Swoole\Database\DetectsLostConnections;
use Utopia\CLI\Console;

/**
Expand Down Expand Up @@ -45,8 +44,7 @@ public function __call(string $method, array $args): mixed
try {
return $this->pdo->{$method}(...$args);
} catch (\Throwable $e) {
/** @phpstan-ignore-next-line can't find static method */
if (DetectsLostConnections::causedByLostConnection($e)) {
if (Connection::hasError($e)) {
Console::warning('[Database] Lost connection detected. Reconnecting...');
$this->reconnect();
return $this->pdo->{$method}(...$args);
Expand Down