Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
feat(transport): retrieve socket name from http/s agents and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek committed Sep 26, 2017
1 parent 1554d73 commit d2750f8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
4 changes: 4 additions & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ Those configuration options are documented below:
Please see the raven-node source code to see `how transports are implemented
<https://github.com/getsentry/raven-node/blob/master/lib/transports.js>`__.

.. describe:: maxReqQueueCount

Controls how many requests can be maximally queued before bailing out and emitting an error. Defaults to `100`.

Environment Variables
---------------------

Expand Down
1 change: 1 addition & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ extend(Raven.prototype, {
this.dataCallback = options.dataCallback;
this.shouldSendCallback = options.shouldSendCallback;
this.sampleRate = typeof options.sampleRate === 'undefined' ? 1 : options.sampleRate;
this.maxReqQueueCount = options.maxReqQueueCount || 100;
this.parseUser = options.parseUser;

if (!this.dsn) {
Expand Down
17 changes: 7 additions & 10 deletions lib/transports.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var timeoutReq = require('timed-out');
var http = require('http');
var https = require('https');

var agentOptions = { keepalive: true, maxSockets: 100 };
var agentOptions = {keepAlive: true, maxSockets: 100};
var httpAgent = new http.Agent(agentOptions);
var httpsAgent = new https.Agent(agentOptions);

Expand Down Expand Up @@ -38,15 +38,12 @@ HTTPTransport.prototype.send = function(client, message, headers, eventId, cb) {
}

// prevent off heap memory explosion
var _agent = options.agent || this.transport.globalAgent;
if (_agent) {
var _name = client.dsn.host + ':' + client.dsn.port;
var _requests = _agent.requests[_name];
if (_requests && _requests.length > client.maxReqQueueCount) {
// other feedback strategy
client.emit('error', new Error('client req queue is full..'));
return;
}
var _name = this.agent.getName({host: client.dsn.host, port: client.dsn.port});
var _requests = this.agent.requests[_name];
if (_requests && Object.keys(_requests).length > client.maxReqQueueCount) {
// other feedback strategy
client.emit('error', new Error('client req queue is full..'));
return;
}

var req = this.transport.request(options, function(res) {
Expand Down
28 changes: 24 additions & 4 deletions test/raven.transports.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,40 @@
var transports = require('../lib/transports');

describe('transports', function() {
it('should have an http/s agent with correct config attached by default', function() {
var http = transports.http;
http.agent.should.exist;
http.agent.keepAlive.should.equal(true);
http.agent.maxSockets.should.equal(100);

var https = transports.https;
https.agent.should.exist;
https.agent.keepAlive.should.equal(true);
https.agent.maxSockets.should.equal(100);
});

it('should emit error when requests queued over the limit', function(done) {
var http = transports.http;
var _cachedAgent = http.options.agent;

http.options.agent = {
var requests = {};
for (var i = 0; i < 10; i++) {
requests[i] = 'req';
}

http.agent = Object.assign({}, _cachedAgent, {
getName: function() {
return 'foo:123';
},
requests: {
'foo:1234': Array.from({length: 10}).fill('req')
'foo:123': requests
}
};
});

http.send({
dsn: {
host: 'foo',
port: 1234
port: 123
},
maxReqQueueCount: 5,
emit: function(event, body) {
Expand Down

0 comments on commit d2750f8

Please sign in to comment.