]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add separate MetainfoSourcesClient
authorMatt Joiner <anacrolix@gmail.com>
Thu, 31 Jul 2025 01:16:27 +0000 (11:16 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 31 Jul 2025 01:16:27 +0000 (11:16 +1000)
client.go
config.go
sources.go

index 999282cf3bdecb68ff73b847d55270d21327df18..a0874f3c17f143ed562d09faa95b720693288a34 100644 (file)
--- a/client.go
+++ b/client.go
@@ -100,7 +100,8 @@ type Client struct {
        numWebSeedRequests map[webseedHostKeyHandle]int
 
        activeAnnounceLimiter limiter.Instance
-       httpClient            *http.Client
+       // TODO: Move this onto ClientConfig.
+       httpClient *http.Client
 
        clientHolepunchAddrSets
 
@@ -243,6 +244,7 @@ func (cl *Client) announceKey() int32 {
 // Initializes a bare minimum Client. *Client and *ClientConfig must not be nil.
 func (cl *Client) init(cfg *ClientConfig) {
        cl.config = cfg
+       cfg.setRateLimiterBursts()
        g.MakeMap(&cl.dopplegangerAddrs)
        g.MakeMap(&cl.torrentsByShortHash)
        g.MakeMap(&cl.torrents)
@@ -262,6 +264,7 @@ func (cl *Client) init(cfg *ClientConfig) {
                        MaxConnsPerHost: 10,
                }
        }
+       cfg.MetainfoSourcesClient = cmp.Or(cfg.MetainfoSourcesClient, cl.httpClient)
        cl.defaultLocalLtepProtocolMap = makeBuiltinLtepProtocols(!cfg.DisablePEX)
        g.MakeMap(&cl.numWebSeedRequests)
 }
@@ -273,7 +276,6 @@ func NewClient(cfg *ClientConfig) (cl *Client, err error) {
                cfg = NewDefaultClientConfig()
                cfg.ListenPort = 0
        }
-       cfg.setRateLimiterBursts()
        cl = &Client{}
        cl.init(cfg)
        go cl.acceptLimitClearer()
index 05da2bf4d9a652ac9073742194445d78aebf7bf7..817856c4aaec997e7cbc2633e35887fd8b14a7c7 100644 (file)
--- a/config.go
+++ b/config.go
@@ -46,7 +46,7 @@ type ClientDhtConfig struct {
        DHTOnQuery func(query *krpc.Msg, source net.Addr) (propagate bool)
 }
 
-// Probably not safe to modify this after it's given to a Client.
+// Probably not safe to modify this after it's given to a Client, or to pass it to multiple Clients.
 type ClientConfig struct {
        ClientTrackerConfig
        ClientDhtConfig
@@ -109,6 +109,9 @@ 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
index 18aa6975478a0f97183f03ec1af14d3cfa7164fa..f115c6c8992cff07edcb12d6b6af67c19eaae468 100644 (file)
@@ -56,7 +56,7 @@ func (t *Torrent) trySource(source string) (retry g.Option[time.Duration], err e
                return
        }
        var mi metainfo.MetaInfo
-       mi, err = getTorrentSource(ctx, source, t.cl.httpClient)
+       mi, err = getTorrentSource(ctx, source, t.cl.config.MetainfoSourcesClient)
        if ctx.Err() != nil {
                return
        }