From 484b020556d5b8aaa554f0241caa03dc08cdffa0 Mon Sep 17 00:00:00 2001 From: Tyler Bui-Palsulich <26876514+tbpg@users.noreply.github.com> Date: Mon, 23 Jul 2018 23:25:38 -0400 Subject: [PATCH] storage: add context to storage.List (#321) --- pkg/download/list.go | 2 +- pkg/storage/backendconnector.go | 4 ++-- pkg/storage/fs/lister.go | 4 +++- pkg/storage/gcp/gcp_test.go | 4 ++-- pkg/storage/gcp/lister.go | 3 +-- pkg/storage/lister.go | 4 +++- pkg/storage/minio/lister.go | 3 ++- pkg/storage/mongo/lister.go | 3 ++- pkg/storage/rdbms/lister.go | 4 +++- pkg/storage/storage_tests/module_storage/storage_test.go | 4 ++-- 10 files changed, 21 insertions(+), 14 deletions(-) diff --git a/pkg/download/list.go b/pkg/download/list.go index 57853507..355e6b42 100644 --- a/pkg/download/list.go +++ b/pkg/download/list.go @@ -24,7 +24,7 @@ func ListHandler(lister storage.Lister, eng *render.Engine) func(c buffalo.Conte if err != nil { return err } - versions, err := lister.List(mod) + versions, err := lister.List(c, mod) if storage.IsNotFoundError(err) { return c.Render(http.StatusNotFound, eng.JSON(err.Error())) } else if err != nil { diff --git a/pkg/storage/backendconnector.go b/pkg/storage/backendconnector.go index 45846d41..eb1a6364 100644 --- a/pkg/storage/backendconnector.go +++ b/pkg/storage/backendconnector.go @@ -31,8 +31,8 @@ func (n noOpConnectedBackend) Exists(module, version string) bool { func (n noOpConnectedBackend) Get(module, vsn string) (*Version, error) { return n.backend.Get(module, vsn) } -func (n noOpConnectedBackend) List(module string) ([]string, error) { - return n.backend.List(module) +func (n noOpConnectedBackend) List(ctx context.Context, module string) ([]string, error) { + return n.backend.List(ctx, module) } func (n noOpConnectedBackend) Save(ctx context.Context, module, version string, mod []byte, zip io.Reader, info []byte) error { return n.backend.Save(ctx, module, version, mod, zip, info) diff --git a/pkg/storage/fs/lister.go b/pkg/storage/fs/lister.go index 8f52c124..60fb0fce 100644 --- a/pkg/storage/fs/lister.go +++ b/pkg/storage/fs/lister.go @@ -1,10 +1,12 @@ package fs import ( + "context" + "github.com/spf13/afero" ) -func (l *storageImpl) List(module string) ([]string, error) { +func (l *storageImpl) List(ctx context.Context, module string) ([]string, error) { loc := l.moduleLocation(module) fileInfos, err := afero.ReadDir(l.filesystem, loc) if err != nil { diff --git a/pkg/storage/gcp/gcp_test.go b/pkg/storage/gcp/gcp_test.go index 7fd3454e..7dde550d 100644 --- a/pkg/storage/gcp/gcp_test.go +++ b/pkg/storage/gcp/gcp_test.go @@ -41,7 +41,7 @@ func (g *GcpTests) TestSaveGetListExistsRoundTrip() { }) g.T().Run("List module versions", func(t *testing.T) { - versionList, err := store.List(g.module) + versionList, err := store.List(g.context, g.module) r.NoError(err) r.Equal(1, len(versionList)) r.Equal(g.version, versionList[0]) @@ -84,7 +84,7 @@ func (g *GcpTests) TestNotFounds() { }) g.T().Run("List not found", func(t *testing.T) { - _, err = store.List("nothing/to/see/here") + _, err = store.List(g.context, "nothing/to/see/here") modNotFoundErr := athensStorage.ErrNotFound{Module: "nothing/to/see/here"} r.EqualError(modNotFoundErr, err.Error()) }) diff --git a/pkg/storage/gcp/lister.go b/pkg/storage/gcp/lister.go index 547604d2..df9a5432 100644 --- a/pkg/storage/gcp/lister.go +++ b/pkg/storage/gcp/lister.go @@ -9,8 +9,7 @@ import ( // List implements the (./pkg/storage).Lister interface // It returns a list of versions, if any, for a given module -func (s *Storage) List(module string) ([]string, error) { - ctx := context.Background() +func (s *Storage) List(ctx context.Context, module string) ([]string, error) { paths, err := s.bucket.List(ctx, module) if err != nil { return nil, err diff --git a/pkg/storage/lister.go b/pkg/storage/lister.go index 0672d9a2..30135b5b 100644 --- a/pkg/storage/lister.go +++ b/pkg/storage/lister.go @@ -1,8 +1,10 @@ package storage +import "context" + // Lister is the interface that lists versions of a specific baseURL & module type Lister interface { // List gets all the versions for the given baseURL & module. // It returns ErrNotFound if the module isn't found - List(module string) ([]string, error) + List(ctx context.Context, module string) ([]string, error) } diff --git a/pkg/storage/minio/lister.go b/pkg/storage/minio/lister.go index 1f98fa16..f6d1b518 100644 --- a/pkg/storage/minio/lister.go +++ b/pkg/storage/minio/lister.go @@ -1,11 +1,12 @@ package minio import ( + "context" "sort" "strings" ) -func (l *storageImpl) List(module string) ([]string, error) { +func (l *storageImpl) List(ctx context.Context, module string) ([]string, error) { dict := make(map[string]struct{}) doneCh := make(chan struct{}) diff --git a/pkg/storage/mongo/lister.go b/pkg/storage/mongo/lister.go index 338ae6a4..7581cd04 100644 --- a/pkg/storage/mongo/lister.go +++ b/pkg/storage/mongo/lister.go @@ -1,6 +1,7 @@ package mongo import ( + "context" "strings" "github.com/globalsign/mgo/bson" @@ -8,7 +9,7 @@ import ( ) // List lists all versions of a module -func (s *ModuleStore) List(module string) ([]string, error) { +func (s *ModuleStore) List(ctx context.Context, module string) ([]string, error) { c := s.s.DB(s.d).C(s.c) result := make([]storage.Module, 0) err := c.Find(bson.M{"module": module}).All(&result) diff --git a/pkg/storage/rdbms/lister.go b/pkg/storage/rdbms/lister.go index 1e0fc39a..24ec176f 100644 --- a/pkg/storage/rdbms/lister.go +++ b/pkg/storage/rdbms/lister.go @@ -1,11 +1,13 @@ package rdbms import ( + "context" + "github.com/gomods/athens/pkg/storage/rdbms/models" ) // List lists all versions of a module -func (r *ModuleStore) List(module string) ([]string, error) { +func (r *ModuleStore) List(ctx context.Context, module string) ([]string, error) { result := make([]models.Module, 0) err := r.conn.Where("module = ?", module).All(&result) if err != nil { diff --git a/pkg/storage/storage_tests/module_storage/storage_test.go b/pkg/storage/storage_tests/module_storage/storage_test.go index 7a5a8da7..4f37337b 100644 --- a/pkg/storage/storage_tests/module_storage/storage_test.go +++ b/pkg/storage/storage_tests/module_storage/storage_test.go @@ -100,7 +100,7 @@ func (d *TestSuites) testList(ts storage.TestSuite) { } // append version from save-get roundtrip versions = append([]string{d.version}, versions...) - retVersions, err := ts.Storage().List(d.module) + retVersions, err := ts.Storage().List(context.Background(), d.module) r.NoError(err, hrn) r.Equal(versions, retVersions, hrn) } @@ -109,7 +109,7 @@ func (d *TestSuites) testGetSaveListRoundTrip(ts storage.TestSuite) { r := d.Require() hrn := ts.StorageHumanReadableName() ts.Storage().Save(context.Background(), d.module, d.version, d.mod, bytes.NewReader(d.zip), d.info) - listedVersions, err := ts.Storage().List(d.module) + listedVersions, err := ts.Storage().List(context.Background(), d.module) r.NoError(err, hrn) r.Equal(1, len(listedVersions), hrn) retVersion := listedVersions[0]