From 389d367fcac0b5050f2e8523ab76a21bf0f3c4e2 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 19 Oct 2023 15:32:45 +1300 Subject: [PATCH] Set hard timeouts for API dbs --- app/config/errors.php | 5 +++++ app/controllers/general.php | 6 +++++- app/init.php | 12 ++++++++---- src/Appwrite/Extend/Exception.php | 1 + 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/app/config/errors.php b/app/config/errors.php index 575a4588ba..88af43afd0 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -466,6 +466,11 @@ return [ 'description' => 'Database already exists', 'code' => 409 ], + Exception::DATABASE_TIMEOUT => [ + 'name' => Exception::DATABASE_TIMEOUT, + 'description' => 'Database timed out.', + 'code' => 408 + ], /** Collections */ Exception::COLLECTION_NOT_FOUND => [ diff --git a/app/controllers/general.php b/app/controllers/general.php index 44fc6ed540..62adab4e86 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -690,7 +690,11 @@ App::error() break; } } elseif ($error instanceof Utopia\Database\Exception\Conflict) { - $error = new AppwriteException(AppwriteException::DOCUMENT_UPDATE_CONFLICT, null, null, $error); + $error = new AppwriteException(AppwriteException::DOCUMENT_UPDATE_CONFLICT, previous: $error); + $code = $error->getCode(); + $message = $error->getMessage(); + } elseif ($error instanceof Utopia\Database\Exception\Timeout) { + $error = new AppwriteException(AppwriteException::DATABASE_TIMEOUT, $message, previous: $error); $code = $error->getCode(); $message = $error->getMessage(); } diff --git a/app/init.php b/app/init.php index 8aec6122fa..f9a237dce2 100644 --- a/app/init.php +++ b/app/init.php @@ -1115,11 +1115,13 @@ App::setResource('dbForProject', function (Group $pools, Database $dbForConsole, $dbAdapter = $pools ->get($project->getAttribute('database')) ->pop() - ->getResource() - ; + ->getResource(); $database = new Database($dbAdapter, $cache); - $database->setNamespace('_' . $project->getInternalId()); + + $database + ->setNamespace('_' . $project->getInternalId()) + ->setTimeout(milliseconds: 15000); return $database; }, ['pools', 'dbForConsole', 'cache', 'project']); @@ -1133,7 +1135,9 @@ App::setResource('dbForConsole', function (Group $pools, Cache $cache) { $database = new Database($dbAdapter, $cache); - $database->setNamespace('_console'); + $database + ->setNamespace('_console') + ->setTimeout(milliseconds: 15000); return $database; }, ['pools', 'cache']); diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 77dc03e310..c7ff2e73c0 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -145,6 +145,7 @@ class Exception extends \Exception /** Databases */ public const DATABASE_NOT_FOUND = 'database_not_found'; public const DATABASE_ALREADY_EXISTS = 'database_already_exists'; + public const DATABASE_TIMEOUT = 'database_timeout'; /** Collections */ public const COLLECTION_NOT_FOUND = 'collection_not_found';