]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Simple connection pruner
authorMatt Joiner <anacrolix@gmail.com>
Wed, 19 Nov 2014 04:03:21 +0000 (22:03 -0600)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 19 Nov 2014 04:03:21 +0000 (22:03 -0600)
client.go

index 4382d6bdfd2f8d2d9b7a5f2607e5239b3ab9961f..20957caea2f843b0645a8b3dc6f658453bc042c4 100644 (file)
--- a/client.go
+++ b/client.go
@@ -1301,9 +1301,32 @@ func (cl *Client) AddMagnet(uri string) (t Torrent, err error) {
        if err != nil {
                t.Close()
        }
+       go cl.connectionPruner(t.torrent)
        return
 }
 
+func (cl *Client) connectionPruner(t *torrent) {
+       for {
+               time.Sleep(15 * time.Second)
+               cl.mu.Lock()
+               license := len(t.Conns) - (socketsPerTorrent+1)/2
+               for _, c := range t.Conns {
+                       if license <= 0 {
+                               break
+                       }
+                       if time.Now().Sub(c.lastUsefulChunkReceived) < time.Minute {
+                               continue
+                       }
+                       if time.Now().Sub(c.completedHandshake) < time.Minute {
+                               continue
+                       }
+                       c.Close()
+                       license--
+               }
+               cl.mu.Unlock()
+       }
+}
+
 func (me *Client) DropTorrent(infoHash InfoHash) (err error) {
        me.mu.Lock()
        defer me.mu.Unlock()