multimodalart HF Staff commited on
Commit
0a8c984
·
verified ·
1 Parent(s): 93d5654

Update proxy.js

Browse files
Files changed (1) hide show
  1. proxy.js +33 -10
proxy.js CHANGED
@@ -22,6 +22,7 @@ async function processQueue() {
22
 
23
  async function retryWith503Backoff(url, options, startTime) {
24
  let attempt = 0;
 
25
 
26
  while (true) {
27
  const elapsed = Date.now() - startTime;
@@ -30,21 +31,43 @@ async function retryWith503Backoff(url, options, startTime) {
30
  }
31
 
32
  const response = await fetch(url, options);
 
33
 
34
- if (response.status !== 503) {
 
35
  return response;
36
  }
37
 
38
- // Exponential backoff: 1s, 2s, 4s, 8s, 16s, 32s, 64s...
39
- const delay = Math.min(1000 * Math.pow(2, attempt), 64000);
40
- attempt++;
41
-
42
- // Check if waiting would exceed max time
43
- if (elapsed + delay > MAX_WAIT_MS) {
44
- throw new Error('Max wait time would be exceeded');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  }
46
-
47
- await new Promise(resolve => setTimeout(resolve, delay));
48
  }
49
  }
50
 
 
22
 
23
  async function retryWith503Backoff(url, options, startTime) {
24
  let attempt = 0;
25
+ let lastResponse = null;
26
 
27
  while (true) {
28
  const elapsed = Date.now() - startTime;
 
31
  }
32
 
33
  const response = await fetch(url, options);
34
+ lastResponse = response;
35
 
36
+ // If successful (2xx), return immediately
37
+ if (response.ok) {
38
  return response;
39
  }
40
 
41
+ // For 503: retry with exponential backoff until max time
42
+ // For other errors: retry up to 3 times, then return the error
43
+ if (response.status === 503) {
44
+ // Exponential backoff for 503: 1s, 2s, 4s, 8s, 16s, 32s, 64s...
45
+ const delay = Math.min(1000 * Math.pow(2, attempt), 64000);
46
+ attempt++;
47
+
48
+ // Check if waiting would exceed max time
49
+ if (elapsed + delay > MAX_WAIT_MS) {
50
+ return response; // Return 503 if we'd exceed max time
51
+ }
52
+
53
+ await new Promise(resolve => setTimeout(resolve, delay));
54
+ } else {
55
+ // For non-503 errors, retry up to 3 times with shorter delays
56
+ if (attempt >= 3) {
57
+ return response; // Return the error after 3 attempts
58
+ }
59
+
60
+ // Short delay for non-503 errors: 1s, 2s, 3s
61
+ const delay = (attempt + 1) * 1000;
62
+ attempt++;
63
+
64
+ // Check if waiting would exceed max time
65
+ if (elapsed + delay > MAX_WAIT_MS) {
66
+ return response;
67
+ }
68
+
69
+ await new Promise(resolve => setTimeout(resolve, delay));
70
  }
 
 
71
  }
72
  }
73