From e0e30e3d57160aa6c97970c539618d52c7463fda Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 6 Mar 2025 15:39:55 +1300 Subject: [PATCH 1/3] Add custom connection checker --- src/Database/Connection.php | 37 +++++++++++++++++++++++++++++++++++++ src/Database/PDO.php | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/Database/Connection.php diff --git a/src/Database/Connection.php b/src/Database/Connection.php new file mode 100644 index 000000000..79cb2fc09 --- /dev/null +++ b/src/Database/Connection.php @@ -0,0 +1,37 @@ + + */ + 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 hasDatabaseError(\Throwable $e): bool + { + if (DetectsLostConnections::causedByLostConnection($e)) { + return true; + } + + $message = $e->getMessage(); + foreach (static::$errors as $needle) { + if (\mb_strpos($message, $needle) !== false) { + return true; + } + } + + return false; + } +} diff --git a/src/Database/PDO.php b/src/Database/PDO.php index 2f8ca0f11..b9d4e0a5d 100644 --- a/src/Database/PDO.php +++ b/src/Database/PDO.php @@ -46,7 +46,7 @@ public function __call(string $method, array $args): mixed return $this->pdo->{$method}(...$args); } catch (\Throwable $e) { /** @phpstan-ignore-next-line can't find static method */ - if (DetectsLostConnections::causedByLostConnection($e)) { + if (Connection::hasDatabaseError($e)) { Console::warning('[Database] Lost connection detected. Reconnecting...'); $this->reconnect(); return $this->pdo->{$method}(...$args); From 6ec9f1fe204e662c7d82e0917ade9b2c97118676 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 6 Mar 2025 15:44:41 +1300 Subject: [PATCH 2/3] Fix stan --- composer.lock | 20 ++++++++++---------- src/Database/Connection.php | 1 + src/Database/PDO.php | 2 -- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/composer.lock b/composer.lock index 64dad5f9e..9f1d43f57 100644 --- a/composer.lock +++ b/composer.lock @@ -465,16 +465,16 @@ }, { "name": "open-telemetry/api", - "version": "1.2.2", + "version": "1.2.3", "source": { "type": "git", "url": "https://github.com/opentelemetry-php/api.git", - "reference": "8b925df3047628968bc5be722468db1b98b82d51" + "reference": "199d7ddda88f5f5619fa73463f1a5a7149ccd1f1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/opentelemetry-php/api/zipball/8b925df3047628968bc5be722468db1b98b82d51", - "reference": "8b925df3047628968bc5be722468db1b98b82d51", + "url": "https://api.github.com/repos/opentelemetry-php/api/zipball/199d7ddda88f5f5619fa73463f1a5a7149ccd1f1", + "reference": "199d7ddda88f5f5619fa73463f1a5a7149ccd1f1", "shasum": "" }, "require": { @@ -531,7 +531,7 @@ "issues": "https://github.com/open-telemetry/opentelemetry-php/issues", "source": "https://github.com/open-telemetry/opentelemetry-php" }, - "time": "2025-02-03T21:49:11+00:00" + "time": "2025-03-05T21:42:54+00:00" }, { "name": "open-telemetry/context", @@ -2712,16 +2712,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.12.19", + "version": "1.12.20", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "c42ba9bab7a940ed00092ecb1c77bad98896d789" + "reference": "3240b1972042c7f73cf1045e879ea5bd5f761bb7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/c42ba9bab7a940ed00092ecb1c77bad98896d789", - "reference": "c42ba9bab7a940ed00092ecb1c77bad98896d789", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/3240b1972042c7f73cf1045e879ea5bd5f761bb7", + "reference": "3240b1972042c7f73cf1045e879ea5bd5f761bb7", "shasum": "" }, "require": { @@ -2766,7 +2766,7 @@ "type": "github" } ], - "time": "2025-02-19T15:42:21+00:00" + "time": "2025-03-05T13:37:43+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/src/Database/Connection.php b/src/Database/Connection.php index 79cb2fc09..9c77c21e1 100644 --- a/src/Database/Connection.php +++ b/src/Database/Connection.php @@ -21,6 +21,7 @@ class Connection */ public static function hasDatabaseError(\Throwable $e): bool { + /** @phpstan-ignore-next-line can't find static method */ if (DetectsLostConnections::causedByLostConnection($e)) { return true; } diff --git a/src/Database/PDO.php b/src/Database/PDO.php index b9d4e0a5d..4351ba7c3 100644 --- a/src/Database/PDO.php +++ b/src/Database/PDO.php @@ -2,7 +2,6 @@ namespace Utopia\Database; -use Swoole\Database\DetectsLostConnections; use Utopia\CLI\Console; /** @@ -45,7 +44,6 @@ 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 (Connection::hasDatabaseError($e)) { Console::warning('[Database] Lost connection detected. Reconnecting...'); $this->reconnect(); From 56f58fa0ce797e38f0bb2c7ca17667b01af0d9e2 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 6 Mar 2025 15:46:18 +1300 Subject: [PATCH 3/3] Update name --- src/Database/Connection.php | 2 +- src/Database/PDO.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Database/Connection.php b/src/Database/Connection.php index 9c77c21e1..cbf189df7 100644 --- a/src/Database/Connection.php +++ b/src/Database/Connection.php @@ -19,7 +19,7 @@ class Connection * @param \Throwable $e * @return bool */ - public static function hasDatabaseError(\Throwable $e): bool + public static function hasError(\Throwable $e): bool { /** @phpstan-ignore-next-line can't find static method */ if (DetectsLostConnections::causedByLostConnection($e)) { diff --git a/src/Database/PDO.php b/src/Database/PDO.php index 4351ba7c3..308bb6b51 100644 --- a/src/Database/PDO.php +++ b/src/Database/PDO.php @@ -44,7 +44,7 @@ public function __call(string $method, array $args): mixed try { return $this->pdo->{$method}(...$args); } catch (\Throwable $e) { - if (Connection::hasDatabaseError($e)) { + if (Connection::hasError($e)) { Console::warning('[Database] Lost connection detected. Reconnecting...'); $this->reconnect(); return $this->pdo->{$method}(...$args);