Skip to content

Commit

Permalink
Address import loop
Browse files Browse the repository at this point in the history
Move the ConsistentHashingStrategyKey to client not config.
  • Loading branch information
tempusfrangit committed Feb 15, 2024
1 parent 7b6e180 commit e7d58cf
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 10 deletions.
7 changes: 5 additions & 2 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/hashicorp/go-retryablehttp"

"github.com/replicate/pget/pkg/config"
"github.com/replicate/pget/pkg/logging"
"github.com/replicate/pget/pkg/version"
)
Expand All @@ -24,6 +23,10 @@ const (
retryMaxWait = 1250 * time.Millisecond
)

type ConsistentHashingStrategy struct{}

var ConsistentHashingStrategyKey ConsistentHashingStrategy

var ErrStrategyFallback = errors.New("fallback to next strategy")

// HTTPClient is a wrapper around http.Client that allows for limiting the number of concurrent connections per host
Expand Down Expand Up @@ -111,7 +114,7 @@ func RetryPolicy(ctx context.Context, resp *http.Response, err error) (bool, err

// While type assertions are not ideal, alternatives are limited to adding custom data in the request
// or in the context. The context clearly isolates this data.
consistentHashing, ok := ctx.Value(config.ConsistentHashingStrategyKey).(bool)
consistentHashing, ok := ctx.Value(ConsistentHashingStrategyKey).(bool)
if ok && consistentHashing {
if fallbackError(err) {
return false, ErrStrategyFallback
Expand Down
3 changes: 1 addition & 2 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/stretchr/testify/assert"

"github.com/replicate/pget/pkg/client"
"github.com/replicate/pget/pkg/config"
)

func TestGetSchemeHostKey(t *testing.T) {
Expand All @@ -24,7 +23,7 @@ func TestGetSchemeHostKey(t *testing.T) {

func TestRetryPolicy(t *testing.T) {
bgCtx := context.Background()
chCtx := context.WithValue(bgCtx, config.ConsistentHashingStrategyKey, true)
chCtx := context.WithValue(bgCtx, client.ConsistentHashingStrategyKey, true)
errContext, cancel := context.WithCancel(bgCtx)
cancel()

Expand Down
4 changes: 0 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ var (
DefaultCacheURIPrefixes = []string{"https://weights.replicate.delivery"}
)

type ConsistentHashingStrategy struct{}

var ConsistentHashingStrategyKey ConsistentHashingStrategy

type DeprecatedFlag struct {
Flag string
Msg string
Expand Down
3 changes: 1 addition & 2 deletions pkg/download/consistent_hashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"golang.org/x/sync/errgroup"

"github.com/replicate/pget/pkg/client"
"github.com/replicate/pget/pkg/config"
"github.com/replicate/pget/pkg/consistent"
"github.com/replicate/pget/pkg/logging"
)
Expand Down Expand Up @@ -250,7 +249,7 @@ func (m *ConsistentHashingMode) Fetch(ctx context.Context, urlString string) (io
}

func (m *ConsistentHashingMode) DoRequest(ctx context.Context, start, end int64, urlString string) (*http.Response, error) {
chContext := context.WithValue(ctx, config.ConsistentHashingStrategyKey, true)
chContext := context.WithValue(ctx, client.ConsistentHashingStrategyKey, true)
req, err := http.NewRequestWithContext(chContext, "GET", urlString, nil)
if err != nil {
return nil, fmt.Errorf("failed to download %s: %w", req.URL.String(), err)
Expand Down

0 comments on commit e7d58cf

Please sign in to comment.