Skip to content

Commit f03de9b

Browse files
authored
[LOPS-1951] Avoid hitting APIs so hard. (#2525)
* Add retry backoff. * Restrict values for maxRetries and retryBackoff. * Workflow processing should not hit api that hard. * Workflow polling should be 5s default. * Remove unneeded changes.
1 parent ee00b7a commit f03de9b

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/Commands/WorkflowProcessingTrait.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ public function processWorkflow(Workflow $workflow): ?Workflow
2424
$progressBar = $this->getContainer()->get($nickname);
2525
return $progressBar->cycle();
2626
}
27-
$retry_interval = $this->getConfig()->get('http_retry_delay_ms', 100);
27+
$retry_interval = $this->getConfig()->get('workflow_polling_delay_ms', 5000);
28+
if ($retry_interval < 1000) {
29+
// The API will not allow polling faster than once per second.
30+
$retry_interval = 1000;
31+
}
2832
do {
2933
$workflow->fetch();
3034
usleep($retry_interval * 1000);

src/ProgressBars/WorkflowProgressBar.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ public function cycle()
4747
}
4848
}
4949

50+
/**
51+
* Sleeps to prevent spamming the API.
52+
*/
53+
protected function sleep()
54+
{
55+
$retry_interval = $this->getConfig()->get('workflow_polling_delay_ms', 5000);
56+
if ($retry_interval < 1000) {
57+
// The API will not allow polling faster than once per second.
58+
$retry_interval = 1000;
59+
}
60+
usleep($retry_interval * 1000);
61+
}
62+
5063
/**
5164
* Runs a single iteration of the progress bar.
5265
* @return bool

src/Request/Request.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,12 @@ private function getClient($base_uri = null): ClientInterface
176176
private function createRetryDecider(): callable
177177
{
178178
$config = $this->getConfig();
179-
$maxRetries = $config->get('http_max_retries', 5);
179+
$maxRetries = $config->get('http_max_retries', $defaultMaxRetries);
180+
// Cap max retries at 10.
181+
$maxRetries = $maxRetries > 10 ? 10 : $maxRetries;
182+
$retryBackoff = $config->get('http_retry_backoff', 5);
183+
// Retry backoff should be at least 3.
184+
$retryBackoff = $retryBackoff < 3 ? 3 : $retryBackoff;
180185
$logger = $this->logger;
181186
$logWarning = function (string $message) use ($logger) {
182187
if ($this->output()->isVerbose()) {
@@ -191,7 +196,8 @@ private function createRetryDecider(): callable
191196
?Exception $exception = null
192197
) use (
193198
$maxRetries,
194-
$logWarning
199+
$logWarning,
200+
$retryBackoff
195201
) {
196202
$logWarningOnRetry = fn (string $reason) => 0 === $retry
197203
? $logWarning(
@@ -217,6 +223,8 @@ private function createRetryDecider(): callable
217223
// Retry on connection-related exceptions such as "Connection refused" and "Operation timed out".
218224
if ($retry !== $maxRetries) {
219225
$logWarningOnRetry($exception->getMessage());
226+
$logWarning(sprintf("Retrying in %s seconds.", $retryBackoff * ($retry + 1)));
227+
sleep($retryBackoff * ($retry + 1));
220228

221229
return true;
222230
}
@@ -240,6 +248,9 @@ private function createRetryDecider(): callable
240248
['body' => $response->getBody()->getContents()]
241249
);
242250

251+
$logWarning(sprintf("Retrying in %s seconds.", $retryBackoff * ($retry + 1)));
252+
sleep($retryBackoff * ($retry + 1));
253+
243254
return true;
244255
}
245256
}

0 commit comments

Comments
 (0)