Skip to content

Commit a7be540

Browse files
Refactor retry logic to use iterative approach and fix payload handling
Co-authored-by: xuyushun441-sys <255036401+xuyushun441-sys@users.noreply.github.com>
1 parent 0c9a493 commit a7be540

1 file changed

Lines changed: 31 additions & 27 deletions

File tree

packages/drivers/sdk/src/index.ts

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -272,36 +272,40 @@ export class RemoteDriver implements Driver, DriverInterface {
272272
fn: () => Promise<T>,
273273
attempt: number = 0
274274
): Promise<T> {
275-
try {
276-
return await fn();
277-
} catch (error: any) {
278-
// Don't retry on client errors (4xx) or if retries are disabled
279-
if (!this.enableRetry || attempt >= this.maxRetries) {
280-
throw error;
281-
}
282-
283-
// Don't retry on validation or auth errors
284-
if (error instanceof ObjectQLError) {
285-
const nonRetryableCodes = [
286-
ApiErrorCode.VALIDATION_ERROR,
287-
ApiErrorCode.UNAUTHORIZED,
288-
ApiErrorCode.FORBIDDEN,
289-
ApiErrorCode.NOT_FOUND
290-
];
291-
if (nonRetryableCodes.includes(error.code as ApiErrorCode)) {
275+
let currentAttempt = attempt;
276+
277+
while (true) {
278+
try {
279+
return await fn();
280+
} catch (error: any) {
281+
// Don't retry on client errors (4xx) or if retries are disabled
282+
if (!this.enableRetry || currentAttempt >= this.maxRetries) {
292283
throw error;
293284
}
294-
}
295285

296-
// Calculate exponential backoff delay
297-
const delay = Math.min(1000 * Math.pow(2, attempt), 10000);
298-
299-
if (this.enableLogging) {
300-
console.log(`Retry attempt ${attempt + 1}/${this.maxRetries} after ${delay}ms delay`);
301-
}
286+
// Don't retry on validation or auth errors
287+
if (error instanceof ObjectQLError) {
288+
const nonRetryableCodes = [
289+
ApiErrorCode.VALIDATION_ERROR,
290+
ApiErrorCode.UNAUTHORIZED,
291+
ApiErrorCode.FORBIDDEN,
292+
ApiErrorCode.NOT_FOUND
293+
];
294+
if (nonRetryableCodes.includes(error.code as ApiErrorCode)) {
295+
throw error;
296+
}
297+
}
302298

303-
await new Promise(resolve => setTimeout(resolve, delay));
304-
return this.retryWithBackoff(fn, attempt + 1);
299+
// Calculate exponential backoff delay
300+
const delay = Math.min(1000 * Math.pow(2, currentAttempt), 10000);
301+
302+
if (this.enableLogging) {
303+
console.log(`Retry attempt ${currentAttempt + 1}/${this.maxRetries} after ${delay}ms delay`);
304+
}
305+
306+
await new Promise(resolve => setTimeout(resolve, delay));
307+
currentAttempt++;
308+
}
305309
}
306310
}
307311

@@ -561,7 +565,7 @@ export class RemoteDriver implements Driver, DriverInterface {
561565
const response = await fetch(targetEndpoint, {
562566
method: 'POST',
563567
headers,
564-
body: payload ? JSON.stringify(payload) : undefined,
568+
body: payload !== undefined ? JSON.stringify(payload) : undefined,
565569
signal: createTimeoutSignal(this.timeout)
566570
});
567571

0 commit comments

Comments
 (0)