Skip to content

Commit

Permalink
fix(push-collector): max-execution-time issue (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
jokesterfr authored Mar 21, 2024
1 parent da92efd commit e319eb1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
35 changes: 26 additions & 9 deletions src/Api/CollectorApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ class CollectorApiClient
*/
private $jwt;

/**
* Default maximum execution time in seconds
*
* @see https://www.php.net/manual/en/info.configuration.php#ini.max-execution-time
*
* @var int
*/
private static $DEFAULT_MAX_EXECUTION_TIME = 30;

/**
* @param PsAccounts $psAccounts
* @param string $collectorApiUrl
Expand All @@ -42,7 +51,8 @@ public function __construct($psAccounts, $collectorApiUrl, $module)
}

/**
* @see https://docs.guzzlephp.org/en/stable/quickstart.html-
* @see https://docs.guzzlephp.org/en/stable/quickstart.html
* @see https://docs.guzzlephp.org/en/stable/request-options.html#read-timeout
*
* @param int $startTime @optional start time in seconds since epoch
*
Expand All @@ -52,8 +62,9 @@ private function getClient(int $startTime = null)
{
return (new ClientFactory())->getClient([
'allow_redirects' => true,
'connect_timeout' => 3,
'connect_timeout' => 10,
'http_errors' => false,
'read_timeout' => 30,
'timeout' => $this->getRemainingTime($startTime),
]);
}
Expand Down Expand Up @@ -155,6 +166,15 @@ public function uploadDelete(string $jobId, string $data, int $startTime)
*/
private function getRemainingTime(int $startTime = null)
{
/**
* Negative remaining time means an immediate timeout (0 means infinity)
*
* @see https://docs.guzzlephp.org/en/stable/request-options.html?highlight=timeout#timeout
*/
$maxExecutionTime = (int) ini_get('max_execution_time');
if ($maxExecutionTime <= 0) {
return CollectorApiClient::$DEFAULT_MAX_EXECUTION_TIME;
}
/*
* An extra 1.5s to be arbitrary substracted
* to keep time for the JSON parsing and state propagation in MySQL
Expand All @@ -165,17 +185,14 @@ private function getRemainingTime(int $startTime = null)
* Default to maximum timeout
*/
if (is_null($startTime)) {
return (int) ini_get('max_execution_time') - $extraOpsTime;
return $maxExecutionTime - $extraOpsTime;
}

$remainingTime = (int) ini_get('max_execution_time') - $extraOpsTime - (time() - $startTime);
$remainingTime = $maxExecutionTime - $extraOpsTime - (time() - $startTime);

/*
* Negative remaining time means an immediate timeout (0 means infinity)
* @see https://docs.guzzlephp.org/en/stable/request-options.html?highlight=timeout#timeout
*/
// A protection that might never be used, but who knows
if ($remainingTime <= 0) {
return 0.1;
return CollectorApiClient::$DEFAULT_MAX_EXECUTION_TIME;
}

return $remainingTime;
Expand Down
2 changes: 1 addition & 1 deletion src/Api/LiveSyncApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private function getClient($timeout = Config::SYNC_API_MAX_TIMEOUT)
{
return (new ClientFactory())->getClient([
'allow_redirects' => true,
'connect_timeout' => 3,
'connect_timeout' => 10,
'http_errors' => false,
'timeout' => $timeout,
]);
Expand Down
2 changes: 1 addition & 1 deletion src/Api/SyncApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private function getClient($timeout = Config::SYNC_API_MAX_TIMEOUT)
{
return (new ClientFactory())->getClient([
'allow_redirects' => true,
'connect_timeout' => 3,
'connect_timeout' => 10,
'http_errors' => false,
'timeout' => $timeout,
]);
Expand Down

0 comments on commit e319eb1

Please sign in to comment.