This is an automated email from the ASF dual-hosted git repository.

juzhiyuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 9b0baeb  feat: pass context for `Get` and `List` (#1324)
9b0baeb is described below

commit 9b0baebc1b4157d15407dd00804c428790a62eb0
Author: nic-chen <[email protected]>
AuthorDate: Tue Jan 19 09:23:43 2021 +0800

    feat: pass context for `Get` and `List` (#1324)
---
 api/internal/core/store/store.go                |  8 ++++----
 api/internal/core/store/store_mock.go           |  4 ++--
 api/internal/core/store/store_test.go           |  5 ++---
 api/internal/filter/recover.go                  |  1 +
 api/internal/handler/consumer/consumer.go       |  4 ++--
 api/internal/handler/global_rule/global_rule.go |  6 +++---
 api/internal/handler/label/label.go             |  2 +-
 api/internal/handler/route/route.go             | 22 +++++++++++-----------
 api/internal/handler/server_info/server_info.go |  4 ++--
 api/internal/handler/service/service.go         | 10 +++++-----
 api/internal/handler/ssl/ssl.go                 |  8 ++++----
 api/internal/handler/upstream/upstream.go       | 10 +++++-----
 12 files changed, 42 insertions(+), 42 deletions(-)

diff --git a/api/internal/core/store/store.go b/api/internal/core/store/store.go
index 7af266c..e71e5df 100644
--- a/api/internal/core/store/store.go
+++ b/api/internal/core/store/store.go
@@ -35,8 +35,8 @@ import (
 )
 
 type Interface interface {
-       Get(key string) (interface{}, error)
-       List(input ListInput) (*ListOutput, error)
+       Get(ctx context.Context, key string) (interface{}, error)
+       List(ctx context.Context, input ListInput) (*ListOutput, error)
        Create(ctx context.Context, obj interface{}) (interface{}, error)
        Update(ctx context.Context, obj interface{}, createIfNotExist bool) 
error
        BatchDelete(ctx context.Context, keys []string) error
@@ -136,7 +136,7 @@ func (s *GenericStore) Init() error {
        return nil
 }
 
-func (s *GenericStore) Get(key string) (interface{}, error) {
+func (s *GenericStore) Get(_ context.Context, key string) (interface{}, error) 
{
        ret, ok := s.cache.Load(key)
        if !ok {
                log.Warnf("data not found by key: %s", key)
@@ -173,7 +173,7 @@ var defLessFunc = func(i, j interface{}) bool {
        return iID < jID
 }
 
-func (s *GenericStore) List(input ListInput) (*ListOutput, error) {
+func (s *GenericStore) List(_ context.Context, input ListInput) (*ListOutput, 
error) {
        var ret []interface{}
        s.cache.Range(func(key, value interface{}) bool {
                if input.Predicate != nil && !input.Predicate(value) {
diff --git a/api/internal/core/store/store_mock.go 
b/api/internal/core/store/store_mock.go
index a974ed6..8d29757 100644
--- a/api/internal/core/store/store_mock.go
+++ b/api/internal/core/store/store_mock.go
@@ -26,12 +26,12 @@ type MockInterface struct {
        mock.Mock
 }
 
-func (m *MockInterface) Get(key string) (interface{}, error) {
+func (m *MockInterface) Get(_ context.Context, key string) (interface{}, 
error) {
        ret := m.Mock.Called(key)
        return ret.Get(0), ret.Error(1)
 }
 
-func (m *MockInterface) List(input ListInput) (*ListOutput, error) {
+func (m *MockInterface) List(_ context.Context, input ListInput) (*ListOutput, 
error) {
        ret := m.Called(input)
 
        var (
diff --git a/api/internal/core/store/store_test.go 
b/api/internal/core/store/store_test.go
index f8ed342..990ddd6 100644
--- a/api/internal/core/store/store_test.go
+++ b/api/internal/core/store/store_test.go
@@ -286,8 +286,7 @@ func TestGenericStore_Get(t *testing.T) {
                for k, v := range tc.giveCache {
                        tc.giveStore.cache.Store(k, v)
                }
-
-               ret, err := tc.giveStore.Get(tc.giveId)
+               ret, err := tc.giveStore.Get(context.Background(), tc.giveId)
                assert.Equal(t, tc.wantRet, ret, tc.caseDesc)
                assert.Equal(t, tc.wantErr, err, tc.caseDesc)
        }
@@ -441,7 +440,7 @@ func TestGenericStore_List(t *testing.T) {
                        tc.giveStore.cache.Store(k, v)
                }
 
-               ret, err := tc.giveStore.List(tc.giveInput)
+               ret, err := tc.giveStore.List(context.Background(), 
tc.giveInput)
                assert.Equal(t, tc.wantRet.TotalSize, ret.TotalSize, 
tc.caseDesc)
                assert.ElementsMatch(t, tc.wantRet.Rows, ret.Rows, tc.caseDesc)
                assert.Equal(t, tc.wantErr, err, tc.caseDesc)
diff --git a/api/internal/filter/recover.go b/api/internal/filter/recover.go
index 9228e57..a8da66c 100644
--- a/api/internal/filter/recover.go
+++ b/api/internal/filter/recover.go
@@ -25,6 +25,7 @@ import (
        "time"
 
        "github.com/gin-gonic/gin"
+
        "github.com/apisix/manager-api/internal/log"
 )
 
diff --git a/api/internal/handler/consumer/consumer.go 
b/api/internal/handler/consumer/consumer.go
index b3ea5a1..2c0cde2 100644
--- a/api/internal/handler/consumer/consumer.go
+++ b/api/internal/handler/consumer/consumer.go
@@ -64,7 +64,7 @@ type GetInput struct {
 func (h *Handler) Get(c droplet.Context) (interface{}, error) {
        input := c.Input().(*GetInput)
 
-       r, err := h.consumerStore.Get(input.Username)
+       r, err := h.consumerStore.Get(c.Context(), input.Username)
        if err != nil {
                return handler.SpecCodeResponse(err), err
        }
@@ -113,7 +113,7 @@ type ListInput struct {
 func (h *Handler) List(c droplet.Context) (interface{}, error) {
        input := c.Input().(*ListInput)
 
-       ret, err := h.consumerStore.List(store.ListInput{
+       ret, err := h.consumerStore.List(c.Context(), store.ListInput{
                Predicate: func(obj interface{}) bool {
                        if input.Username != "" {
                                return 
strings.Contains(obj.(*entity.Consumer).Username, input.Username)
diff --git a/api/internal/handler/global_rule/global_rule.go 
b/api/internal/handler/global_rule/global_rule.go
index 2e3657b..8a5975b 100644
--- a/api/internal/handler/global_rule/global_rule.go
+++ b/api/internal/handler/global_rule/global_rule.go
@@ -69,7 +69,7 @@ type GetInput struct {
 func (h *Handler) Get(c droplet.Context) (interface{}, error) {
        input := c.Input().(*GetInput)
 
-       r, err := h.globalRuleStore.Get(input.ID)
+       r, err := h.globalRuleStore.Get(c.Context(), input.ID)
        if err != nil {
                return handler.SpecCodeResponse(err), err
        }
@@ -112,7 +112,7 @@ type ListInput struct {
 func (h *Handler) List(c droplet.Context) (interface{}, error) {
        input := c.Input().(*ListInput)
 
-       ret, err := h.globalRuleStore.List(store.ListInput{
+       ret, err := h.globalRuleStore.List(c.Context(), store.ListInput{
                PageSize:   input.PageSize,
                PageNumber: input.PageNumber,
        })
@@ -154,7 +154,7 @@ func Patch(c *gin.Context) (interface{}, error) {
        subPath := c.Param("path")
 
        routeStore := store.GetStore(store.HubKeyGlobalRule)
-       stored, err := routeStore.Get(ID)
+       stored, err := routeStore.Get(c, ID)
        if err != nil {
                return handler.SpecCodeResponse(err), err
        }
diff --git a/api/internal/handler/label/label.go 
b/api/internal/handler/label/label.go
index 9a29f8c..77574f6 100644
--- a/api/internal/handler/label/label.go
+++ b/api/internal/handler/label/label.go
@@ -194,7 +194,7 @@ func (h *Handler) List(c droplet.Context) (interface{}, 
error) {
        var totalRet = new(store.ListOutput)
        var existMap = make(map[string]struct{})
        for _, item := range items {
-               ret, err := item.(store.Interface).List(
+               ret, err := item.(store.Interface).List(c.Context(),
                        store.ListInput{
                                Predicate: predicate,
                                Format:    format,
diff --git a/api/internal/handler/route/route.go 
b/api/internal/handler/route/route.go
index 6f4fadb..c462c42 100644
--- a/api/internal/handler/route/route.go
+++ b/api/internal/handler/route/route.go
@@ -85,7 +85,7 @@ func Patch(c *gin.Context) (interface{}, error) {
        subPath := c.Param("path")
 
        routeStore := store.GetStore(store.HubKeyRoute)
-       stored, err := routeStore.Get(ID)
+       stored, err := routeStore.Get(c, ID)
        if err != nil {
                return handler.SpecCodeResponse(err), err
        }
@@ -159,14 +159,14 @@ type GetInput struct {
 func (h *Handler) Get(c droplet.Context) (interface{}, error) {
        input := c.Input().(*GetInput)
 
-       r, err := h.routeStore.Get(input.ID)
+       r, err := h.routeStore.Get(c.Context(), input.ID)
        if err != nil {
                return &data.SpecCodeResponse{StatusCode: http.StatusNotFound}, 
err
        }
 
        //format respond
        route := r.(*entity.Route)
-       script, _ := h.scriptStore.Get(input.ID)
+       script, _ := h.scriptStore.Get(c.Context(), input.ID)
        if script != nil {
                route.Script = script.(*entity.Script).Script
        }
@@ -208,7 +208,7 @@ func (h *Handler) List(c droplet.Context) (interface{}, 
error) {
                        fmt.Errorf("%s: \"%s\"", err.Error(), input.Label)
        }
 
-       ret, err := h.routeStore.List(store.ListInput{
+       ret, err := h.routeStore.List(c.Context(), store.ListInput{
                Predicate: func(obj interface{}) bool {
                        if input.Name != "" && 
!strings.Contains(obj.(*entity.Route).Name, input.Name) {
                                return false
@@ -248,7 +248,7 @@ func (h *Handler) List(c droplet.Context) (interface{}, 
error) {
        for i, item := range ret.Rows {
                route = item.(*entity.Route)
                id := utils.InterfaceToString(route.ID)
-               script, _ := h.scriptStore.Get(id)
+               script, _ := h.scriptStore.Get(c.Context(), id)
                if script != nil {
                        route.Script = script.(*entity.Script).Script
                }
@@ -300,7 +300,7 @@ func (h *Handler) Create(c droplet.Context) (interface{}, 
error) {
        //check depend
        if input.ServiceID != nil {
                serviceID := utils.InterfaceToString(input.ServiceID)
-               _, err := h.svcStore.Get(serviceID)
+               _, err := h.svcStore.Get(c.Context(), serviceID)
                if err != nil {
                        if err == data.ErrNotFound {
                                return &data.SpecCodeResponse{StatusCode: 
http.StatusBadRequest},
@@ -311,7 +311,7 @@ func (h *Handler) Create(c droplet.Context) (interface{}, 
error) {
        }
        if input.UpstreamID != nil {
                upstreamID := utils.InterfaceToString(input.UpstreamID)
-               _, err := h.upstreamStore.Get(upstreamID)
+               _, err := h.upstreamStore.Get(c.Context(), upstreamID)
                if err != nil {
                        if err == data.ErrNotFound {
                                return &data.SpecCodeResponse{StatusCode: 
http.StatusBadRequest},
@@ -382,7 +382,7 @@ func (h *Handler) Update(c droplet.Context) (interface{}, 
error) {
        //check depend
        if input.ServiceID != nil {
                serviceID := utils.InterfaceToString(input.ServiceID)
-               _, err := h.svcStore.Get(serviceID)
+               _, err := h.svcStore.Get(c.Context(), serviceID)
                if err != nil {
                        if err == data.ErrNotFound {
                                return &data.SpecCodeResponse{StatusCode: 
http.StatusBadRequest},
@@ -393,7 +393,7 @@ func (h *Handler) Update(c droplet.Context) (interface{}, 
error) {
        }
        if input.UpstreamID != nil {
                upstreamID := utils.InterfaceToString(input.UpstreamID)
-               _, err := h.upstreamStore.Get(upstreamID)
+               _, err := h.upstreamStore.Get(c.Context(), upstreamID)
                if err != nil {
                        if err == data.ErrNotFound {
                                return &data.SpecCodeResponse{StatusCode: 
http.StatusBadRequest},
@@ -440,7 +440,7 @@ func (h *Handler) Update(c droplet.Context) (interface{}, 
error) {
        } else {
                //remove exists script
                id := utils.InterfaceToString(input.Route.ID)
-               script, _ := h.scriptStore.Get(id)
+               script, _ := h.scriptStore.Get(c.Context(), id)
                if script != nil {
                        if err := h.scriptStore.BatchDelete(c.Context(), 
strings.Split(id, ",")); err != nil {
                                log.Warnf("delete script %s failed", 
input.Route.ID)
@@ -526,7 +526,7 @@ func Exist(c *gin.Context) (interface{}, error) {
        exclude := c.Query("exclude")
        routeStore := store.GetStore(store.HubKeyRoute)
 
-       ret, err := routeStore.List(store.ListInput{
+       ret, err := routeStore.List(c, store.ListInput{
                Predicate:  nil,
                PageSize:   0,
                PageNumber: 0,
diff --git a/api/internal/handler/server_info/server_info.go 
b/api/internal/handler/server_info/server_info.go
index 0b3f529..ee16a7b 100644
--- a/api/internal/handler/server_info/server_info.go
+++ b/api/internal/handler/server_info/server_info.go
@@ -54,7 +54,7 @@ type GetInput struct {
 func (h *Handler) Get(c droplet.Context) (interface{}, error) {
        input := c.Input().(*GetInput)
 
-       r, err := h.serverInfoStore.Get(input.ID)
+       r, err := h.serverInfoStore.Get(c.Context(), input.ID)
        if err != nil {
                return handler.SpecCodeResponse(err), err
        }
@@ -70,7 +70,7 @@ type ListInput struct {
 func (h *Handler) List(c droplet.Context) (interface{}, error) {
        input := c.Input().(*ListInput)
 
-       ret, err := h.serverInfoStore.List(store.ListInput{
+       ret, err := h.serverInfoStore.List(c.Context(), store.ListInput{
                Predicate: func(obj interface{}) bool {
                        if input.Hostname != "" {
                                return 
strings.Contains(obj.(*entity.ServerInfo).Hostname, input.Hostname)
diff --git a/api/internal/handler/service/service.go 
b/api/internal/handler/service/service.go
index dbd3714..4272739 100644
--- a/api/internal/handler/service/service.go
+++ b/api/internal/handler/service/service.go
@@ -71,7 +71,7 @@ type GetInput struct {
 func (h *Handler) Get(c droplet.Context) (interface{}, error) {
        input := c.Input().(*GetInput)
 
-       r, err := h.serviceStore.Get(input.ID)
+       r, err := h.serviceStore.Get(c.Context(), input.ID)
        if err != nil {
                return handler.SpecCodeResponse(err), err
        }
@@ -126,7 +126,7 @@ type ListInput struct {
 func (h *Handler) List(c droplet.Context) (interface{}, error) {
        input := c.Input().(*ListInput)
 
-       ret, err := h.serviceStore.List(store.ListInput{
+       ret, err := h.serviceStore.List(c.Context(), store.ListInput{
                Predicate: func(obj interface{}) bool {
                        if input.Name != "" {
                                return 
strings.Contains(obj.(*entity.Service).Name, input.Name)
@@ -155,7 +155,7 @@ func (h *Handler) Create(c droplet.Context) (interface{}, 
error) {
 
        if input.UpstreamID != nil {
                upstreamID := utils.InterfaceToString(input.UpstreamID)
-               _, err := h.upstreamStore.Get(upstreamID)
+               _, err := h.upstreamStore.Get(c.Context(), upstreamID)
                if err != nil {
                        if err == data.ErrNotFound {
                                return &data.SpecCodeResponse{StatusCode: 
http.StatusBadRequest},
@@ -192,7 +192,7 @@ func (h *Handler) Update(c droplet.Context) (interface{}, 
error) {
 
        if input.UpstreamID != nil {
                upstreamID := utils.InterfaceToString(input.UpstreamID)
-               _, err := h.upstreamStore.Get(upstreamID)
+               _, err := h.upstreamStore.Get(c.Context(), upstreamID)
                if err != nil {
                        if err == data.ErrNotFound {
                                return &data.SpecCodeResponse{StatusCode: 
http.StatusBadRequest},
@@ -232,7 +232,7 @@ func (h *Handler) Patch(c droplet.Context) (interface{}, 
error) {
                subPath = arr[1]
        }
 
-       stored, err := h.serviceStore.Get(input.ID)
+       stored, err := h.serviceStore.Get(c.Context(), input.ID)
        if err != nil {
                return handler.SpecCodeResponse(err), err
        }
diff --git a/api/internal/handler/ssl/ssl.go b/api/internal/handler/ssl/ssl.go
index 79d793e..da645d8 100644
--- a/api/internal/handler/ssl/ssl.go
+++ b/api/internal/handler/ssl/ssl.go
@@ -80,7 +80,7 @@ type GetInput struct {
 func (h *Handler) Get(c droplet.Context) (interface{}, error) {
        input := c.Input().(*GetInput)
 
-       ret, err := h.sslStore.Get(input.ID)
+       ret, err := h.sslStore.Get(c.Context(), input.ID)
        if err != nil {
                return handler.SpecCodeResponse(err), err
        }
@@ -139,7 +139,7 @@ type ListInput struct {
 func (h *Handler) List(c droplet.Context) (interface{}, error) {
        input := c.Input().(*ListInput)
 
-       ret, err := h.sslStore.List(store.ListInput{
+       ret, err := h.sslStore.List(c.Context(), store.ListInput{
                Predicate: func(obj interface{}) bool {
                        if input.SNI != "" {
                                if strings.Contains(obj.(*entity.SSL).Sni, 
input.SNI) {
@@ -239,7 +239,7 @@ func Patch(c *gin.Context) (interface{}, error) {
        subPath := c.Param("path")
 
        sslStore := store.GetStore(store.HubKeySsl)
-       stored, err := sslStore.Get(ID)
+       stored, err := sslStore.Get(c, ID)
        if err != nil {
                return handler.SpecCodeResponse(err), err
        }
@@ -458,7 +458,7 @@ func Exist(c *gin.Context) (interface{}, error) {
        }
 
        routeStore := store.GetStore(store.HubKeySsl)
-       ret, err := routeStore.List(store.ListInput{
+       ret, err := routeStore.List(c, store.ListInput{
                Predicate:  nil,
                PageSize:   0,
                PageNumber: 0,
diff --git a/api/internal/handler/upstream/upstream.go 
b/api/internal/handler/upstream/upstream.go
index 77b0af2..2e8cf87 100644
--- a/api/internal/handler/upstream/upstream.go
+++ b/api/internal/handler/upstream/upstream.go
@@ -71,7 +71,7 @@ type GetInput struct {
 func (h *Handler) Get(c droplet.Context) (interface{}, error) {
        input := c.Input().(*GetInput)
 
-       r, err := h.upstreamStore.Get(input.ID)
+       r, err := h.upstreamStore.Get(c.Context(), input.ID)
        if err != nil {
                return handler.SpecCodeResponse(err), err
        }
@@ -124,7 +124,7 @@ type ListInput struct {
 func (h *Handler) List(c droplet.Context) (interface{}, error) {
        input := c.Input().(*ListInput)
 
-       ret, err := h.upstreamStore.List(store.ListInput{
+       ret, err := h.upstreamStore.List(c.Context(), store.ListInput{
                Predicate: func(obj interface{}) bool {
                        if input.Name != "" {
                                return 
strings.Contains(obj.(*entity.Upstream).Name, input.Name)
@@ -204,7 +204,7 @@ func (h *Handler) Patch(c droplet.Context) (interface{}, 
error) {
                subPath = arr[1]
        }
 
-       stored, err := h.upstreamStore.Get(input.ID)
+       stored, err := h.upstreamStore.Get(c.Context(), input.ID)
        if err != nil {
                return handler.SpecCodeResponse(err), err
        }
@@ -254,7 +254,7 @@ func Exist(c *gin.Context) (interface{}, error) {
        exclude := c.Query("exclude")
        routeStore := store.GetStore(store.HubKeyUpstream)
 
-       ret, err := routeStore.List(store.ListInput{
+       ret, err := routeStore.List(c, store.ListInput{
                Predicate:  nil,
                PageSize:   0,
                PageNumber: 0,
@@ -283,7 +283,7 @@ func Exist(c *gin.Context) (interface{}, error) {
 func listUpstreamNames(c *gin.Context) (interface{}, error) {
        routeStore := store.GetStore(store.HubKeyUpstream)
 
-       ret, err := routeStore.List(store.ListInput{
+       ret, err := routeStore.List(c, store.ListInput{
                Predicate:  nil,
                PageSize:   0,
                PageNumber: 0,

Reply via email to