Mark connections as unhealthy on lost connection error from DB

This commit is contained in:
Jake Barnby
2025-01-08 18:39:06 +13:00
parent 2c04ace022
commit 71b892132b
2 changed files with 23 additions and 2 deletions
+13 -1
View File
@@ -21,6 +21,7 @@ use Appwrite\Utopia\Response\Filters\V18 as ResponseV18;
use Appwrite\Utopia\View;
use Executor\Executor;
use MaxMind\Db\Reader;
use Swoole\Database\DetectsLostConnections;
use Swoole\Http\Request as SwooleRequest;
use Utopia\App;
use Utopia\CLI\Console;
@@ -28,6 +29,7 @@ use Utopia\Config\Config;
use Utopia\Database\Database;
use Utopia\Database\DateTime;
use Utopia\Database\Document;
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;
@@ -38,6 +40,7 @@ use Utopia\Logger\Adapter\Sentry;
use Utopia\Logger\Log;
use Utopia\Logger\Log\User;
use Utopia\Logger\Logger;
use Utopia\Pools\Connection;
use Utopia\System\System;
use Utopia\Validator\Hostname;
use Utopia\Validator\Text;
@@ -746,7 +749,16 @@ App::error()
->inject('logger')
->inject('log')
->inject('queueForUsage')
->action(function (Throwable $error, App $utopia, Request $request, Response $response, Document $project, ?Logger $logger, Log $log, Usage $queueForUsage) {
->inject('connectionForProject')
->action(function (Throwable $error, App $utopia, Request $request, Response $response, Document $project, ?Logger $logger, Log $log, Usage $queueForUsage, Connection $connectionForProject) {
if (
($error instanceof PDOException || $error instanceof DatabaseException)
&& DetectsLostConnections::causedByLostConnection($error)
) {
// Mark connection as unhealthy so it will be recycled on next reclaim.
$connectionForProject->setHealthy(false);
}
$version = System::getEnv('_APP_VERSION', 'UNKNOWN');
$route = $utopia->getRoute();
$class = \get_class($error);
+10 -1
View File
@@ -413,7 +413,16 @@ $worker
->inject('log')
->inject('pools')
->inject('project')
->action(function (Throwable $error, ?Logger $logger, Log $log, Group $pools, Document $project) use ($queueName) {
->inject('connectionForProject')
->action(function (Throwable $error, ?Logger $logger, Log $log, Group $pools, Document $project, PoolConnection $connectionForProject) use ($queueName) {
if (
($error instanceof PDOException || $error instanceof DatabaseException)
&& DetectsLostConnections::causedByLostConnection($error)
) {
// Mark connection as unhealthy, it will be recycled on next reclaim.
$connectionForProject->setHealthy(false);
}
$pools->reclaim();
$version = System::getEnv('_APP_VERSION', 'UNKNOWN');