Skip to content

Commit

Permalink
remove Request resubmit logic
Browse files Browse the repository at this point in the history
add default timeout to Request
  • Loading branch information
darkdarkdragon committed Oct 2, 2015
1 parent 8edc3b1 commit c69790e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 59 deletions.
73 changes: 22 additions & 51 deletions src/core/request.js
Expand Up @@ -34,13 +34,13 @@ function Request(remote, command) {
command: command,
id: undefined
};
this._submissionTimeout = this.remote.submission_timeout;
}

util.inherits(Request, EventEmitter);

// Send the request to a remote.
Request.prototype.request = function(servers, callback_) {
const self = this;
const callback = typeof servers === 'function' ? servers : callback_;

if (this.requested) {
Expand All @@ -59,31 +59,31 @@ Request.prototype.request = function(servers, callback_) {
this.on('error', function() {});
this.emit('request', this.remote);

function doRequest() {
if (Array.isArray(servers)) {
servers.forEach(function(server) {
self.setServer(server);
self.remote.request(self);
}, self);
} else {
self.remote.request(self);
const timeout = setTimeout(() => {
if (typeof callback === 'function') {
callback(new RippleError('tejTimeout'));
}
}

function onReconnect() {
doRequest();
}
this.emit('timeout');
// just in case
this.emit = _.noop;
this.cancel();
}, this._submissionTimeout);

function onResponse() {
self.remote.removeListener('connected', onReconnect);
clearTimeout(timeout);
}

if (this.remote.isConnected()) {
this.remote.on('connected', onReconnect);
}
this.once('response', onResponse);

doRequest();
if (Array.isArray(servers)) {
servers.forEach(function(server) {
this.setServer(server);
this.remote.request(this);
}, this);
} else {
this.remote.request(this);
}

return this;
};
Expand Down Expand Up @@ -264,40 +264,11 @@ Request.prototype.callback = function(callback, successEvent, errorEvent) {
return this;
};

Request.prototype.timeout = function(duration, callback) {
const self = this;

function requested() {
self.timeout(duration, callback);
}

if (!this.requested) {
// Defer until requested
return this.once('request', requested);
Request.prototype.setSubmissionTimeout = function(delay) {
if (!_.isFinite(delay)) {
throw new Error('delay must be number');
}

const emit = this.emit;
let timed_out = false;

const timeout = setTimeout(function() {
timed_out = true;

if (typeof callback === 'function') {
callback();
}

emit.call(self, 'timeout');
self.cancel();
}, duration);

this.emit = function() {
if (!timed_out) {
clearTimeout(timeout);
emit.apply(self, arguments);
}
};

return this;
this._submissionTimeout = delay;
};

Request.prototype.setServer = function(server) {
Expand Down
3 changes: 2 additions & 1 deletion src/core/transactionmanager.js
Expand Up @@ -698,7 +698,8 @@ TransactionManager.prototype._request = function(tx) {

tx.emit('postsubmit');

submitRequest.timeout(self._submissionTimeout, requestTimeout);
submitRequest.setSubmissionTimeout(self._submissionTimeout);
submitRequest.once('timeout', requestTimeout);
};

/**
Expand Down
19 changes: 12 additions & 7 deletions test/request-test.js
Expand Up @@ -538,6 +538,7 @@ describe('Request', function() {
setTimeout(function() {
successEmitted = true;
req.emit('success', SERVER_INFO);
req.emit('response', SERVER_INFO);
}, 200);
};

Expand All @@ -546,8 +547,9 @@ describe('Request', function() {
remote._servers = [server];

const request = new Request(remote, 'server_info');
request.setSubmissionTimeout(10);

request.timeout(10, function() {
request.on('timeout', function() {
setTimeout(function() {
assert(successEmitted);
done();
Expand All @@ -568,7 +570,8 @@ describe('Request', function() {
assert.strictEqual(req.message.command, 'server_info');
setTimeout(function() {
req.emit('success', SERVER_INFO);
}, 200);
req.emit('response', SERVER_INFO);
}, 20);
};

const remote = new Remote();
Expand All @@ -583,13 +586,15 @@ describe('Request', function() {
timedOut = true;
});

request.timeout(1000);
request.setSubmissionTimeout(100);

request.callback(function(err, res) {
assert(!timedOut);
assert.ifError(err);
assert.deepEqual(res, SERVER_INFO);
done();
setTimeout(function() {
assert(!timedOut, 'must not timeout');
assert.ifError(err);
assert.deepEqual(res, SERVER_INFO);
done();
}, 100);
});
});

Expand Down

0 comments on commit c69790e

Please sign in to comment.