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()