Skip to content

Commit d18516b

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 a308d11 commit d18516b

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

packages/ingestion/src/ingestionClient.ts

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ export function createIngestionClient({
268268
requestOptions?: RequestOptions,
269269
): Promise<Array<WatchResponse>> {
270270
let records: Array<PushTaskRecords> = [];
271+
let offset = 0;
271272
const responses: Array<WatchResponse> = [];
273+
const waitBatchSize = Math.floor(batchSize / 10) || batchSize;
272274

273275
const objectEntries = objects.entries();
274276
for (const [i, obj] of objectEntries) {
@@ -279,44 +281,48 @@ export function createIngestionClient({
279281
);
280282
records = [];
281283
}
282-
}
283-
284-
let retryCount = 0;
285-
286-
if (waitForTasks) {
287-
for (const resp of responses) {
288-
if (!resp.eventID) {
289-
throw new Error('received unexpected response from the push endpoint, eventID must not be undefined');
290-
}
291284

292-
await createIterablePromise({
293-
func: async () => {
294-
if (resp.eventID === undefined || !resp.eventID) {
295-
throw new Error('received unexpected response from the push endpoint, eventID must not be undefined');
296-
}
297-
298-
return this.getEvent({ runID: resp.runID, eventID: resp.eventID }).catch((error: ApiError) => {
299-
if (error.status === 404) {
300-
return undefined;
285+
if (
286+
waitForTasks &&
287+
responses.length > 0 &&
288+
(responses.length % waitBatchSize === 0 || i === objects.length - 1)
289+
) {
290+
for (const resp of responses.slice(offset, offset + waitBatchSize)) {
291+
if (!resp.eventID) {
292+
throw new Error('received unexpected response from the push endpoint, eventID must not be undefined');
293+
}
294+
295+
let retryCount = 0;
296+
297+
await createIterablePromise({
298+
func: async () => {
299+
if (resp.eventID === undefined || !resp.eventID) {
300+
throw new Error('received unexpected response from the push endpoint, eventID must not be undefined');
301301
}
302302

303-
throw error;
304-
});
305-
},
306-
validate: (response) => response !== undefined,
307-
aggregator: () => (retryCount += 1),
308-
error: {
309-
validate: () => retryCount >= 50,
310-
message: () => `The maximum number of retries exceeded. (${retryCount}/${50})`,
311-
},
312-
timeout: (): number => Math.min(retryCount * 500, 5000),
313-
});
303+
return this.getEvent({ runID: resp.runID, eventID: resp.eventID }).catch((error: ApiError) => {
304+
if (error.status === 404) {
305+
return undefined;
306+
}
307+
308+
throw error;
309+
});
310+
},
311+
validate: (response) => response !== undefined,
312+
aggregator: () => (retryCount += 1),
313+
error: {
314+
validate: () => retryCount >= 50,
315+
message: () => `The maximum number of retries exceeded. (${retryCount}/${50})`,
316+
},
317+
timeout: (): number => Math.min(retryCount * 500, 5000),
318+
});
319+
}
320+
offset += waitBatchSize;
314321
}
315322
}
316323

317324
return responses;
318325
},
319-
320326
/**
321327
* Creates a new authentication resource.
322328
*

0 commit comments

Comments
 (0)