From 94e245c5f5ef3f36910e50e3b8a5a92ae38f2c76 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Thu, 31 Jul 2025 11:16:27 +1000 Subject: [PATCH] Add separate MetainfoSourcesClient --- client.go | 6 ++++-- config.go | 5 ++++- sources.go | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index 999282cf..a0874f3c 100644 --- 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() diff --git a/config.go b/config.go index 05da2bf4..817856c4 100644 --- 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 diff --git a/sources.go b/sources.go index 18aa6975..f115c6c8 100644 --- a/sources.go +++ b/sources.go @@ -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 } -- 2.51.0