From 8468d3ebd1c96d0781043f1ffc88996e3ef1670f Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Thu, 1 Nov 2018 20:35:19 -0700 Subject: [PATCH] fix: limit concurrent Loads to improve stability --- web3.js/src/loader.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/web3.js/src/loader.js b/web3.js/src/loader.js index 6c612b2822..6117d5fee4 100644 --- a/web3.js/src/loader.js +++ b/web3.js/src/loader.js @@ -51,7 +51,7 @@ export class Loader { const chunkSize = 256; let offset = 0; let array = data; - const transactions = []; + let transactions = []; while (array.length > 0) { const bytes = array.slice(0, chunkSize); const userdata = Buffer.alloc(chunkSize + 16); @@ -71,6 +71,15 @@ export class Loader { }); transactions.push(sendAndConfirmTransaction(this.connection, program, transaction)); + // Run up to 8 Loads in parallel to prevent too many parallel transactions from + // getting rejected with AccountInUse. + // + // TODO: 8 was selected empirically and should probably be revisited + if (transactions.length === 8) { + await Promise.all(transactions); + transactions = []; + } + offset += chunkSize; array = array.slice(chunkSize); }