From dd240b66c4ff619ca10e399e9200c855d75adf06 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 18 Nov 2014 22:03:21 -0600 Subject: [PATCH] Simple connection pruner --- client.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/client.go b/client.go index 4382d6bd..20957cae 100644 --- 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() -- 2.48.1