Skip to content

Commit 4c4e3b8

Browse files
alyssachvastacopybara-github
authored andcommitted
Internal change
GitOrigin-RevId: ad019433d3e4e4cc59294b7ddcda3db429b4a231
1 parent 48be32b commit 4c4e3b8

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/tasks/summarization_subtasks/recursive_summarization.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// Functions for different ways to summarize Comment and Vote data.
1616

1717
import { Model } from "../../models/model";
18+
import { MAX_RETRIES } from "../../models/vertex_model";
1819
import { SummaryContent } from "../../types";
1920

2021
export abstract class RecursiveSummary<InputType> {
@@ -34,9 +35,10 @@ export abstract class RecursiveSummary<InputType> {
3435

3536
/**
3637
* Resolves Promises sequentially, optionally using batching for limited parallelization.
38+
* Adds a one-second backoff for failed calls.
3739
*
38-
* Batching can be used to execute mutiple promises in parallel that will then be resolved in
39-
* order. The batchSize can be though of as the maximum number of parallel threads.
40+
* Batching can be used to execute multiple promises in parallel that will then be resolved in
41+
* order. The batchSize can be thought of as the maximum number of parallel threads.
4042
* @param promises the promises to resolve.
4143
* @param numParallelExecutions how many promises to resolve at once, the default is 2 based on the
4244
* current Gemini qps quotas, see: https://cloud.google.com/gemini/docs/quotas#per-second.
@@ -48,10 +50,24 @@ export async function resolvePromisesInParallel<T>(
4850
): Promise<T[]> {
4951
const results: T[] = [];
5052

53+
async function retryPromise(promise: Promise<T>, currentRetry: number = 0): Promise<T> {
54+
try {
55+
return await promise;
56+
} catch (error) {
57+
if (currentRetry >= MAX_RETRIES) {
58+
console.error(`Promise failed after ${MAX_RETRIES} retries:`, error);
59+
throw error;
60+
}
61+
console.error("Promise failed, retrying in 1 second:", error);
62+
await new Promise((resolve) => setTimeout(resolve, 1000)); // 1 second delay
63+
return retryPromise(promise, currentRetry + 1);
64+
}
65+
}
66+
5167
for (let i = 0; i < promises.length; i += numParallelExecutions) {
52-
const batch = promises.slice(i, i + numParallelExecutions);
53-
const batchResults = await Promise.all(batch); // Resolve batch in parallel
54-
results.push(...batchResults); // Add batch results to the main results array
68+
const batch = promises.slice(i, i + numParallelExecutions).map(retryPromise); // Apply retry to each promise in the batch
69+
const batchResults = await Promise.all(batch);
70+
results.push(...batchResults);
5571
}
5672

5773
return results;

0 commit comments

Comments
 (0)