From a02510db2d52fa098836b2655d42511ae6f9fc05 Mon Sep 17 00:00:00 2001 From: Dafydd Durairaj Date: Mon, 24 Aug 2020 14:20:45 -0400 Subject: [PATCH] Exponential backoff on waiting between 429 Errors in connection.js --- web3.js/src/connection.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/web3.js/src/connection.js b/web3.js/src/connection.js index c97f7c09805e9f..2cb01fb0dc0c3e 100644 --- a/web3.js/src/connection.js +++ b/web3.js/src/connection.js @@ -509,21 +509,23 @@ function createRpcRequest(url): RpcRequest { try { let too_many_requests_retries = 5; let res = {}; - + let waitTime = 500; for (;;) { res = await fetch(url, options); if ( - res.status !== 429 /* Too many requests */ || - too_many_requests_retries === 0 + res.status !== 429 /* Too many requests */ ) { break; } - + too_many_requests_retries -= 1; + if (too_many_requests_retries === 0) { + break; + } console.log( - `Server responded with ${res.status} ${res.statusText}. Retrying after brief delay...`, + `Server responded with ${res.status} ${res.statusText}. Retrying after ${waitTime}ms delay...`, ); - await sleep(500); - too_many_requests_retries -= 1; + await sleep(waitTime); + waitTime *= 2; } const text = await res.text();