klaytn/caver-js

http timeout options

Closed this issue · 0 comments

Is your request related to a problem? Please describe.
I've been testing caver.getBlock function.
It does not respond when it requests many times quickly or at the same time.

const Caver = require('caver-js');

const cluster = require('cluster');
const { cpus } = require('os');

const TESTNET = 'https://api.baobab.klaytn.net:8651';

const caver = new Caver(TESTNET);

const LIMIT = 10000;
const CHUNCK_SIZE = 100;

const getTransactions = count => {
  return async () => {
    if (count >= LIMIT) {
      console.log('done');
      process.exit(0)
    }
    let tasks = Array.from({ length: CHUNCK_SIZE }, (v, k) => count + 1).map(
      blockNumber => caver.klay.getBlock(blockNumber, true),
    );

    console.log('from blocknumber :', count + 1);
    const results = await Promise.all(tasks);
    console.log('to blocknumber :', count + CHUNCK_SIZE);

    count += CHUNCK_SIZE;

    // process.nextTick(getTransactions(count)); // Test with nextTick
  };
};

if (cluster.isMaster) {
  cpus().forEach(() => {
    cluster.fork();
  });
} else {
  let count = 0;
  // getTransactions(count)(); // Test with nextTick
  setInterval(getTransactions(count), 10); // test with setInterval
}

I tested with "nextTick" and "setInterval". When "CHUNK SIZE" becomes large, "caver.klay.getBlock" does not respond.

Describe the solution you'd like
In my opinion, the simple way for now is that just pass timeout options when create Caver instance If there is no reason why "httpProvider" should not have "timeout" option.

before

// test.js
const caver = new Caver(TESTNET); 

p = new this.providers.HttpProvider(p);

after

// test.js
const caver = new Caver(TESTNET, {timeout: 1000}); // pass timeout.

// index.js
p = new this.providers.HttpProvider(p, net); // pass net.

Describe alternatives you've considered
consider to change http provider request library "xhr2-cookies" to others.

Additional context
Add any other context or screenshots about the feature request here.