network: register connected addr in handleVersionCmd()

Prevent useless attempts to connect to this peer if the peer has already made
a connection to us.
This commit is contained in:
Roman Khimov 2020-01-28 19:10:13 +03:00
parent 9eafec0d1d
commit eb4ec61b8b
3 changed files with 6 additions and 3 deletions

View file

@ -18,6 +18,7 @@ type Discoverer interface {
RequestRemote(int)
RegisterBadAddr(string)
RegisterGoodAddr(string)
RegisterConnectedAddr(string)
UnregisterConnectedAddr(string)
UnconnectedPeers() []string
BadPeers() []string
@ -153,8 +154,8 @@ func (d *DefaultDiscovery) UnregisterConnectedAddr(s string) {
d.lock.Unlock()
}
// registerConnectedAddr tells discoverer that given address is now connected.
func (d *DefaultDiscovery) registerConnectedAddr(addr string) {
// RegisterConnectedAddr tells discoverer that given address is now connected.
func (d *DefaultDiscovery) RegisterConnectedAddr(addr string) {
d.lock.Lock()
delete(d.unconnectedAddrs, addr)
d.connectedAddrs[addr] = true
@ -166,7 +167,7 @@ func (d *DefaultDiscovery) tryAddress(addr string) {
d.RegisterBadAddr(addr)
d.RequestRemote(1)
} else {
d.registerConnectedAddr(addr)
d.RegisterConnectedAddr(addr)
}
}

View file

@ -136,6 +136,7 @@ func (d testDiscovery) BackFill(addrs ...string) {}
func (d testDiscovery) PoolCount() int { return 0 }
func (d testDiscovery) RegisterBadAddr(string) {}
func (d testDiscovery) RegisterGoodAddr(string) {}
func (d testDiscovery) RegisterConnectedAddr(string) {}
func (d testDiscovery) UnregisterConnectedAddr(string) {}
func (d testDiscovery) UnconnectedPeers() []string { return []string{} }
func (d testDiscovery) RequestRemote(n int) {}

View file

@ -348,6 +348,7 @@ func (s *Server) handleVersionCmd(p Peer, version *payload.Version) error {
return errIdenticalID
}
peerAddr := p.PeerAddr().String()
s.discovery.RegisterConnectedAddr(peerAddr)
s.lock.RLock()
for peer := range s.peers {
if p == peer {