]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Allow dropping connections to the same peer ID
authorMatt Joiner <anacrolix@gmail.com>
Sat, 16 Jun 2018 06:40:37 +0000 (16:40 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Sat, 16 Jun 2018 06:40:37 +0000 (16:40 +1000)
Necessary for a test that expects one connection to each other Client.

config.go
torrent.go

index f438cbce8e93a27804d24c60f723836d506882b6..899a2f062f5deb55df1ccc95c76f9bec894bf9db 100644 (file)
--- a/config.go
+++ b/config.go
@@ -110,6 +110,7 @@ type ClientConfig struct {
        PublicIp6 net.IP
 
        DisableAcceptRateLimiting bool
+       dropDuplicatePeerIds      bool
 }
 
 func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig {
index 62c61a10edcdf0a8afa9ffabe8b81adb1044a88f..3259dcc4d9db45ad542b1beab0a99612a03653b3 100644 (file)
@@ -1503,6 +1503,20 @@ func (t *Torrent) addConnection(c *connection) (err error) {
        if t.closed.IsSet() {
                return errors.New("torrent closed")
        }
+       for c0 := range t.conns {
+               if c.PeerID != c0.PeerID {
+                       continue
+               }
+               if !t.cl.config.dropDuplicatePeerIds {
+                       continue
+               }
+               if left, ok := c.hasPreferredNetworkOver(c0); ok && left {
+                       c0.Close()
+                       t.deleteConnection(c0)
+               } else {
+                       return errors.New("existing connection preferred")
+               }
+       }
        if !t.wantConns() {
                return errors.New("don't want conns")
        }