From f417f4cb0492fd49be38668be9f620cacc02ca22 Mon Sep 17 00:00:00 2001 From: strophy <32928115+strophy@users.noreply.github.com> Date: Thu, 30 Jun 2022 13:36:53 +0200 Subject: [PATCH] feat: introduce timeout (#45) Co-authored-by: Ivan Shumkov --- README.md | 3 ++- lib/index.js | 14 ++++++++++++++ test/index.js | 30 +++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b032c92..94ffe5e 100644 --- a/README.md +++ b/README.md @@ -121,7 +121,8 @@ var client = new RPCclient({ user: 'dash', pass: 'local321', host: '127.0.0.1', - port: 19998 + port: 19998, + timeout: 1000 }); var cb = function (err, data) { diff --git a/lib/index.js b/lib/index.js index 430d9d8..504310c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -10,6 +10,7 @@ function RpcClient(opts) { this.port = opts.port || 9998; this.user = opts.user || 'user'; this.pass = opts.pass || 'pass'; + this.timeout = opts.timeout; this.protocol = opts.protocol === 'http' ? http : https; this.batchedCalls = null; this.disableAgent = opts.disableAgent || false; @@ -74,6 +75,10 @@ function innerRpc(request, callback) { port: self.port, rejectUnauthorized: self.rejectUnauthorized, agent: self.disableAgent ? false : undefined, +}; + +if (self.timeout) { + options.timeout = self.timeout; }; if (self.httpOptions) { @@ -142,6 +147,11 @@ function innerRpc(request, callback) { } }); + req.on('timeout', () => { + const err = new Error(`Timeout Error: ${options.timeout}ms exceeded`); + callback(err); + }); + req.setHeader('Content-Length', request.length); req.setHeader('Content-Type', 'application/json'); req.setHeader('Authorization', `Basic ${auth}`); @@ -156,6 +166,10 @@ RpcClient.prototype.batch = function (batchCallback, resultCallback) { this.batchedCalls = null; }; +RpcClient.prototype.setTimeout = function (timeout) { + this.timeout = timeout; +} + // For definitions of RPC calls, see various files in: https://github.com/dashpay/dash/tree/master/src RpcClient.callspec = { abandonTransaction: 'str', diff --git a/test/index.js b/test/index.js index 9332748..dc2fab2 100644 --- a/test/index.js +++ b/test/index.js @@ -525,4 +525,32 @@ describe('RpcClient', function() { }); -}); + it('should throw error when timeout is triggered', (done) => { + var client = new RpcClient({ + user: 'user', + pass: 'pass', + host: 'localhost', + port: 8332, + }); + + client.httpOptions = { + timeout: 100 + }; + + var requestStub = sinon.stub(client.protocol, 'request').callsFake(function (options, callback) { + var req = new FakeRequest(); + setTimeout(function () { + req.emit('timeout'); + }, options.timeout); + return req; + }); + + client.getDifficulty((error, parsedBuf) => { + should.exist(error); + should.not.exist(parsedBuf); + error.message.should.equal(`Timeout Error: ${client.httpOptions.timeout}ms exceeded`); + requestStub.restore(); + done(); + }) + }); +})