store connections as a set instead of map-of-remote-addresses

...since it is theoretically possible that the lifecycle events from
two consecutive inbound connections from the same remote address
overlap.
This commit is contained in:
Matthias Radestock committed 2015-09-30 17:29:13 +01:00
1 parent e68ca761a9
commit 0159e26631
2 files changed
+18 -17

No files matched your search

+16 -15
View File
@@ -20,7 +20,7 @@ type ConnectionMaker struct {
port int port int
discovery bool discovery bool
targets map[string]*Target targets map[string]*Target
connections map[string]Connection connections map[Connection]struct{}
directPeers peerAddrs directPeers peerAddrs
actionChan chan<- ConnectionMakerAction actionChan chan<- ConnectionMakerAction
} }
@@ -44,7 +44,7 @@ func NewConnectionMaker(ourself *LocalPeer, peers *Peers, port int, discovery bo
discovery: discovery, discovery: discovery,
directPeers: peerAddrs{}, directPeers: peerAddrs{},
targets: make(map[string]*Target), targets: make(map[string]*Target),
connections: make(map[string]Connection), connections: make(map[Connection]struct{}),
actionChan: actionChan} actionChan: actionChan}
go cm.queryLoop(actionChan) go cm.queryLoop(actionChan)
return cm return cm
@@ -99,10 +99,9 @@ func (cm *ConnectionMaker) ConnectionAborted(address string, err error) {
func (cm *ConnectionMaker) ConnectionCreated(conn Connection) { func (cm *ConnectionMaker) ConnectionCreated(conn Connection) {
cm.actionChan <- func() bool { cm.actionChan <- func() bool {
address := conn.RemoteTCPAddr() cm.connections[conn] = void
cm.connections[address] = conn
if conn.Outbound() { if conn.Outbound() {
delete(cm.targets, address) delete(cm.targets, conn.RemoteTCPAddr())
} }
return false return false
} }
@@ -110,10 +109,9 @@ func (cm *ConnectionMaker) ConnectionCreated(conn Connection) {
func (cm *ConnectionMaker) ConnectionTerminated(conn Connection, err error) { func (cm *ConnectionMaker) ConnectionTerminated(conn Connection, err error) {
cm.actionChan <- func() bool { cm.actionChan <- func() bool {
address := conn.RemoteTCPAddr() delete(cm.connections, conn)
delete(cm.connections, address)
if conn.Outbound() { if conn.Outbound() {
cm.retry(address, err) cm.retry(conn.RemoteTCPAddr(), err)
} }
return true return true
} }
@@ -143,10 +141,10 @@ func (cm *ConnectionMaker) checkStateAndAttemptConnections() time.Duration {
validTarget = make(map[string]struct{}) validTarget = make(map[string]struct{})
directTarget = make(map[string]struct{}) directTarget = make(map[string]struct{})
) )
ourConnectedPeers, ourInboundIPs := cm.ourConnections() ourConnectedPeers, ourConnectedTargets, ourInboundIPs := cm.ourConnections()
addTarget := func(address string) { addTarget := func(address string) {
if _, connected := cm.connections[address]; connected { if _, connected := ourConnectedTargets[address]; connected {
return return
} }
validTarget[address] = void validTarget[address] = void
@@ -187,13 +185,16 @@ func (cm *ConnectionMaker) checkStateAndAttemptConnections() time.Duration {
return cm.connectToTargets(validTarget, directTarget) return cm.connectToTargets(validTarget, directTarget)
} }
func (cm *ConnectionMaker) ourConnections() (PeerNameSet, map[string]struct{}) { func (cm *ConnectionMaker) ourConnections() (PeerNameSet, map[string]struct{}, map[string]struct{}) {
var ( var (
ourConnectedPeers = make(PeerNameSet) ourConnectedPeers = make(PeerNameSet)
ourInboundIPs = make(map[string]struct{}) ourConnectedTargets = make(map[string]struct{})
ourInboundIPs = make(map[string]struct{})
) )
for address, conn := range cm.connections { for conn := range cm.connections {
address := conn.RemoteTCPAddr()
ourConnectedPeers[conn.Remote().Name] = void ourConnectedPeers[conn.Remote().Name] = void
ourConnectedTargets[address] = void
if conn.Outbound() { if conn.Outbound() {
continue continue
} }
@@ -201,7 +202,7 @@ func (cm *ConnectionMaker) ourConnections() (PeerNameSet, map[string]struct{}) {
ourInboundIPs[ip] = void ourInboundIPs[ip] = void
} }
} }
return ourConnectedPeers, ourInboundIPs return ourConnectedPeers, ourConnectedTargets, ourInboundIPs
} }
func (cm *ConnectionMaker) addPeerTargets(ourConnectedPeers PeerNameSet, addTarget func(string)) { func (cm *ConnectionMaker) addPeerTargets(ourConnectedPeers PeerNameSet, addTarget func(string)) {
+2 -2
View File
@@ -165,12 +165,12 @@ func NewLocalConnectionStatusSlice(cm *ConnectionMaker) []LocalConnectionStatus
resultChan := make(chan []LocalConnectionStatus, 0) resultChan := make(chan []LocalConnectionStatus, 0)
cm.actionChan <- func() bool { cm.actionChan <- func() bool {
var slice []LocalConnectionStatus var slice []LocalConnectionStatus
for address, conn := range cm.connections { for conn := range cm.connections {
state := "pending" state := "pending"
if conn.Established() { if conn.Established() {
state = "established" state = "established"
} }
slice = append(slice, LocalConnectionStatus{address, conn.Outbound(), state, conn.Remote().String()}) slice = append(slice, LocalConnectionStatus{conn.RemoteTCPAddr(), conn.Outbound(), state, conn.Remote().String()})
} }
for address, target := range cm.targets { for address, target := range cm.targets {
var state, info string var state, info string