Skip to content

Commit 19e21a3

Browse files
authored
Fix Backfill HTTP Usage (#1350)
Using a local `client` is not the right thing to do in `fetchRepo`. Also, this exposes a `Client` option to callers, and defaults to the existing value if not passed Seeing this on a big backfill <img width="2553" height="783" alt="image" src="https://github.com/user-attachments/assets/29e297b9-e8fb-4c53-88df-f318e68dcef2" />
2 parents 05a727c + 9d9b40e commit 19e21a3

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

backfill/backfill.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ type Backfiller struct {
8585

8686
tryRelayRepoFetch bool
8787

88+
httpClient *http.Client
89+
8890
stop chan chan struct{}
8991

9092
Directory identity.Directory
@@ -119,6 +121,10 @@ type BackfillOptions struct {
119121
NSIDFilter string
120122
SyncRequestsPerSecond int
121123
RelayHost string
124+
125+
// Client is an optional HTTP client to use for fetching repos from PDS servers.
126+
// If nil, a default client will be created.
127+
Client *http.Client
122128
}
123129

124130
func DefaultBackfillOptions() *BackfillOptions {
@@ -151,6 +157,14 @@ func NewBackfiller(
151157
opts.RelayHost = "http://" + opts.RelayHost[5:]
152158
}
153159

160+
httpClient := opts.Client
161+
if httpClient == nil {
162+
httpClient = &http.Client{
163+
Transport: otelhttp.NewTransport(http.DefaultTransport),
164+
Timeout: 600 * time.Second,
165+
}
166+
}
167+
154168
return &Backfiller{
155169
Name: name,
156170
Store: store,
@@ -162,6 +176,7 @@ func NewBackfiller(
162176
NSIDFilter: opts.NSIDFilter,
163177
syncLimiter: rate.NewLimiter(rate.Limit(opts.SyncRequestsPerSecond), 1),
164178
RelayHost: opts.RelayHost,
179+
httpClient: httpClient,
165180
stop: make(chan chan struct{}, 1),
166181
Directory: identity.DefaultDirectory(),
167182
}
@@ -334,10 +349,6 @@ func (b *Backfiller) fetchRepo(ctx context.Context, did, since, host string) (io
334349
}
335350

336351
// GET and CAR decode the body
337-
client := &http.Client{
338-
Transport: otelhttp.NewTransport(http.DefaultTransport),
339-
Timeout: 600 * time.Second,
340-
}
341352
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
342353
if err != nil {
343354
return nil, fmt.Errorf("failed to create request: %w", err)
@@ -351,7 +362,7 @@ func (b *Backfiller) fetchRepo(ctx context.Context, did, since, host string) (io
351362

352363
b.syncLimiter.Wait(ctx)
353364

354-
resp, err := client.Do(req)
365+
resp, err := b.httpClient.Do(req)
355366
if err != nil {
356367
return nil, fmt.Errorf("failed to send request: %w", err)
357368
}

0 commit comments

Comments
 (0)