Skip to content

Commit

Permalink
Move who owns HTTPClient
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat committed Jan 31, 2024
1 parent fa7454c commit 794bd97
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
5 changes: 4 additions & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package httprc
import (
"context"
"fmt"
"net/http"
"sync"
"time"
)
Expand Down Expand Up @@ -60,6 +61,7 @@ func NewCache(ctx context.Context, options ...CacheOption) *Cache {
var errSink ErrSink
var wl Whitelist
var nworkers int
var client = http.DefaultClient
for _, option := range options {
//nolint:forcetypeassert
switch option.Ident() {
Expand All @@ -72,6 +74,7 @@ func NewCache(ctx context.Context, options ...CacheOption) *Cache {
case identFetcherWorkerCount{}:
nworkers = option.Value().(int)
}
// TODO add HTTPClient option
}

if refreshWindow < time.Second {
Expand All @@ -81,7 +84,7 @@ func NewCache(ctx context.Context, options ...CacheOption) *Cache {
registry := newRegistry()

fetch := newFetcher(ctx, nworkers, wl)
queue := newQueue(ctx, registry, refreshWindow, fetch, errSink)
queue := newQueue(ctx, registry, refreshWindow, fetch, client, errSink)

return &Cache{
queue: queue,
Expand Down
9 changes: 7 additions & 2 deletions queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type queue struct {
fetch *fetcher
fetchCond *sync.Cond
fetchQueue []*rqentry
client HTTPClient

// list is a sorted list of urls to their expected fire time
// when we get a new tick in the RQ loop, we process everything
Expand All @@ -90,14 +91,18 @@ func (cf clockFunc) Now() time.Time {
return cf()
}

func newQueue(ctx context.Context, registry *registry, window time.Duration, fetch *fetcher, errSink ErrSink) *queue {
func newQueue(ctx context.Context, registry *registry, window time.Duration, fetch *fetcher, client HTTPClient, errSink ErrSink) *queue {
if client == nil {
client = http.DefaultClient
}
fetchLocker := &sync.Mutex{}
rq := &queue{
windowSize: window,
fetch: fetch,
fetchCond: sync.NewCond(fetchLocker),
registry: registry,
clock: clockFunc(time.Now),
client: client,
}

go rq.refreshLoop(ctx, errSink)
Expand All @@ -107,7 +112,7 @@ func newQueue(ctx context.Context, registry *registry, window time.Duration, fet

func (q *queue) Register(u string, options ...RegisterOption) error {
var refreshInterval time.Duration
var client HTTPClient
var client HTTPClient = q.client

Check warning on line 115 in queue.go

View workflow job for this annotation

GitHub Actions / lint (1.19)

var-declaration: should omit type HTTPClient from declaration of var client; it will be inferred from the right-hand side (revive)
var wl Whitelist
var transform Transformer = BodyBytes{}

Expand Down
2 changes: 1 addition & 1 deletion queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestQueue(t *testing.T) {
defer srv.Close()

f := newFetcher(ctx, 0, InsecureWhitelist{})
q := newQueue(ctx, newRegistry(), 15*time.Minute, f, &noErrorSink{t: t})
q := newQueue(ctx, newRegistry(), 15*time.Minute, f, &dummyClient{srv: srv}, &noErrorSink{t: t})

base := time.Now()
q.clock = clockFunc(func() time.Time {
Expand Down

0 comments on commit 794bd97

Please sign in to comment.