Merge pull request #9213 from appwrite/fix-dead-connections

Fix dead connections
This commit is contained in:
Jake Barnby
2025-01-15 15:27:42 +13:00
parent 7adf81708c
commit 3f620bbc44
4 changed files with 78 additions and 28 deletions
+11
View File
@@ -5,6 +5,7 @@ require_once __DIR__ . '/../vendor/autoload.php';
use Appwrite\Utopia\Request;
use Appwrite\Utopia\Response;
use Swoole\Constant;
use Swoole\Database\DetectsLostConnections;
use Swoole\Http\Request as SwooleRequest;
use Swoole\Http\Response as SwooleResponse;
use Swoole\Http\Server;
@@ -17,6 +18,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\Exception\Duplicate;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Permission;
@@ -346,6 +348,15 @@ $http->on(Constant::EVENT_REQUEST, function (SwooleRequest $swooleRequest, Swool
$app->run($request, $response);
} catch (\Throwable $th) {
if (
($th instanceof PDOException || $th instanceof DatabaseException)
&& DetectsLostConnections::causedByLostConnection($th)
) {
// Mark connection as unhealthy so it will be recycled on next reclaim.
$connectionForProject = $app->getResource('connectionForProject');
$connectionForProject->setHealthy(false);
}
$version = System::getEnv('_APP_VERSION', 'UNKNOWN');
$logger = $app->getResource("logger");