From 6aa459dc0dddea5ce14869f3b0a0dedb713ddfe0 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 25 Aug 2014 06:01:05 +1000 Subject: [PATCH] Stop announcing on DHT if a torrent is removed --- client.go | 5 +++++ torrent.go | 12 ++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index e4e9cecb..ecbbcc4c 100644 --- a/client.go +++ b/client.go @@ -961,6 +961,8 @@ func newTorrent(ih InfoHash, announceList [][]string) (t *torrent, err error) { t = &torrent{ InfoHash: ih, Peers: make(map[peersKey]Peer, 2000), + + closing: make(chan struct{}), } t.Trackers = make([][]tracker.Client, len(announceList)) for tierIndex := range announceList { @@ -1115,6 +1117,9 @@ func (cl *Client) announceTorrentDHT(t *torrent) { log.Printf("error adding peers from dht for torrent %q: %s", t, err) break getPeers } + case <-t.closing: + ps.Close() + return } } ps.Close() diff --git a/torrent.go b/torrent.go index 07a18c48..82256f1e 100644 --- a/torrent.go +++ b/torrent.go @@ -334,11 +334,19 @@ func (t *torrent) Length() int64 { } func (t *torrent) isClosed() bool { - return t.closed + select { + case <-t.closing: + return true + default: + return false + } } func (t *torrent) Close() (err error) { - t.closed = true + if t.isClosed() { + return + } + close(t.closing) t.dataLock.Lock() t.Data.Close() t.Data = nil