From: Matt Joiner Date: Tue, 12 Aug 2025 13:27:36 +0000 (+1000) Subject: Add MetainfoSourcesMerger X-Git-Tag: v1.59.0~2^2~29 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=231b02a64d10d7b135dc6683c4e381a2fed4c3d0;p=btrtrc.git Add MetainfoSourcesMerger --- diff --git a/config.go b/config.go index 02eb61bf..12fa54cd 100644 --- a/config.go +++ b/config.go @@ -12,6 +12,7 @@ import ( "github.com/anacrolix/dht/v2/krpc" "github.com/anacrolix/log" "github.com/anacrolix/missinggo/v2" + "github.com/anacrolix/torrent/metainfo" "github.com/pion/webrtc/v4" "golang.org/x/time/rate" @@ -50,6 +51,7 @@ type ClientDhtConfig struct { type ClientConfig struct { ClientTrackerConfig ClientDhtConfig + MetainfoSourcesConfig // Store torrent file data in this directory unless DefaultStorage is // specified. @@ -111,9 +113,6 @@ type ClientConfig struct { Logger log.Logger Slogger *slog.Logger - // Used for torrent metainfo sources only. Falls back to the http.Client created to wrap - // WebTransport. - MetainfoSourcesClient *http.Client // Used for torrent sources and webseeding if set. WebTransport http.RoundTripper // Defines proxy for HTTP requests, such as for trackers. It's commonly set from the result of @@ -255,6 +254,9 @@ func NewDefaultClientConfig() *ClientConfig { return func() ([]dht.Addr, error) { return dht.GlobalBootstrapAddrs(network) } } cc.PeriodicallyAnnounceTorrentsToDht = true + cc.MetainfoSourcesMerger = func(t *Torrent, info *metainfo.MetaInfo) error { + return t.MergeSpec(TorrentSpecFromMetaInfo(info)) + } return cc } @@ -278,3 +280,13 @@ func EffectiveDownloadRateLimit(l *rate.Limiter) rate.Limit { } return l.Limit() } + +type MetainfoSourcesConfig struct { + // Used for torrent metainfo sources only. Falls back to the http.Client created to wrap + // WebTransport. + MetainfoSourcesClient *http.Client + // If a sources successfully fetches metainfo, this function is called to apply the metainfo. t + // is provided to prevent a race as the fetcher for the source was bound to it. Returning an + // error will kill the respective sourcer. + MetainfoSourcesMerger func(t *Torrent, info *metainfo.MetaInfo) error +} diff --git a/sources.go b/sources.go index f115c6c8..650d7a8c 100644 --- a/sources.go +++ b/sources.go @@ -64,7 +64,7 @@ func (t *Torrent) trySource(source string) (retry g.Option[time.Duration], err e retry.Set(time.Duration(rand.Int64N(int64(time.Minute)))) return } - err = t.MergeSpec(TorrentSpecFromMetaInfo(&mi)) + err = t.cl.config.MetainfoSourcesMerger(t, &mi) return }