[NOD-1289] Check if connection exists before establishing another one with the same address (#883)

This commit is contained in:
Ori Newman 2020-08-20 11:50:29 +03:00 committed by GitHub
parent fcae491e6d
commit c331293a2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 1 deletions

View File

@ -63,6 +63,7 @@ func (c *ConnectionManager) checkRequestedConnections(connSet connectionSet) {
}
// try to initiate connection
log.Debugf("Connecting to connection request %s", connReq.address)
err := c.initiateConnection(connReq.address)
if err != nil {
log.Infof("Couldn't connect to %s: %s", address, err)

View File

@ -143,3 +143,19 @@ func (c *ConnectionManager) waitTillNextIteration() {
case <-c.loopTicker.C:
}
}
func (c *ConnectionManager) connectionExists(addressString string) bool {
if _, ok := c.activeRequested[addressString]; ok {
return true
}
if _, ok := c.activeOutgoing[addressString]; ok {
return true
}
if _, ok := c.activeIncoming[addressString]; ok {
return true
}
return false
}

View File

@ -39,6 +39,12 @@ func (c *ConnectionManager) checkOutgoingConnections(connSet connectionSet) {
netAddress := address.NetAddress()
tcpAddress := netAddress.TCPAddress()
addressString := tcpAddress.String()
if c.connectionExists(addressString) {
log.Debugf("Fetched address %s from address manager but it's already connected. Skipping...", addressString)
continue
}
isBanned, err := c.addressManager.IsBanned(netAddress)
if err != nil {
log.Infof("Couldn't resolve whether %s is banned: %s", addressString, err)
@ -49,6 +55,8 @@ func (c *ConnectionManager) checkOutgoingConnections(connSet connectionSet) {
}
c.addressManager.Attempt(netAddress)
log.Debugf("Connecting to %s because we have %d outgoing connections and the target is "+
"%d", addressString, len(c.activeOutgoing), c.targetOutgoing)
err = c.initiateConnection(addressString)
if err != nil {
log.Infof("Couldn't connect to %s: %s", addressString, err)

View File

@ -53,7 +53,7 @@ func (c *gRPCConnection) Start(router *router.Router) {
spawn("gRPCConnection.Start-connectionLoops", func() {
err := c.connectionLoops()
if err != nil {
log.Errorf("error from connectionLoops for %s: %+v", c.address, err)
log.Errorf("error from connectionLoops for %s: %s", c.address, err)
}
})
}