Skip to content

Commit

Permalink
FABN-1058 NodeSDK handler close peer
Browse files Browse the repository at this point in the history
Have the handler close the peer connection
on error.

Change-Id: Iac956cb7e27fb792540976ce68057f1f5ba49937
Signed-off-by: Bret Harrison <[email protected]>
  • Loading branch information
harrisob committed Dec 13, 2018
1 parent f7df755 commit 89e8c4b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
22 changes: 20 additions & 2 deletions fabric-client/lib/Peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,20 @@ class Peer extends Remote {
super(url, opts);

logger.debug('Peer.const - url: %s timeout: %s name:%s', url, this._request_timeout, this.getName());
this._endorserClient = new _serviceProto.Endorser(this._endpoint.addr, this._endpoint.creds, this._options);
this._discoveryClient = new _discoveryProto.Discovery(this._endpoint.addr, this._endpoint.creds, this._options);
this._endorserClient = null;
this._discoveryClient = null;
this._createClients();
}

_createClients() {
if (!this._endorserClient) {
logger.debug('_createClients - create peer endorser connection ' + this._endpoint.addr);
this._endorserClient = new _serviceProto.Endorser(this._endpoint.addr, this._endpoint.creds, this._options);
}
if (!this._discoveryClient) {
logger.debug('_createClients - create peer discovery connection ' + this._endpoint.addr);
this._discoveryClient = new _discoveryProto.Discovery(this._endpoint.addr, this._endpoint.creds, this._options);
}
}

/**
Expand All @@ -62,10 +74,12 @@ class Peer extends Remote {
if (this._endorserClient) {
logger.debug('close - closing peer endorser connection ' + this._endpoint.addr);
this._endorserClient.close();
this._endorserClient = null;
}
if (this._discoveryClient) {
logger.debug('close - closing peer discovery connection ' + this._endpoint.addr);
this._discoveryClient.close();
this._discoveryClient = null;
}
}

Expand Down Expand Up @@ -95,6 +109,8 @@ class Peer extends Remote {
throw new Error('Missing proposal to send to peer');
}

this._createClients();

await this.waitForReady(this._endorserClient);

return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -166,6 +182,8 @@ class Peer extends Remote {
return Promise.reject(new Error('Missing request to send to peer discovery service'));
}

this._createClients();

return this.waitForReady(this._discoveryClient).then(() => {
return new Promise((resolve, reject) => {
const send_timeout = setTimeout(() => {
Expand Down
4 changes: 4 additions & 0 deletions fabric-client/lib/impl/DiscoveryEndorsementHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ class DiscoveryEndorsementHandler extends api.EndorsementHandler {
} catch (caught_error) {
if (!(caught_error instanceof Error)) {
error = new Error(caught_error.toString());
// if this peer failed to connect then close it
if (error.connectFailed) {
peer.close();
}
} else {
error = caught_error;
}
Expand Down

0 comments on commit 89e8c4b

Please sign in to comment.