1
0
Fork 0
mirror of synced 2024-07-09 00:16:13 +12:00

Merge pull request #2895 from appwrite/feat-fix-timeout-bugs

feat: fix timeout issues
This commit is contained in:
Torsten Dittmann 2022-03-04 14:22:07 +01:00 committed by GitHub
commit 2a56beae0a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 28 deletions

View file

@ -25,14 +25,14 @@ use Utopia\Swoole\Response;
use Utopia\Validator\ArrayList; use Utopia\Validator\ArrayList;
use Utopia\Validator\Assoc; use Utopia\Validator\Assoc;
use Utopia\Validator\Boolean; use Utopia\Validator\Boolean;
use Utopia\Validator\Range as ValidatorRange; use Utopia\Validator\Range;
use Utopia\Validator\Text; use Utopia\Validator\Text;
Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL); Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL);
/** Constants */ /** Constants */
const MAINTENANCE_INTERVAL = 1200; // 20 minutes const MAINTENANCE_INTERVAL = 3600; // 3600 seconds = 1 hour
/** /**
* Create a Swoole table to store runtime information * Create a Swoole table to store runtime information
@ -395,9 +395,9 @@ App::delete('/v1/runtimes/:runtimeId')
App::post('/v1/execution') App::post('/v1/execution')
->desc('Create an execution') ->desc('Create an execution')
->param('runtimeId', '', new Text(64), 'The runtimeID to execute') ->param('runtimeId', '', new Text(64), 'The runtimeID to execute')
->param('vars', [], new Assoc(), 'Environment variables required for the build', false) ->param('vars', [], new Assoc(), 'Environment variables required for the build')
->param('data', '', new Text(8192), 'Data to be forwarded to the function, this is user specified.', true) ->param('data', '{}', new Text(8192), 'Data to be forwarded to the function, this is user specified.', true)
->param('timeout', 15, new ValidatorRange(1, 900), 'Function maximum execution time in seconds.', true) ->param('timeout', 15, new Range(1, (int) App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900)), 'Function maximum execution time in seconds.')
->inject('activeRuntimes') ->inject('activeRuntimes')
->inject('response') ->inject('response')
->action( ->action(
@ -423,17 +423,19 @@ App::post('/v1/execution')
$errNo = -1; $errNo = -1;
$executorResponse = ''; $executorResponse = '';
$timeout ??= (int) App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900);
$ch = \curl_init(); $ch = \curl_init();
$body = \json_encode([ $body = \json_encode([
'env' => $vars, 'env' => $vars,
'payload' => $data, 'payload' => $data,
'timeout' => $timeout ?? (int) App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900) 'timeout' => $timeout
]); ]);
\curl_setopt($ch, CURLOPT_URL, "http://" . $runtimeId . ":3000/"); \curl_setopt($ch, CURLOPT_URL, "http://" . $runtimeId . ":3000/");
\curl_setopt($ch, CURLOPT_POST, true); \curl_setopt($ch, CURLOPT_POST, true);
\curl_setopt($ch, CURLOPT_POSTFIELDS, $body); \curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
\curl_setopt($ch, CURLOPT_TIMEOUT, $timeout ?? (int) App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900)); \curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
\curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); \curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
\curl_setopt($ch, CURLOPT_HTTPHEADER, [ \curl_setopt($ch, CURLOPT_HTTPHEADER, [

View file

@ -82,7 +82,9 @@ class Executor
'commands' => $commands 'commands' => $commands
]; ];
$response = $this->call(self::METHOD_POST, $route, $headers, $params, true, App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900)); $timeout = (int) App::getEnv('_APP_FUNCTIONS_BUILD_TIMEOUT', 900);
$response = $this->call(self::METHOD_POST, $route, $headers, $params, true, $timeout);
$status = $response['headers']['status-code']; $status = $response['headers']['status-code'];
if ($status >= 400) { if ($status >= 400) {
@ -159,13 +161,18 @@ class Executor
'timeout' => $timeout, 'timeout' => $timeout,
]; ];
$response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); /* Add 2 seconds as a buffer to the actual timeout value since there can be a slight variance*/
$requestTimeout = $timeout + 2;
$response = $this->call(self::METHOD_POST, $route, $headers, $params, true, $requestTimeout);
$status = $response['headers']['status-code']; $status = $response['headers']['status-code'];
if ($status >= 400) { for ($attempts = 0; $attempts < 10; $attempts++) {
for ($attempts = 0; $attempts < 10; $attempts++) { try {
switch ($status) { switch (true) {
case 404: case $status < 400:
return $response['body'];
case $status === 404:
$response = $this->createRuntime( $response = $this->createRuntime(
deploymentId: $deploymentId, deploymentId: $deploymentId,
projectId: $projectId, projectId: $projectId,
@ -176,31 +183,23 @@ class Executor
entrypoint: $entrypoint, entrypoint: $entrypoint,
commands: [] commands: []
); );
$response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, $requestTimeout);
$status = $response['headers']['status-code']; $status = $response['headers']['status-code'];
break; break;
case 406: case $status === 406:
$response = $this->call(self::METHOD_POST, $route, $headers, $params, true, 30); $response = $this->call(self::METHOD_POST, $route, $headers, $params, true, $requestTimeout);
$status = $response['headers']['status-code']; $status = $response['headers']['status-code'];
break; break;
default: default:
throw new \Exception($response['body']['message'], $status); throw new \Exception($response['body']['message'], $status);
} }
} catch (\Exception $e) {
if ($status < 400) { throw new \Exception($e->getMessage(), $e->getCode());
return $response['body'];
}
if ($status != 406) {
throw new \Exception($response['body']['message'], $status);
}
sleep(2);
} }
throw new Exception($response['body']['message'], 503); sleep(2);
} }
return $response['body']; throw new Exception($response['body']['message'], 503);
} }
/** /**