Cache valid responses for 500 ms
This commit is contained in:
parent
d3f3d5ca36
commit
5185c1e225
@ -6,6 +6,9 @@ var debug = require('../../util/debug')('capacity-http-simple');
|
||||
function HttpSimpleCapacity(host, capacityEndpoint) {
|
||||
this.host = host;
|
||||
this.capacityEndpoint = capacityEndpoint;
|
||||
|
||||
this.lastResponse = null;
|
||||
this.lastResponseTime = 0;
|
||||
}
|
||||
|
||||
module.exports = HttpSimpleCapacity;
|
||||
@ -36,13 +39,24 @@ HttpSimpleCapacity.prototype.getResponse = function(callback) {
|
||||
json: true
|
||||
};
|
||||
debug('getCapacity(%s)', this.host);
|
||||
|
||||
// throttle requests for 500 ms
|
||||
var now = Date.now();
|
||||
if (this.lastResponse !== null && ((now - this.lastResponseTime) < 500)) {
|
||||
return callback(null, this.lastResponse);
|
||||
}
|
||||
|
||||
request.post(requestParams, function(err, res, jsonRes) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
if (jsonRes && jsonRes.retcode === 0) {
|
||||
return callback(null, jsonRes.return_values || {});
|
||||
this.lastResponse = jsonRes.return_values || {};
|
||||
// We could go more aggressive by updating lastResponseTime on failures.
|
||||
this.lastResponseTime = now;
|
||||
|
||||
return callback(null, this.lastResponse);
|
||||
}
|
||||
return callback(new Error('Could not retrieve information from endpoint'));
|
||||
});
|
||||
}.bind(this));
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user