Skip to content

Commit f0942f7

Browse files
fix(clients): reduce chances of Push rate limiting (generated)
algolia/api-clients-automation#5153 Co-authored-by: algolia-bot <[email protected]> Co-authored-by: Clément Vannicatte <[email protected]>
1 parent b44444f commit f0942f7

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

lib/Api/IngestionClient.php

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2861,49 +2861,50 @@ public function chunkedPush(
28612861
$responses = [];
28622862
$records = [];
28632863
$count = 0;
2864+
$offset = 0;
2865+
$waitBatchSize = (int) ($batchSize / 10);
2866+
if ($waitBatchSize < 1) {
2867+
$waitBatchSize = $batchSize;
2868+
}
28642869

28652870
foreach ($objects as $object) {
28662871
$records[] = $object;
28672872
$ok = false;
2873+
++$count;
28682874

2869-
if (sizeof($records) === $batchSize || $count === sizeof($objects) - 1) {
2875+
if (sizeof($records) === $batchSize || $count === sizeof($objects)) {
28702876
$responses[] = $this->push($indexName, ['action' => $action, 'records' => $records], false, $referenceIndexName, $requestOptions);
28712877
$records = [];
28722878
}
28732879

2874-
++$count;
2875-
}
2880+
if ($waitForTasks && !empty($responses) && (0 === sizeof($responses) % $waitBatchSize || $count === sizeof($objects))) {
2881+
$timeoutCalculation = 'Algolia\AlgoliaSearch\Support\Helpers::linearTimeout';
28762882

2877-
if (!empty($records)) {
2878-
$responses[] = $this->push($indexName, ['action' => $action, 'records' => $records], false, $referenceIndexName, $requestOptions);
2879-
}
2883+
foreach (array_slice($responses, $offset, $waitBatchSize) as $response) {
2884+
$retry = 0;
28802885

2881-
if ($waitForTasks && !empty($responses)) {
2882-
$timeoutCalculation = 'Algolia\AlgoliaSearch\Support\Helpers::linearTimeout';
2886+
while ($retry < 50) {
2887+
try {
2888+
$this->getEvent($response['runID'], $response['eventID']);
28832889

2884-
foreach ($responses as $response) {
2885-
$retry = 0;
2890+
$ok = true;
28862891

2887-
while ($retry < 50) {
2888-
try {
2889-
$this->getEvent($response['runID'], $response['eventID']);
2892+
break;
2893+
} catch (NotFoundException $e) {
2894+
// just retry
2895+
}
28902896

2891-
$ok = true;
2892-
2893-
break;
2894-
} catch (NotFoundException $e) {
2895-
// just retry
2897+
++$retry;
2898+
usleep(
2899+
call_user_func_array($timeoutCalculation, [$this->config->getWaitTaskTimeBeforeRetry(), $retry])
2900+
);
28962901
}
28972902

2898-
++$retry;
2899-
usleep(
2900-
call_user_func_array($timeoutCalculation, [$this->config->getWaitTaskTimeBeforeRetry(), $retry])
2901-
);
2902-
}
2903-
2904-
if (false === $ok) {
2905-
throw new ExceededRetriesException('Maximum number of retries (50) exceeded.');
2903+
if (false === $ok) {
2904+
throw new ExceededRetriesException('Maximum number of retries (50) exceeded.');
2905+
}
29062906
}
2907+
$offset = $offset + $waitBatchSize;
29072908
}
29082909
}
29092910

0 commit comments

Comments
 (0)