Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed endpoint connection should be removed from agent endpoint table. #225

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,7 @@ Agent.prototype.request = function request(options, callback) {
port: options.port,
localAddress: options.localAddress
});
var self = this;

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

endpoint.on('closed', function () {
delete self.endpoints[key];
});

this.endpoints[key] = endpoint;
endpoint.pipe(endpoint.socket).pipe(endpoint);
request._start(endpoint.createStream(), options);
Expand Down Expand Up @@ -1011,6 +1016,9 @@ Agent.prototype.request = function request(options, callback) {
self._log.info({ e: endpoint, server: options.host + ':' + options.port },
'New outgoing HTTP/2 connection');
self.endpoints[key] = endpoint;
endpoint.on('closed', function () {
delete self.endpoints[key];
});
self.emit(key, endpoint);
} else {
self.emit(key, undefined);
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ Connection.prototype.close = function close(error) {
});
this.push(null);
this._closed = true;
this.emit('closed');
};

Connection.prototype._receiveGoaway = function _receiveGoaway(frame) {
Expand All @@ -583,6 +584,7 @@ Connection.prototype._receiveGoaway = function _receiveGoaway(frame) {
if (frame.error !== 'NO_ERROR') {
this.emit('peerError', frame.error);
}
this.emit('closed');
};

// Flow control
Expand Down
2 changes: 1 addition & 1 deletion lib/protocol/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ Endpoint.prototype._initializeErrorHandling = function _initializeErrorHandling(
this._compressor.on('error', this._error.bind(this, 'compressor'));
this._decompressor.on('error', this._error.bind(this, 'decompressor'));
this._connection.on('error', this._error.bind(this, 'connection'));

this._connection.on('peerError', this.emit.bind(this, 'peerError'));
this._connection.on('closed', this.emit.bind(this, 'closed'));
};

Endpoint.prototype._error = function _error(component, error) {
Expand Down
6 changes: 6 additions & 0 deletions test/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ describe('connection.js', function() {

c.close();
});
it('should emit an event', function(done) {
c.on('closed', function () {
done();
});
c.close();
});
});
});
});
11 changes: 11 additions & 0 deletions test/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ describe('endpoint.js', function() {
});
});
});
describe('connection closing', function() {
describe('closing the endpoint connection', function() {
it('should emit an event', function(done) {
var c = new Endpoint(util.log.child({ role: 'client' }), 'CLIENT', settings);
c.on('closed', function () {
done();
});
c.close();
});
});
});
describe('bunyan serializer', function() {
describe('`e`', function() {
var format = endpoint.serializers.e;
Expand Down