refactor: abstract away connection tie breaking

So we don't need to carry that logic in LocalPeer
This commit is contained in:
Matthias Radestock committed 2015-01-21 20:42:27 +00:00
1 parent 6efa794ab4
commit 9302105a2e
2 files changed
+28 -9

No files matched your search

+20
View File
@@ -20,6 +20,14 @@ type Connection interface {
Shutdown(error)
}
type ConnectionTieBreak int
const (
TieBreakWon ConnectionTieBreak = iota
TieBreakLost
TieBreakTied
)
type RemoteConnection struct {
local *Peer
remote *Peer
@@ -116,6 +124,18 @@ func (conn *LocalConnection) Start(acceptNewPeer bool) {
go conn.run(queryChan, acceptNewPeer)
}
func (conn *LocalConnection) BreakTie(dupConn *LocalConnection) ConnectionTieBreak {
// conn.UID is used as the tie breaker here, in the knowledge that
// both sides will make the same decision.
if conn.UID < dupConn.UID {
return TieBreakWon
} else if dupConn.UID < conn.UID {
return TieBreakLost
} else {
return TieBreakTied
}
}
func (conn *LocalConnection) Established() bool {
conn.RLock()
defer conn.RUnlock()
+8 -9
View File
@@ -189,21 +189,20 @@ func (peer *LocalPeer) handleAddConnection(conn *LocalConnection) {
if dupConn == conn {
return
}
// conn.UID is used as the tie breaker here, in the
// knowledge that both sides will make the same decision.
dupConnLocal := dupConn.(*LocalConnection)
if conn.UID == dupConnLocal.UID {
switch conn.BreakTie(dupConnLocal) {
case TieBreakWon:
dupConnLocal.CheckFatal(dupErr)
peer.handleDeleteConnection(dupConnLocal)
case TieBreakLost:
conn.CheckFatal(dupErr)
return
case TieBreakTied:
// oh good grief. Sod it, just kill both of them.
conn.CheckFatal(dupErr)
dupConnLocal.CheckFatal(dupErr)
peer.handleDeleteConnection(dupConnLocal)
return
} else if conn.UID < dupConnLocal.UID {
dupConnLocal.CheckFatal(dupErr)
peer.handleDeleteConnection(dupConnLocal)
} else {
conn.CheckFatal(dupErr)
return
}
}
if err := peer.checkConnectionLimit(); err != nil {