15
15
// Functions for different ways to summarize Comment and Vote data.
16
16
17
17
import { Model } from "../../models/model" ;
18
+ import { MAX_RETRIES } from "../../models/vertex_model" ;
18
19
import { SummaryContent } from "../../types" ;
19
20
20
21
export abstract class RecursiveSummary < InputType > {
@@ -34,9 +35,10 @@ export abstract class RecursiveSummary<InputType> {
34
35
35
36
/**
36
37
* Resolves Promises sequentially, optionally using batching for limited parallelization.
38
+ * Adds a one-second backoff for failed calls.
37
39
*
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.
40
42
* @param promises the promises to resolve.
41
43
* @param numParallelExecutions how many promises to resolve at once, the default is 2 based on the
42
44
* current Gemini qps quotas, see: https://cloud.google.com/gemini/docs/quotas#per-second.
@@ -48,10 +50,24 @@ export async function resolvePromisesInParallel<T>(
48
50
) : Promise < T [ ] > {
49
51
const results : T [ ] = [ ] ;
50
52
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
+
51
67
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 ) ;
55
71
}
56
72
57
73
return results ;
0 commit comments