refactor: abstract away connection tie breaking
So we don't need to carry that logic in LocalPeer
This commit is contained in:
1 parent
6efa794ab4
commit
9302105a2e
2 files changed
+28
-9
No files matched your search
@@ -20,6 +20,14 @@ type Connection interface {
|
|||||||
Shutdown(error)
|
Shutdown(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ConnectionTieBreak int
|
||||||
|
|
||||||
|
const (
|
||||||
|
TieBreakWon ConnectionTieBreak = iota
|
||||||
|
TieBreakLost
|
||||||
|
TieBreakTied
|
||||||
|
)
|
||||||
|
|
||||||
type RemoteConnection struct {
|
type RemoteConnection struct {
|
||||||
local *Peer
|
local *Peer
|
||||||
remote *Peer
|
remote *Peer
|
||||||
@@ -116,6 +124,18 @@ func (conn *LocalConnection) Start(acceptNewPeer bool) {
|
|||||||
go conn.run(queryChan, acceptNewPeer)
|
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 {
|
func (conn *LocalConnection) Established() bool {
|
||||||
conn.RLock()
|
conn.RLock()
|
||||||
defer conn.RUnlock()
|
defer conn.RUnlock()
|
||||||
|
|||||||
@@ -189,21 +189,20 @@ func (peer *LocalPeer) handleAddConnection(conn *LocalConnection) {
|
|||||||
if dupConn == conn {
|
if dupConn == conn {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// conn.UID is used as the tie breaker here, in the
|
|
||||||
// knowledge that both sides will make the same decision.
|
|
||||||
dupConnLocal := dupConn.(*LocalConnection)
|
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.
|
// oh good grief. Sod it, just kill both of them.
|
||||||
conn.CheckFatal(dupErr)
|
conn.CheckFatal(dupErr)
|
||||||
dupConnLocal.CheckFatal(dupErr)
|
dupConnLocal.CheckFatal(dupErr)
|
||||||
peer.handleDeleteConnection(dupConnLocal)
|
peer.handleDeleteConnection(dupConnLocal)
|
||||||
return
|
return
|
||||||
} else if conn.UID < dupConnLocal.UID {
|
|
||||||
dupConnLocal.CheckFatal(dupErr)
|
|
||||||
peer.handleDeleteConnection(dupConnLocal)
|
|
||||||
} else {
|
|
||||||
conn.CheckFatal(dupErr)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := peer.checkConnectionLimit(); err != nil {
|
if err := peer.checkConnectionLimit(); err != nil {
|
||||||
|
|||||||
Reference in new issue
Block a user