forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TT-6968 GroupLogin synchronization force (TykTechnologies#4605)
[changelog] fixed: force synchronization for the edge group when `slave_options.synchroniser_enabled` is set to true.
- Loading branch information
1 parent
05b50f7
commit 178511b
Showing
5 changed files
with
171 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package rpc | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/TykTechnologies/tyk/apidef" | ||
"github.com/TykTechnologies/tyk/storage" | ||
) | ||
|
||
type SyncronizerForcer struct { | ||
store *storage.RedisCluster | ||
} | ||
|
||
//NewSyncForcer returns a new syncforcer with a connected redis with a key prefix synchronizer-group- for group synchronization control. | ||
func NewSyncForcer(redisController *storage.RedisController) *SyncronizerForcer { | ||
sf := &SyncronizerForcer{} | ||
|
||
sf.store = &storage.RedisCluster{KeyPrefix: "synchronizer-group-", RedisController: redisController} | ||
sf.store.Connect() | ||
|
||
return sf | ||
} | ||
|
||
// GroupLoginCallback checks if the groupID key exists in the storage to turn on/off ForceSync param. | ||
// If the the key doesn't exists in the storage, it creates it and set ForceSync to true | ||
func (sf *SyncronizerForcer) GroupLoginCallback(userKey string, groupID string) interface{} { | ||
shouldForce := false | ||
|
||
_, err := sf.store.GetKey(groupID) | ||
if err != nil && errors.Is(err, storage.ErrKeyNotFound) { | ||
shouldForce = true | ||
|
||
err = sf.store.SetKey(groupID, "", 0) | ||
if err != nil { | ||
Log.Error("error setting syncforcer key", err) | ||
} | ||
Log.Info("Forcing MDCB synchronization for group:", groupID) | ||
} | ||
|
||
return apidef.GroupLoginRequest{ | ||
UserKey: userKey, | ||
GroupID: groupID, | ||
ForceSync: shouldForce, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package rpc | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/TykTechnologies/tyk/apidef" | ||
"github.com/TykTechnologies/tyk/config" | ||
"github.com/TykTechnologies/tyk/storage" | ||
) | ||
|
||
var rc *storage.RedisController | ||
|
||
func init() { | ||
conf := config.Default | ||
|
||
rc = storage.NewRedisController(context.Background()) | ||
go rc.ConnectToRedis(context.Background(), nil, &conf) | ||
for { | ||
if rc.Connected() { | ||
break | ||
} | ||
|
||
time.Sleep(10 * time.Millisecond) | ||
} | ||
} | ||
|
||
func TestNewSyncForcer(t *testing.T) { | ||
sf := NewSyncForcer(rc) | ||
|
||
assert.True(t, sf.store.ControllerInitiated()) | ||
assert.Equal(t, "synchronizer-group-", sf.store.KeyPrefix) | ||
|
||
assert.Equal(t, true, sf.store.RedisController.Connected()) | ||
} | ||
|
||
func TestGroupLoginCallback(t *testing.T) { | ||
sf := NewSyncForcer(rc) | ||
defer sf.store.DeleteAllKeys() | ||
|
||
key := "key" | ||
groupID := "group" | ||
|
||
//first time, it should force since the group key doesn't exists | ||
groupLogin, ok := sf.GroupLoginCallback(key, groupID).(apidef.GroupLoginRequest) | ||
assert.True(t, ok) | ||
assert.Equal(t, true, groupLogin.ForceSync) | ||
assert.Equal(t, key, groupLogin.UserKey) | ||
assert.Equal(t, groupID, groupLogin.GroupID) | ||
|
||
//second time, it shouldn't force since the group key already exists | ||
groupLogin, ok = sf.GroupLoginCallback(key, groupID).(apidef.GroupLoginRequest) | ||
assert.True(t, ok) | ||
assert.Equal(t, false, groupLogin.ForceSync) | ||
assert.Equal(t, key, groupLogin.UserKey) | ||
assert.Equal(t, groupID, groupLogin.GroupID) | ||
} |