]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Tidy up the duplicate conn selection code
authorMatt Joiner <anacrolix@gmail.com>
Fri, 26 May 2017 03:00:20 +0000 (13:00 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Fri, 26 May 2017 03:00:28 +0000 (13:00 +1000)
torrent.go

index 4db2eb3133e79c6f61815766b9051a9821b6af1c..6da9d46a704e24e6d28283d9955a7a817bb6a551 100644 (file)
@@ -1305,20 +1305,21 @@ func (t *Torrent) addConnection(c *connection, outgoing bool) bool {
        }
        for c0 := range t.conns {
                if c.PeerID == c0.PeerID {
-                       // Retain the connection from lower peer ID to higher.
+                       // Already connected to a client with that ID.
+                       duplicateClientConns.Add(1)
                        lower := string(t.cl.peerID[:]) < string(c.PeerID[:])
-                       if (outgoing && lower) || (!outgoing && !lower) {
+                       // Retain the connection from initiated from lower peer ID to
+                       // higher.
+                       if outgoing == lower {
+                               // Close the other one.
                                c0.Close()
+                               // Is it safe to delete from the map while we're iterating
+                               // over it?
                                t.deleteConnection(c0)
-                               duplicateClientConns.Add(1)
-                               log.Printf("Drop connection: %s, %s, %s", t.name(), c0.localAddr(), c0.remoteAddr())
-                               continue
+                       } else {
+                               // Abandon this one.
+                               return false
                        }
-
-                       // Already connected to a client with that ID.
-                       duplicateClientConns.Add(1)
-                       log.Printf("Drop connection: %s, %s, %s", t.name(), c.localAddr(), c.remoteAddr())
-                       return false
                }
        }
        if len(t.conns) >= t.maxEstablishedConns {