Skip to content

Commit

Permalink
Fix 2.9 hybrid compatibility (TykTechnologies#2557)
Browse files Browse the repository at this point in the history
All broken because we moved gateway files to sub package
Move RCP structures to separate package, so they can be imported by MDCB

Fix TykTechnologies#2538

Depends on TykTechnologies/gorpc#3
  • Loading branch information
buger authored Sep 25, 2019
1 parent 17becf4 commit 6a673e5
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 61 deletions.
30 changes: 30 additions & 0 deletions apidef/rpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package apidef

type InboundData struct {
KeyName string
Value string
SessionState string
Timeout int64
Per int64
Expire int64
}

type DefRequest struct {
OrgId string
Tags []string
}

type GroupLoginRequest struct {
UserKey string
GroupID string
}

type GroupKeySpaceRequest struct {
OrgID string
GroupID string
}

type KeysValuesPair struct {
Keys []string
Values []string
}
78 changes: 28 additions & 50 deletions gateway/rpc_storage_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,25 @@ import (

"github.com/garyburd/redigo/redis"

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/storage"

"github.com/sirupsen/logrus"
)

type InboundData struct {
KeyName string
Value string
SessionState string
Timeout int64
Per int64
Expire int64
}

type DefRequest struct {
OrgId string
Tags []string
}

type KeysValuesPair struct {
Keys []string
Values []string
}

type GroupLoginRequest struct {
UserKey string
GroupID string
}

type GroupKeySpaceRequest struct {
OrgID string
GroupID string
}

var (
dispatcherFuncs = map[string]interface{}{
"Login": func(clientAddr, userKey string) bool {
return false
},
"LoginWithGroup": func(clientAddr string, groupData *GroupLoginRequest) bool {
"LoginWithGroup": func(clientAddr string, groupData *apidef.GroupLoginRequest) bool {
return false
},
"GetKey": func(keyName string) (string, error) {
return "", nil
},
"SetKey": func(ibd *InboundData) error {
"SetKey": func(ibd *apidef.InboundData) error {
return nil
},
"GetExp": func(keyName string) (int64, error) {
Expand All @@ -72,10 +44,10 @@ var (
"DeleteRawKey": func(keyName string) (bool, error) {
return true, nil
},
"GetKeysAndValues": func(searchString string) (*KeysValuesPair, error) {
"GetKeysAndValues": func(searchString string) (*apidef.KeysValuesPair, error) {
return nil, nil
},
"GetKeysAndValuesWithFilter": func(searchString string) (*KeysValuesPair, error) {
"GetKeysAndValuesWithFilter": func(searchString string) (*apidef.KeysValuesPair, error) {
return nil, nil
},
"DeleteKeys": func(keys []string) (bool, error) {
Expand All @@ -84,16 +56,16 @@ var (
"Decrement": func(keyName string) error {
return nil
},
"IncrememntWithExpire": func(ibd *InboundData) (int64, error) {
"IncrememntWithExpire": func(ibd *apidef.InboundData) (int64, error) {
return 0, nil
},
"AppendToSet": func(ibd *InboundData) error {
"AppendToSet": func(ibd *apidef.InboundData) error {
return nil
},
"SetRollingWindow": func(ibd *InboundData) (int, error) {
"SetRollingWindow": func(ibd *apidef.InboundData) (int, error) {
return 0, nil
},
"GetApiDefinitions": func(dr *DefRequest) (string, error) {
"GetApiDefinitions": func(dr *apidef.DefRequest) (string, error) {
return "", nil
},
"GetPolicies": func(orgId string) (string, error) {
Expand All @@ -108,7 +80,7 @@ var (
"GetKeySpaceUpdate": func(clientAddr, orgId string) ([]string, error) {
return nil, nil
},
"GetGroupKeySpaceUpdate": func(clientAddr string, groupData *GroupKeySpaceRequest) ([]string, error) {
"GetGroupKeySpaceUpdate": func(clientAddr string, groupData *apidef.GroupKeySpaceRequest) ([]string, error) {
return nil, nil
},
"Ping": func() bool {
Expand Down Expand Up @@ -146,7 +118,7 @@ func (r *RPCStorageHandler) Connect() bool {
r.SuppressRegister,
dispatcherFuncs,
func(userKey string, groupID string) interface{} {
return GroupLoginRequest{
return apidef.GroupLoginRequest{
UserKey: userKey,
GroupID: groupID,
}
Expand Down Expand Up @@ -231,7 +203,13 @@ func (r *RPCStorageHandler) GetRawKey(keyName string) (string, error) {
}

func (r *RPCStorageHandler) GetMultiKey(keyNames []string) ([]string, error) {
log.Warning("RPCStorageHandler.GetMultiKey - Not implemented")
for _, key := range keyNames {
if value, err := r.GetKey(key); err != nil {
return nil, err
} else {
return []string{value}, nil
}
}

return nil, nil
}
Expand Down Expand Up @@ -268,7 +246,7 @@ func (r *RPCStorageHandler) SetExp(keyName string, timeout int64) error {
// SetKey will create (or update) a key value in the store
func (r *RPCStorageHandler) SetKey(keyName, session string, timeout int64) error {
start := time.Now() // get current time
ibd := InboundData{
ibd := apidef.InboundData{
KeyName: r.fixKey(keyName),
SessionState: session,
Timeout: timeout,
Expand Down Expand Up @@ -331,7 +309,7 @@ func (r *RPCStorageHandler) Decrement(keyName string) {
// IncrementWithExpire will increment a key in redis
func (r *RPCStorageHandler) IncrememntWithExpire(keyName string, expire int64) int64 {

ibd := InboundData{
ibd := apidef.InboundData{
KeyName: keyName,
Expire: expire,
}
Expand Down Expand Up @@ -396,8 +374,8 @@ func (r *RPCStorageHandler) GetKeysAndValuesWithFilter(filter string) map[string

returnValues := make(map[string]string)

for i, v := range kvPair.(*KeysValuesPair).Keys {
returnValues[r.cleanKey(v)] = kvPair.(*KeysValuesPair).Values[i]
for i, v := range kvPair.(*apidef.KeysValuesPair).Keys {
returnValues[r.cleanKey(v)] = kvPair.(*apidef.KeysValuesPair).Values[i]
}

return returnValues
Expand All @@ -422,8 +400,8 @@ func (r *RPCStorageHandler) GetKeysAndValues() map[string]string {
}

returnValues := make(map[string]string)
for i, v := range kvPair.(*KeysValuesPair).Keys {
returnValues[r.cleanKey(v)] = kvPair.(*KeysValuesPair).Values[i]
for i, v := range kvPair.(*apidef.KeysValuesPair).Keys {
returnValues[r.cleanKey(v)] = kvPair.(*apidef.KeysValuesPair).Values[i]
}

return returnValues
Expand Down Expand Up @@ -536,7 +514,7 @@ func (r *RPCStorageHandler) GetAndDeleteSet(keyName string) []interface{} {
}

func (r *RPCStorageHandler) AppendToSet(keyName, value string) {
ibd := InboundData{
ibd := apidef.InboundData{
KeyName: keyName,
Value: value,
}
Expand Down Expand Up @@ -570,7 +548,7 @@ func (r *RPCStorageHandler) AppendToSetPipelined(key string, values []string) {
// SetScrollingWindow is used in the rate limiter to handle rate limits fairly.
func (r *RPCStorageHandler) SetRollingWindow(keyName string, per int64, val string, pipeline bool) (int, []interface{}) {
start := time.Now() // get current time
ibd := InboundData{
ibd := apidef.InboundData{
KeyName: keyName,
Per: per,
Expire: -1,
Expand Down Expand Up @@ -634,7 +612,7 @@ func (r RPCStorageHandler) IsAccessError(err error) bool {

// GetAPIDefinitions will pull API definitions from the RPC server
func (r *RPCStorageHandler) GetApiDefinitions(orgId string, tags []string) string {
dr := DefRequest{
dr := apidef.DefRequest{
OrgId: orgId,
Tags: tags,
}
Expand Down Expand Up @@ -783,7 +761,7 @@ func (r *RPCStorageHandler) CheckForKeyspaceChanges(orgId string) {
reqData["orgId"] = orgId
} else {
funcName = "GetGroupKeySpaceUpdate"
req = GroupKeySpaceRequest{
req = apidef.GroupKeySpaceRequest{
OrgID: orgId,
GroupID: groupID,
}
Expand Down
9 changes: 5 additions & 4 deletions gateway/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/TykTechnologies/tyk/cli"

"github.com/TykTechnologies/gorpc"
"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/rpc"
"github.com/TykTechnologies/tyk/test"
Expand Down Expand Up @@ -124,7 +125,7 @@ func TestSyncAPISpecsRPCFailure_CheckGlobals(t *testing.T) {
// Test RPC
callCount := 0
dispatcher := gorpc.NewDispatcher()
dispatcher.AddFunc("GetApiDefinitions", func(clientAddr string, dr *DefRequest) (string, error) {
dispatcher.AddFunc("GetApiDefinitions", func(clientAddr string, dr *apidef.DefRequest) (string, error) {
if callCount == 0 {
callCount += 1
return `[]`, nil
Expand Down Expand Up @@ -183,7 +184,7 @@ func TestSyncAPISpecsRPCFailure_CheckGlobals(t *testing.T) {
func TestSyncAPISpecsRPCFailure(t *testing.T) {
// Test RPC
dispatcher := gorpc.NewDispatcher()
dispatcher.AddFunc("GetApiDefinitions", func(clientAddr string, dr *DefRequest) (string, error) {
dispatcher.AddFunc("GetApiDefinitions", func(clientAddr string, dr *apidef.DefRequest) (string, error) {
return "malformed json", nil
})
dispatcher.AddFunc("Login", func(clientAddr, userKey string) bool {
Expand All @@ -202,7 +203,7 @@ func TestSyncAPISpecsRPCFailure(t *testing.T) {
func TestSyncAPISpecsRPCSuccess(t *testing.T) {
// Test RPC
dispatcher := gorpc.NewDispatcher()
dispatcher.AddFunc("GetApiDefinitions", func(clientAddr string, dr *DefRequest) (string, error) {
dispatcher.AddFunc("GetApiDefinitions", func(clientAddr string, dr *apidef.DefRequest) (string, error) {
return jsonMarshalString(BuildAPI(func(spec *APISpec) {
spec.UseKeylessAccess = false
})), nil
Expand Down Expand Up @@ -281,7 +282,7 @@ func TestSyncAPISpecsRPCSuccess(t *testing.T) {
rpc.ResetEmergencyMode()

dispatcher := gorpc.NewDispatcher()
dispatcher.AddFunc("GetApiDefinitions", func(clientAddr string, dr *DefRequest) (string, error) {
dispatcher.AddFunc("GetApiDefinitions", func(clientAddr string, dr *apidef.DefRequest) (string, error) {
return jsonMarshalString(BuildAPI(
func(spec *APISpec) { spec.UseKeylessAccess = false },
func(spec *APISpec) { spec.UseKeylessAccess = false },
Expand Down
4 changes: 2 additions & 2 deletions vendor/github.com/TykTechnologies/gorpc/dispatcher.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions vendor/github.com/TykTechnologies/gorpc/server.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@
"revisionTime": "2017-02-22T15:40:38Z"
},
{
"checksumSHA1": "pk+Fj0KfcNdwugEfI9657E7flgI=",
"checksumSHA1": "HdyEg0NlrJck03XxYhfAHX16ArA=",
"path": "github.com/TykTechnologies/gorpc",
"revision": "2fd6ca5242c4dbee5ab151010ee844f3fb5507a8",
"revisionTime": "2018-09-28T16:00:09Z"
"revision": "f38605581dbf0f37ee4f277a00da77cd8db16e5a",
"revisionTime": "2019-09-25T17:50:35Z"
},
{
"checksumSHA1": "FEq3KG6Kgarh8P5XIUx3bH13zDM=",
Expand Down

0 comments on commit 6a673e5

Please sign in to comment.