Skip to content

Commit 5bb0185

Browse files
committed
Fix floating timeouts
1 parent 1ec80b4 commit 5bb0185

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

app/http.php

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -571,14 +571,10 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
571571
'duration' => $duration,
572572
]);
573573

574-
$activeRuntimes->set($runtimeName, [
575-
'name' => $runtimeName,
576-
'hostname' => $runtimeHostname,
577-
'created' => $startTime,
578-
'updated' => \microtime(true),
579-
'status' => 'Up ' . \round($duration, 2) . 's',
580-
'key' => $secret,
581-
]);
574+
$activeRuntime = $activeRuntimes->get($runtimeName);
575+
$activeRuntime['updated'] = \microtime(true);
576+
$activeRuntime['status'] = 'Up ' . \round($duration, 2) . 's';
577+
$activeRuntimes->set($runtimeName, $activeRuntime);
582578
} catch (Throwable $th) {
583579
$error = $th->getMessage() . $output;
584580

@@ -857,12 +853,7 @@ function (string $runtimeId, ?string $payload, string $path, string $method, arr
857853
throw new Exception('Runtime secret not found. Please re-create the runtime.', 500);
858854
}
859855

860-
$startTime = \microtime(true);
861-
862-
$executeV2 = function () use ($variables, $payload, $secret, $hostname, &$startTime, $timeout): array {
863-
// Restart execution timer to not could failed attempts
864-
$startTime = \microtime(true);
865-
856+
$executeV2 = function () use ($variables, $payload, $secret, $hostname, $timeout): array {
866857
$statusCode = 0;
867858
$errNo = -1;
868859
$executorResponse = '';
@@ -935,10 +926,7 @@ function (string $runtimeId, ?string $payload, string $path, string $method, arr
935926
];
936927
};
937928

938-
$executeV3 = function () use ($path, $method, $headers, $payload, $secret, $hostname, &$startTime, $timeout): array {
939-
// Restart execution timer to not could failed attempts
940-
$startTime = \microtime(true);
941-
929+
$executeV3 = function () use ($path, $method, $headers, $payload, $secret, $hostname, $timeout): array {
942930
$statusCode = 0;
943931
$errNo = -1;
944932
$executorResponse = '';
@@ -973,7 +961,7 @@ function (string $runtimeId, ?string $payload, string $path, string $method, arr
973961
\curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
974962

975963
$headers['x-open-runtimes-secret'] = $secret;
976-
$headers['x-open-runtimes-timeout'] = $timeout;
964+
$headers['x-open-runtimes-timeout'] = \max(\intval($timeout), 1);
977965
$headersArr = [];
978966
foreach ($headers as $key => $value) {
979967
$headersArr[] = $key . ': ' . $value;
@@ -1027,6 +1015,9 @@ function (string $runtimeId, ?string $payload, string $path, string $method, arr
10271015
];
10281016
};
10291017

1018+
// From here we calculate billable duration of execution
1019+
$startTime = \microtime(true);
1020+
10301021
$listening = $runtime['listening'];
10311022

10321023
if (!$listening) {
@@ -1052,10 +1043,11 @@ function (string $runtimeId, ?string $payload, string $path, string $method, arr
10521043
$runtime = $activeRuntimes->get($runtimeName);
10531044
$runtime['listening'] = true;
10541045
$activeRuntimes->set($runtimeName, $runtime);
1046+
1047+
// Lower timeout by time it took to cold-start
1048+
$timeout -= (\microtime(true) - $pingStart);
10551049
}
10561050

1057-
// Lower timeout by time it took to cold-start
1058-
$timeout -= (\microtime(true) - $startTime);
10591051

10601052
// Execute function
10611053
for ($i = 0; $i < 10; $i++) {

0 commit comments

Comments
 (0)