From 305e7d04ca3075d71442d993f10033955c9efb09 Mon Sep 17 00:00:00 2001 From: Vladimir Erdman Date: Wed, 15 Jan 2025 13:28:54 +0400 Subject: [PATCH 1/2] Add stash timeout --- cmd/proxy/actions/app_proxy.go | 2 +- pkg/download/protocol_test.go | 6 +++--- pkg/stash/stasher.go | 7 ++++--- pkg/stash/stasher_test.go | 3 ++- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/cmd/proxy/actions/app_proxy.go b/cmd/proxy/actions/app_proxy.go index 86c2a2478..82dfebecf 100644 --- a/cmd/proxy/actions/app_proxy.go +++ b/cmd/proxy/actions/app_proxy.go @@ -105,7 +105,7 @@ func addProxyRoutes( if err != nil { return err } - st := stash.New(mf, s, indexer, stash.WithPool(c.GoGetWorkers), withSingleFlight) + st := stash.New(mf, s, indexer, c.TimeoutDuration(), stash.WithPool(c.GoGetWorkers), withSingleFlight) df, err := mode.NewFile(c.DownloadMode, c.DownloadURL) if err != nil { diff --git a/pkg/download/protocol_test.go b/pkg/download/protocol_test.go index d7070e013..367699b3f 100644 --- a/pkg/download/protocol_test.go +++ b/pkg/download/protocol_test.go @@ -46,7 +46,7 @@ func getDP(t *testing.T) Protocol { if err != nil { t.Fatal(err) } - st := stash.New(mf, s, nop.New()) + st := stash.New(mf, s, nop.New(), 10*time.Minute) return New(&Opts{ Storage: s, Stasher: st, @@ -382,7 +382,7 @@ func TestDownloadProtocol(t *testing.T) { t.Fatal(err) } mp := &mockFetcher{} - st := stash.New(mp, s, nop.New()) + st := stash.New(mp, s, nop.New(), 10*time.Minute) dp := New(&Opts{s, st, nil, nil, Strict}) ctx := context.Background() @@ -434,7 +434,7 @@ func TestDownloadProtocolWhenFetchFails(t *testing.T) { t.Fatal(err) } mp := ¬FoundFetcher{} - st := stash.New(mp, s, nop.New()) + st := stash.New(mp, s, nop.New(), 10*time.Minute) dp := New(&Opts{s, st, nil, nil, Strict}) ctx := context.Background() _, err = dp.GoMod(ctx, fakeMod.mod, fakeMod.ver) diff --git a/pkg/stash/stasher.go b/pkg/stash/stasher.go index 68365cf57..267f38daa 100644 --- a/pkg/stash/stasher.go +++ b/pkg/stash/stasher.go @@ -28,8 +28,8 @@ type Wrapper func(Stasher) Stasher // New returns a plain stasher that takes // a module from a download.Protocol and // stashes it into a backend.Storage. -func New(f module.Fetcher, s storage.Backend, indexer index.Indexer, wrappers ...Wrapper) Stasher { - var st Stasher = &stasher{f, s, storage.WithChecker(s), indexer} +func New(f module.Fetcher, s storage.Backend, indexer index.Indexer, timeout time.Duration, wrappers ...Wrapper) Stasher { + var st Stasher = &stasher{f, s, storage.WithChecker(s), indexer, timeout} for _, w := range wrappers { st = w(st) } @@ -42,6 +42,7 @@ type stasher struct { storage storage.Backend checker storage.Checker indexer index.Indexer + timeout time.Duration } func (s *stasher) Stash(ctx context.Context, mod, ver string) (string, error) { @@ -52,7 +53,7 @@ func (s *stasher) Stash(ctx context.Context, mod, ver string) (string, error) { // create a new context that ditches whatever deadline the caller passed // but keep the tracing info so that we can properly trace the whole thing. - ctx, cancel := context.WithTimeout(trace.NewContext(context.Background(), span), time.Minute*10) + ctx, cancel := context.WithTimeout(trace.NewContext(context.Background(), span), s.timeout) defer cancel() v, err := s.fetchModule(ctx, mod, ver) if err != nil { diff --git a/pkg/stash/stasher_test.go b/pkg/stash/stasher_test.go index 702915778..b341e9ace 100644 --- a/pkg/stash/stasher_test.go +++ b/pkg/stash/stasher_test.go @@ -8,6 +8,7 @@ import ( "github.com/gomods/athens/pkg/index/nop" "github.com/gomods/athens/pkg/storage" + "time" ) type stashTest struct { @@ -54,7 +55,7 @@ func TestStash(t *testing.T) { var mf mockFetcher mf.ver = testCase.modVer - s := New(&mf, &ms, nop.New()) + s := New(&mf, &ms, nop.New(), 10*time.Minute) newVersion, err := s.Stash(context.Background(), "module", testCase.ver) if err != nil { t.Fatal(err) From 68473b0dbe087336d88704e042a085c6dc240f60 Mon Sep 17 00:00:00 2001 From: Vladimir Erdman Date: Wed, 15 Jan 2025 13:50:04 +0400 Subject: [PATCH 2/2] sort imports --- pkg/stash/stasher_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/stash/stasher_test.go b/pkg/stash/stasher_test.go index b341e9ace..d44f39de1 100644 --- a/pkg/stash/stasher_test.go +++ b/pkg/stash/stasher_test.go @@ -5,10 +5,10 @@ import ( "io" "strings" "testing" + "time" "github.com/gomods/athens/pkg/index/nop" "github.com/gomods/athens/pkg/storage" - "time" ) type stashTest struct {