Skip to content

Commit a692001

Browse files
committed
feat(transport): retrieve socket name from http/s agents and add tests
1 parent 9dbfdd5 commit a692001

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

docs/config.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ Those configuration options are documented below:
220220
Please see the raven-node source code to see `how transports are implemented
221221
<https://github.com/getsentry/raven-node/blob/master/lib/transports.js>`__.
222222

223+
.. describe:: maxReqQueueCount
224+
225+
Controls how many requests can be maximally queued before bailing out and emitting an error. Defaults to `100`.
226+
223227
Environment Variables
224228
---------------------
225229

lib/client.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ extend(Raven.prototype, {
6767
this.dataCallback = options.dataCallback;
6868
this.shouldSendCallback = options.shouldSendCallback;
6969
this.sampleRate = typeof options.sampleRate === 'undefined' ? 1 : options.sampleRate;
70+
this.maxReqQueueCount = options.maxReqQueueCount || 100;
7071
this.parseUser = options.parseUser;
7172

7273
if (!this.dsn) {

lib/transports.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var timeoutReq = require('timed-out');
77
var http = require('http');
88
var https = require('https');
99

10-
var agentOptions = { keepalive: true, maxSockets: 100 };
10+
var agentOptions = {keepAlive: true, maxSockets: 100};
1111
var httpAgent = new http.Agent(agentOptions);
1212
var httpsAgent = new https.Agent(agentOptions);
1313

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

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

5249
var req = this.transport.request(options, function(res) {

test/raven.transports.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,40 @@
33
var transports = require('../lib/transports');
44

55
describe('transports', function() {
6+
it('should have an http/s agent with correct config attached by default', function() {
7+
var http = transports.http;
8+
http.agent.should.exist;
9+
http.agent.keepAlive.should.equal(true);
10+
http.agent.maxSockets.should.equal(100);
11+
12+
var https = transports.https;
13+
https.agent.should.exist;
14+
https.agent.keepAlive.should.equal(true);
15+
https.agent.maxSockets.should.equal(100);
16+
});
17+
618
it('should emit error when requests queued over the limit', function(done) {
719
var http = transports.http;
820
var _cachedAgent = http.options.agent;
921

10-
http.options.agent = {
22+
var requests = {};
23+
for (var i = 0; i < 10; i++) {
24+
requests[i] = 'req';
25+
}
26+
27+
http.agent = Object.assign({}, _cachedAgent, {
28+
getName: function() {
29+
return 'foo:123';
30+
},
1131
requests: {
12-
'foo:1234': Array.from({length: 10}).fill('req')
32+
'foo:123': requests
1333
}
14-
};
34+
});
1535

1636
http.send({
1737
dsn: {
1838
host: 'foo',
19-
port: 1234
39+
port: 123
2040
},
2141
maxReqQueueCount: 5,
2242
emit: function(event, body) {

0 commit comments

Comments
 (0)