From cb1291d565c3d4a747b09dfcea95efb272935412 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 25 Feb 2022 18:38:12 +1100 Subject: [PATCH] Separate torrent sources source file --- client.go | 61 ---------------------------------------------- sources.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 61 deletions(-) create mode 100644 sources.go diff --git a/client.go b/client.go index 07be8f3c..804b4c79 100644 --- a/client.go +++ b/client.go @@ -1336,67 +1336,6 @@ func (t *Torrent) MergeSpec(spec *TorrentSpec) error { return nil } -func (t *Torrent) useSources(sources []string) { - select { - case <-t.Closed(): - return - case <-t.GotInfo(): - return - default: - } - for _, s := range sources { - _, loaded := t.activeSources.LoadOrStore(s, struct{}{}) - if loaded { - continue - } - s := s - go func() { - err := t.useActiveTorrentSource(s) - _, loaded := t.activeSources.LoadAndDelete(s) - if !loaded { - panic(s) - } - level := log.Debug - if err != nil { - level = log.Warning - } - t.logger.Levelf(level, "used torrent source %q [err=%q]", s, err) - }() - } -} - -func (t *Torrent) useActiveTorrentSource(source string) error { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - go func() { - select { - case <-t.GotInfo(): - case <-t.Closed(): - case <-ctx.Done(): - } - cancel() - }() - mi, err := getTorrentSource(ctx, source, t.cl.httpClient) - if err != nil { - return err - } - return t.MergeSpec(TorrentSpecFromMetaInfo(&mi)) -} - -func getTorrentSource(ctx context.Context, source string, hc *http.Client) (mi metainfo.MetaInfo, err error) { - var req *http.Request - if req, err = http.NewRequestWithContext(ctx, http.MethodGet, source, nil); err != nil { - return - } - var resp *http.Response - if resp, err = hc.Do(req); err != nil { - return - } - defer resp.Body.Close() - err = bencode.NewDecoder(resp.Body).Decode(&mi) - return -} - func (cl *Client) dropTorrent(infoHash metainfo.Hash, wg *sync.WaitGroup) (err error) { t, ok := cl.torrents[infoHash] if !ok { diff --git a/sources.go b/sources.go new file mode 100644 index 00000000..b2d5b940 --- /dev/null +++ b/sources.go @@ -0,0 +1,71 @@ +package torrent + +import ( + "context" + "net/http" + + "github.com/anacrolix/log" + "github.com/anacrolix/torrent/bencode" + "github.com/anacrolix/torrent/metainfo" +) + +func (t *Torrent) useSources(sources []string) { + select { + case <-t.Closed(): + return + case <-t.GotInfo(): + return + default: + } + for _, s := range sources { + _, loaded := t.activeSources.LoadOrStore(s, struct{}{}) + if loaded { + continue + } + s := s + go func() { + err := t.useActiveTorrentSource(s) + _, loaded := t.activeSources.LoadAndDelete(s) + if !loaded { + panic(s) + } + level := log.Debug + if err != nil { + level = log.Warning + } + t.logger.Levelf(level, "used torrent source %q [err=%q]", s, err) + }() + } +} + +func (t *Torrent) useActiveTorrentSource(source string) error { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + go func() { + select { + case <-t.GotInfo(): + case <-t.Closed(): + case <-ctx.Done(): + } + cancel() + }() + mi, err := getTorrentSource(ctx, source, t.cl.httpClient) + if err != nil { + return err + } + return t.MergeSpec(TorrentSpecFromMetaInfo(&mi)) +} + +func getTorrentSource(ctx context.Context, source string, hc *http.Client) (mi metainfo.MetaInfo, err error) { + var req *http.Request + if req, err = http.NewRequestWithContext(ctx, http.MethodGet, source, nil); err != nil { + return + } + var resp *http.Response + if resp, err = hc.Do(req); err != nil { + return + } + defer resp.Body.Close() + err = bencode.NewDecoder(resp.Body).Decode(&mi) + return +}