]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add MetainfoSourcesMerger
authorMatt Joiner <anacrolix@gmail.com>
Tue, 12 Aug 2025 13:27:36 +0000 (23:27 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 12 Aug 2025 13:27:36 +0000 (23:27 +1000)
config.go
sources.go

index 02eb61bf6498737a315ed46260006f45f18472e8..12fa54cdd832e2288e5c12e89f2edd590695dd1f 100644 (file)
--- 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
+}
index f115c6c8992cff07edcb12d6b6af67c19eaae468..650d7a8c3e578e44e0270b807ecdeccbafa84a22 100644 (file)
@@ -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
 }