Skip to content

Commit c6c4a45

Browse files
committed
v0.1.4 auto-retry fetches
1 parent b25881b commit c6c4a45

4 files changed

Lines changed: 29 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Deploy and verify your circuits to Circuitscan with a simple command.
55
See also: [Circuitscan CLI Documentation](https://circuitscan.readthedocs.io/en/latest/usage.html)
66

77
> [!NOTE]
8-
> Supports circom 2.0.8-2.1.9, snarkjs 0.6.11-0.7.4
8+
> Supports circom 2.0.8-2.1.9, snarkjs 0.6.11-0.7.5
99
>
1010
> Supports noir 0.31.0-0.34.0
1111

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "circuitscan",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"main": "cli.js",
55
"type": "module",
66
"author": "numtel <ben@latenightsketches.com>",

src/circuitscan.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
generateRandomString,
77
delay,
88
instanceSizes,
9+
fetchWithRetry,
910
} from './utils.js';
1011
import {StatusLogger} from './StatusLogger.js';
1112

@@ -64,7 +65,7 @@ export async function invokeRemoteMachine(payload, options) {
6465
};
6566

6667
const {curCompilerURL} = await determineCompilerUrl(options);
67-
const response = await fetch(curCompilerURL, {
68+
const response = await fetchWithRetry(curCompilerURL, {
6869
method: 'POST',
6970
headers: {
7071
'Content-Type': 'application/json',
@@ -87,7 +88,7 @@ export async function invokeRemoteMachine(payload, options) {
8788
const {stderr, stdout} = await watchInstance(options.config.blobUrl, requestId, apiKey, options, 8000);
8889
console.error(stderr);
8990
console.log(stdout);
90-
const response = await fetch(`${options.config.blobUrl}instance-response/${requestId}.json`);
91+
const response = await fetchWithRetry(`${options.config.blobUrl}instance-response/${requestId}.json`);
9192
try {
9293
const data = await response.json();
9394
body = JSON.parse(data.body);
@@ -103,7 +104,7 @@ export async function verifyCircuit(action, pkgName, chainId, contract, options)
103104
const event = {payload: {action, pkgName, chainId, contract}};
104105
console.log(`# Verifying circuit...`);
105106

106-
const response = await fetch(options.config.serverURL, {
107+
const response = await fetchWithRetry(options.config.serverURL, {
107108
method: 'POST',
108109
headers: {
109110
'Content-Type': 'application/json',
@@ -228,13 +229,13 @@ function healthcheckFetch(ip, timeout) {
228229
const timeoutPromise = new Promise((_, reject) =>
229230
setTimeout(() => reject(new TimeoutError()), timeout)
230231
);
231-
const fetchPromise = fetch(`http://${ip}:3000`);
232+
const fetchPromise = fetchWithRetry(`http://${ip}:3000`);
232233
return Promise.race([fetchPromise, timeoutPromise]);
233234
}
234235

235236
async function terminateInstance(requestId, apiKey, options) {
236237
const event = {payload: {requestId}, apiKey};
237-
const response = await fetch(process.env.LOCAL_TERMINATOR || (options && options.config && options.config.terminatorURL), {
238+
const response = await fetchWithRetry(process.env.LOCAL_TERMINATOR || (options && options.config && options.config.terminatorURL), {
238239
method: 'POST',
239240
headers: {
240241
'Content-Type': 'application/json',
@@ -250,7 +251,7 @@ async function terminateInstance(requestId, apiKey, options) {
250251
}
251252

252253
async function fetchResult(blobUrl, requestId, pipename) {
253-
const response = await fetch(`${blobUrl}instance/${requestId}/${pipename}.txt`);
254+
const response = await fetchWithRetry(`${blobUrl}instance/${requestId}/${pipename}.txt`);
254255
if (!response.ok) {
255256
if (response.status === 404 || response.status === 403) {
256257
throw new NotFoundError;

src/utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,23 @@ export function viemChain(nameOrId) {
120120
if(chain.id === Number(nameOrId)) return chain;
121121
}
122122
}
123+
124+
export async function fetchWithRetry(url, options = {}, retries = 5, delay = 1000) {
125+
for (let attempt = 0; attempt < retries; attempt++) {
126+
try {
127+
const response = await fetch(url, options);
128+
return response;
129+
} catch (error) {
130+
if (error.message.includes('ETIMEDOUT') || error.message.includes('fetch failed')) {
131+
if (attempt < retries - 1) {
132+
console.warn(`Retrying fetch (${attempt + 1}/${retries}) after timeout...`);
133+
await new Promise(res => setTimeout(res, delay * Math.pow(2, attempt))); // Exponential backoff
134+
} else {
135+
throw new Error(`Fetch failed after ${retries} retries: ${error.message}`);
136+
}
137+
} else {
138+
throw error; // If it's not a timeout error, rethrow it immediately
139+
}
140+
}
141+
}
142+
}

0 commit comments

Comments
 (0)