Skip to content

Commit 785aff4

Browse files
authored
Merge pull request #1 from ashimoon/delete-closed-endpoints
Delete closed endpoints
2 parents 2c4c310 + 04b1d36 commit 785aff4

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed

lib/http.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,7 @@ Agent.prototype.request = function request(options, callback) {
943943
port: options.port,
944944
localAddress: options.localAddress
945945
});
946+
var self = this;
946947

947948
endpoint.socket.on('error', function (error) {
948949
self._log.error('Socket error: ' + error.toString());
@@ -954,6 +955,10 @@ Agent.prototype.request = function request(options, callback) {
954955
request.emit('error', error);
955956
});
956957

958+
endpoint.on('closed', function () {
959+
delete self.endpoints[key];
960+
});
961+
957962
this.endpoints[key] = endpoint;
958963
endpoint.pipe(endpoint.socket).pipe(endpoint);
959964
request._start(endpoint.createStream(), options);
@@ -1011,6 +1016,9 @@ Agent.prototype.request = function request(options, callback) {
10111016
self._log.info({ e: endpoint, server: options.host + ':' + options.port },
10121017
'New outgoing HTTP/2 connection');
10131018
self.endpoints[key] = endpoint;
1019+
endpoint.on('closed', function () {
1020+
delete self.endpoints[key];
1021+
});
10141022
self.emit(key, endpoint);
10151023
} else {
10161024
self.emit(key, undefined);

lib/protocol/connection.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ Connection.prototype.close = function close(error) {
574574
});
575575
this.push(null);
576576
this._closed = true;
577+
this.emit('closed');
577578
};
578579

579580
Connection.prototype._receiveGoaway = function _receiveGoaway(frame) {
@@ -583,6 +584,7 @@ Connection.prototype._receiveGoaway = function _receiveGoaway(frame) {
583584
if (frame.error !== 'NO_ERROR') {
584585
this.emit('peerError', frame.error);
585586
}
587+
this.emit('closed');
586588
};
587589

588590
// Flow control

lib/protocol/endpoint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ Endpoint.prototype._initializeErrorHandling = function _initializeErrorHandling(
233233
this._compressor.on('error', this._error.bind(this, 'compressor'));
234234
this._decompressor.on('error', this._error.bind(this, 'decompressor'));
235235
this._connection.on('error', this._error.bind(this, 'connection'));
236-
237236
this._connection.on('peerError', this.emit.bind(this, 'peerError'));
237+
this._connection.on('closed', this.emit.bind(this, 'closed'));
238238
};
239239

240240
Endpoint.prototype._error = function _error(component, error) {

test/connection.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ describe('connection.js', function() {
232232

233233
c.close();
234234
});
235+
it('should emit an event', function(done) {
236+
c.on('closed', function () {
237+
done();
238+
});
239+
c.close();
240+
});
235241
});
236242
});
237243
});

test/endpoint.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ describe('endpoint.js', function() {
2626
});
2727
});
2828
});
29+
describe('connection closing', function() {
30+
describe('closing the endpoint connection', function() {
31+
it('should emit an event', function(done) {
32+
var c = new Endpoint(util.log.child({ role: 'client' }), 'CLIENT', settings);
33+
c.on('closed', function () {
34+
done();
35+
});
36+
c.close();
37+
});
38+
});
39+
});
2940
describe('bunyan serializer', function() {
3041
describe('`e`', function() {
3142
var format = endpoint.serializers.e;

0 commit comments

Comments
 (0)