Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pass context for Get and List #1324

Merged
merged 2 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions api/internal/core/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get and List is work in memory, how to use the context?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't use it currently, but it's is convenient to pass custom parameters or other data in custom development

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep parameter enough would be better, is here any concret cases which must need context? It will need context only when do some io operations(disk io or network io) as far as I know, store is responsible to query data in memory, so it looks like will never do IO operations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please @nic-chen confirm

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, there may be some business parameters that need to be passed. If we pass these custom parameters one by one, the handler code needs to be modified, and the code will be conflicts with upstream. If use ctx to deliver, user only need to pay attention to the business part.

Create(ctx context.Context, obj interface{}) (interface{}, error)
Update(ctx context.Context, obj interface{}, createIfNotExist bool) error
BatchDelete(ctx context.Context, keys []string) error
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions api/internal/core/store/store_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
5 changes: 2 additions & 3 deletions api/internal/core/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions api/internal/filter/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/gin-gonic/gin"

"github.com/apisix/manager-api/internal/log"
)

Expand Down
4 changes: 2 additions & 2 deletions api/internal/handler/consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions api/internal/handler/global_rule/global_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
})
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion api/internal/handler/label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 11 additions & 11 deletions api/internal/handler/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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},
Expand All @@ -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},
Expand Down Expand Up @@ -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},
Expand All @@ -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},
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions api/internal/handler/server_info/server_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions api/internal/handler/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions api/internal/handler/ssl/ssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions api/internal/handler/upstream/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down