Skip to content

Commit

Permalink
fix(reporter): fix reporter http chunk reading
Browse files Browse the repository at this point in the history
feat(reporter): add incremental retry to reporter
  • Loading branch information
peteyycz committed Aug 7, 2015
1 parent 22ec858 commit a0c68f7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/reporters/trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ var COLLECTOR_API_SERVICE = config.collectorApi + config.collectorApiServiceEndp
function TraceReporter (options) {
this.apiKey = options.apiKey || process.env.TRACE_API_KEY;
this.appName = options.appName || process.env.TRACE_APP_NAME;
this.baseRetryInterval = 100;
this.retryLimit = 5;
this.retryCount = 0;

//check if everything is ok with config
if (!this.apiKey) {
Expand Down Expand Up @@ -44,6 +47,10 @@ TraceReporter.prototype.send = function (data, callback) {
req.end();
};

TraceReporter.prototype._getRetryInterval = function() {
return Math.pow(2, this.retryCount) * this.baseRetryInterval;
};

TraceReporter.prototype.getService = function(callback) {

var opts = url.parse(COLLECTOR_API_SERVICE);
Expand All @@ -59,18 +66,22 @@ TraceReporter.prototype.getService = function(callback) {
'Content-Type': 'application/json'
}
}, function (res) {
var resChunk;
res.setEncoding('utf8');
res.on('data', function (chunk) {
var res;
try {
res = JSON.parse(chunk);
resChunk = JSON.parse(chunk);
} catch (ex) {
return callback(ex);
}
if (res.statusCode === 409) {
return _this.getService(callback);
if (++_this.retryCount < _this.retryLimit) {
return setTimeout(_this.getService(callback),
_this._getRetryInterval());
}
return callback(new Error('The trace collector-api is currently unavailable'));
}
return callback(null, res);
return callback(null, resChunk);
});
})
.on('error', callback);
Expand Down
27 changes: 27 additions & 0 deletions lib/reporters/trace.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,31 @@ describe('The Trace reporter module', function () {
});
});

it('retries on 409 error', function (done) {
var options = {
appName: 'testName',
apiKey: 'testApiKey'
};
var data = {
name: options.appName
};
var traceReporter = TraceReporter.create(options);
traceReporter.retryLimit = 2;
traceReporter.baseRetryInterval = 0;

nock(collectorApi, {
reqheaders: {
'Authorization': 'Bearer testApiKey',
'Content-Type': 'application/json'
}
})
.post(collectorApiServiceEndpoint, JSON.stringify(data))
.times(traceReporter.retryLimit)
.reply(409, {});

traceReporter.getService(function (err) {
expect(err.message).to.be.eql('The trace collector-api is currently unavailable');
done();
});
});
});

0 comments on commit a0c68f7

Please sign in to comment.