Skip to content

Commit

Permalink
fix(p2p): cancel retry connection timer
Browse files Browse the repository at this point in the history
This properly cancels the timer to retry a connection to a peer when
the peer is closed. Not canceling the timer could interfere with
gracefully shutting down `xud`, as the process would continue waiting
for the timer.
  • Loading branch information
sangaman committed Nov 28, 2018
1 parent da3278e commit 28a06a4
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/p2p/Peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class Peer extends EventEmitter {
private socket?: Socket;
private parser: Parser = new Parser(Packet.PROTOCOL_DELIMITER);
private closed = false;
/** Timer to retry connection to peer after the previous attempt failed. */
private retryConnectionTimer?: NodeJS.Timer;
private connectTimeout?: NodeJS.Timer;
private stallTimer?: NodeJS.Timer;
private pingTimer?: NodeJS.Timer;
Expand Down Expand Up @@ -206,6 +208,11 @@ class Peer extends EventEmitter {
delete this.socket;
}

if (this.retryConnectionTimer) {
clearTimeout(this.retryConnectionTimer);
this.retryConnectionTimer = undefined;
}

if (this.pingTimer) {
clearInterval(this.pingTimer);
this.pingTimer = undefined;
Expand Down Expand Up @@ -290,6 +297,7 @@ class Peer extends EventEmitter {
}
this.socket!.removeListener('error', onError);
this.socket!.removeListener('connect', onConnect);
this.retryConnectionTimer = undefined;
};

const onConnect = () => {
Expand Down Expand Up @@ -326,7 +334,7 @@ class Peer extends EventEmitter {
`failed: ${err.message}. retrying in ${retryDelay / 1000} sec...`,
);

setTimeout(() => {
this.retryConnectionTimer = setTimeout(() => {
retryDelay = Math.min(Peer.CONNECTION_RETRIES_MAX_DELAY, retryDelay * 2);
retries = retries + 1;
this.socket!.connect(this.address);
Expand Down

0 comments on commit 28a06a4

Please sign in to comment.