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
p2p: persistent - redial if first dial fails #1404
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,7 @@ type Switch struct { | |
reactorsByCh map[byte]Reactor | ||
peers *PeerSet | ||
dialing *cmn.CMap | ||
reconnecting *cmn.CMap | ||
nodeInfo NodeInfo // our node info | ||
nodeKey *NodeKey // our node privkey | ||
addrBook AddrBook | ||
|
@@ -75,6 +76,7 @@ func NewSwitch(config *cfg.P2PConfig) *Switch { | |
reactorsByCh: make(map[byte]Reactor), | ||
peers: NewPeerSet(), | ||
dialing: cmn.NewCMap(), | ||
reconnecting: cmn.NewCMap(), | ||
} | ||
|
||
// Ensure we have a completely undeterministic PRNG. cmd.RandInt64() draws | ||
|
@@ -255,7 +257,7 @@ func (sw *Switch) StopPeerForError(peer Peer, reason interface{}) { | |
sw.stopAndRemovePeer(peer, reason) | ||
|
||
if peer.IsPersistent() { | ||
go sw.reconnectToPeer(peer) | ||
go sw.reconnectToPeer(peer.NodeInfo().NetAddress()) | ||
} | ||
} | ||
|
||
|
@@ -274,24 +276,28 @@ func (sw *Switch) stopAndRemovePeer(peer Peer, reason interface{}) { | |
} | ||
} | ||
|
||
// reconnectToPeer tries to reconnect to the peer, first repeatedly | ||
// reconnectToPeer tries to reconnect to the addr, first repeatedly | ||
// with a fixed interval, then with exponential backoff. | ||
// If no success after all that, it stops trying, and leaves it | ||
// to the PEX/Addrbook to find the peer again | ||
func (sw *Switch) reconnectToPeer(peer Peer) { | ||
// NOTE this will connect to the self reported address, | ||
// not necessarily the original we dialed | ||
netAddr := peer.NodeInfo().NetAddress() | ||
// to the PEX/Addrbook to find the peer with the addr again | ||
func (sw *Switch) reconnectToPeer(addr *NetAddress) { | ||
if sw.reconnecting.Has(string(addr.ID)) { | ||
return | ||
} | ||
|
||
sw.reconnecting.Set(string(addr.ID), addr) | ||
defer sw.reconnecting.Delete(string(addr.ID)) | ||
|
||
start := time.Now() | ||
sw.Logger.Info("Reconnecting to peer", "peer", peer) | ||
sw.Logger.Info("Reconnecting to peer", "addr", addr) | ||
for i := 0; i < reconnectAttempts; i++ { | ||
if !sw.IsRunning() { | ||
return | ||
} | ||
|
||
err := sw.DialPeerWithAddress(netAddr, true) | ||
err := sw.DialPeerWithAddress(addr, true) | ||
if err != nil { | ||
sw.Logger.Info("Error reconnecting to peer. Trying again", "tries", i, "err", err, "peer", peer) | ||
sw.Logger.Info("Error reconnecting to peer. Trying again", "tries", i, "err", err, "addr", addr) | ||
// sleep a set amount | ||
sw.randomSleep(reconnectInterval) | ||
continue | ||
|
@@ -301,7 +307,7 @@ func (sw *Switch) reconnectToPeer(peer Peer) { | |
} | ||
|
||
sw.Logger.Error("Failed to reconnect to peer. Beginning exponential backoff", | ||
"peer", peer, "elapsed", time.Since(start)) | ||
"addr", addr, "elapsed", time.Since(start)) | ||
for i := 0; i < reconnectBackOffAttempts; i++ { | ||
if !sw.IsRunning() { | ||
return | ||
|
@@ -310,13 +316,13 @@ func (sw *Switch) reconnectToPeer(peer Peer) { | |
// sleep an exponentially increasing amount | ||
sleepIntervalSeconds := math.Pow(reconnectBackOffBaseSeconds, float64(i)) | ||
sw.randomSleep(time.Duration(sleepIntervalSeconds) * time.Second) | ||
err := sw.DialPeerWithAddress(netAddr, true) | ||
err := sw.DialPeerWithAddress(addr, true) | ||
if err == nil { | ||
return // success | ||
} | ||
sw.Logger.Info("Error reconnecting to peer. Trying again", "tries", i, "err", err, "peer", peer) | ||
sw.Logger.Info("Error reconnecting to peer. Trying again", "tries", i, "err", err, "addr", addr) | ||
} | ||
sw.Logger.Error("Failed to reconnect to peer. Giving up", "peer", peer, "elapsed", time.Since(start)) | ||
sw.Logger.Error("Failed to reconnect to peer. Giving up", "addr", addr, "elapsed", time.Since(start)) | ||
} | ||
|
||
// SetAddrBook allows to set address book on Switch. | ||
|
@@ -470,6 +476,7 @@ func (sw *Switch) addOutboundPeerWithConfig(addr *NetAddress, config *PeerConfig | |
peerConn, err := newOutboundPeerConn(addr, config, persistent, sw.nodeKey.PrivKey) | ||
if err != nil { | ||
sw.Logger.Error("Failed to dial peer", "address", addr, "err", err) | ||
go sw.reconnectToPeer(addr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we only want to do this if the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
return err | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather not have functions inside config.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we can Have
badAddr
, which returns error onDialTimeout
. This will require to inroduce NetAddress interface.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think better would be to set a flag on the remotePeer to allow it to reject the the connection in its accept routine. We should try to avoid putting methods in config structs like this, and I'm not sure we want the NetAddress to be an interface (though perhaps I could be convinced, just feels like not the right way to solve the problem of writing these tests, even if its after all a good idea).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can have a the
Dial
func somewhere else? It seems more convenient to have a configurable dial behavior for testing, for e.g it could be used for slow connections / timeouts.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
Switch
?