Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce timeout #45

Merged
merged 6 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || undefined;
shumkov marked this conversation as resolved.
Show resolved Hide resolved
this.protocol = opts.protocol === 'http' ? http : https;
this.batchedCalls = null;
this.disableAgent = opts.disableAgent || false;
Expand Down Expand Up @@ -74,6 +75,7 @@ function innerRpc(request, callback) {
port: self.port,
rejectUnauthorized: self.rejectUnauthorized,
agent: self.disableAgent ? false : undefined,
timeout: self.timeout ? self.timeout : undefined,
strophy marked this conversation as resolved.
Show resolved Hide resolved
};

if (self.httpOptions) {
Expand Down Expand Up @@ -142,6 +144,11 @@ function innerRpc(request, callback) {
}
});

req.on('timeout', () => {
const err = new Error(`Timeout Error: ${options.timeout}ms exceeded`);
req.destroy(err);
});

req.setHeader('Content-Length', request.length);
req.setHeader('Content-Type', 'application/json');
req.setHeader('Authorization', `Basic ${auth}`);
Expand All @@ -156,6 +163,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',
Expand Down
20 changes: 20 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,3 +526,23 @@ describe('RpcClient', function() {
});

});

it('should throw error when timeout is triggered', (done) => {
var client = new RpcClient({
user: 'user',
pass: 'pass',
host: '127.0.0.1',
port: 1000,
});

client.httpOptions = {
timeout: 100
};

client.getDifficulty((error, parsedBuf) => {
should.exist(error);
should.not.exist(parsedBuf);
error.message.should.equal('Timeout Error: 100ms exceeded')
done();
})
});