Skip to content

Commit

Permalink
fix some struct names
Browse files Browse the repository at this point in the history
  • Loading branch information
FireDefend committed Aug 22, 2024
1 parent 71bc7a8 commit 8f6affb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
3 changes: 0 additions & 3 deletions coa/pkg/apis/v1alpha2/providers/keylock/keylock.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ import (
"time"

providers "github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2/providers"
//"encoding/json"
)

type UnLock func()

type IKeyLockProvider interface {
Init(config providers.IProviderConfig) error
Lock(string)
Expand Down
30 changes: 15 additions & 15 deletions coa/pkg/apis/v1alpha2/providers/keylock/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ import (

var sLog = logger.NewLogger("coa.runtime")

type MemoryStringLockProvider struct {
type MemoryKeyLockProvider struct {
lm *LockMap
cleanInterval int //seconds
purgeDuration int // 12 hours before
}

type MemoryStringLockProviderConfig struct {
type MemoryKeyLockProviderConfig struct {
CleanInterval int `json:"cleanInterval"`
PurgeDuration int `json:"purgeDuration"`
}

func toMemoryStringLockProviderConfig(config providers.IProviderConfig) (MemoryStringLockProviderConfig, error) {
ret := MemoryStringLockProviderConfig{}
func toMemoryKeyLockProviderConfig(config providers.IProviderConfig) (MemoryKeyLockProviderConfig, error) {
ret := MemoryKeyLockProviderConfig{}
data, err := json.Marshal(config)
if err != nil {
return ret, err
Expand All @@ -39,19 +39,19 @@ func toMemoryStringLockProviderConfig(config providers.IProviderConfig) (MemoryS
return ret, err
}

func (mslp *MemoryStringLockProvider) Init(config providers.IProviderConfig) error {
stringLockConfig, err := toMemoryStringLockProviderConfig(config)
func (mslp *MemoryKeyLockProvider) Init(config providers.IProviderConfig) error {
KeyLockConfig, err := toMemoryKeyLockProviderConfig(config)
if err != nil {
sLog.Errorf(" P (String Lock): failed to parse provider config %+v", err)
return errors.New("expected MemoryStringLockProviderConfig")
return errors.New("expected MemoryKeyLockProviderConfig")
}
if stringLockConfig.CleanInterval > 0 {
mslp.cleanInterval = stringLockConfig.CleanInterval
if KeyLockConfig.CleanInterval > 0 {
mslp.cleanInterval = KeyLockConfig.CleanInterval
} else {
mslp.cleanInterval = 30 // default: 30 seconds
}
if stringLockConfig.PurgeDuration > 0 {
mslp.purgeDuration = stringLockConfig.PurgeDuration
if KeyLockConfig.PurgeDuration > 0 {
mslp.purgeDuration = KeyLockConfig.PurgeDuration
} else {
mslp.purgeDuration = 60 * 60 * 12 // default: 12 hours
}
Expand All @@ -65,20 +65,20 @@ func (mslp *MemoryStringLockProvider) Init(config providers.IProviderConfig) err
return nil
}

func (mslp *MemoryStringLockProvider) Lock(key string) {
func (mslp *MemoryKeyLockProvider) Lock(key string) {
mslp.lm.getLockNode(key).Lock()
}

func (mslp *MemoryStringLockProvider) UnLock(key string) {
func (mslp *MemoryKeyLockProvider) UnLock(key string) {
mslp.lm.getLockNode(key).Unlock()
go mslp.lm.updateLockLRU(key)
}

func (mslp *MemoryStringLockProvider) TryLock(key string) bool {
func (mslp *MemoryKeyLockProvider) TryLock(key string) bool {
return mslp.lm.getLockNode(key).TryLock()
}

func (mslp *MemoryStringLockProvider) TryLockWithTimeout(key string, duration time.Duration) bool {
func (mslp *MemoryKeyLockProvider) TryLockWithTimeout(key string, duration time.Duration) bool {
start := time.Now()
for start.Add(duration).After(time.Now()) {
if mslp.TryLock(key) {
Expand Down
16 changes: 8 additions & 8 deletions coa/pkg/apis/v1alpha2/providers/keylock/memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ func (m MockProviderConfig) MarshalJSON() ([]byte, error) {
})
}

func TestToMemoryStringLockProviderConfig(t *testing.T) {
func TestToMemoryKeyLockProviderConfig(t *testing.T) {
mockConfig := MockProviderConfig{
CleanInterval: 45,
PurgeDuration: 3600,
}
expectedConfig := MemoryStringLockProviderConfig{
expectedConfig := MemoryKeyLockProviderConfig{
CleanInterval: 45,
PurgeDuration: 3600,
}
Expand All @@ -44,7 +44,7 @@ func TestToMemoryStringLockProviderConfig(t *testing.T) {
t.Fatalf("Failed to marshal mock config: %v", err)
}

var providerConfig MemoryStringLockProviderConfig
var providerConfig MemoryKeyLockProviderConfig
err = json.Unmarshal(data, &providerConfig)
if err != nil {
t.Fatalf("Failed to unmarshal provider config: %v", err)
Expand All @@ -60,7 +60,7 @@ func TestInit(t *testing.T) {
CleanInterval: 45,
PurgeDuration: 3600,
}
provider := MemoryStringLockProvider{}
provider := MemoryKeyLockProvider{}
err := provider.Init(mockConfig)
if err != nil {
t.Fatalf("Initialization failed: %v", err)
Expand All @@ -79,7 +79,7 @@ func TestInit(t *testing.T) {

func TestInitDefaultValues(t *testing.T) {
mockConfig := MockProviderConfig{}
provider := MemoryStringLockProvider{}
provider := MemoryKeyLockProvider{}
err := provider.Init(mockConfig)
if err != nil {
t.Fatalf("Initialization failed: %v", err)
Expand All @@ -95,7 +95,7 @@ func TestInitDefaultValues(t *testing.T) {
}

func TestLockAndUnlock(t *testing.T) {
provider := MemoryStringLockProvider{}
provider := MemoryKeyLockProvider{}
provider.lm = NewLockMap()

provider.Lock("testKey")
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestLockManagerClean(t *testing.T) {
}

func TestConcurrentLockUnlock(t *testing.T) {
provider := MemoryStringLockProvider{}
provider := MemoryKeyLockProvider{}
provider.lm = NewLockMap()

var wg sync.WaitGroup
Expand Down Expand Up @@ -190,7 +190,7 @@ func TestConcurrentLockUnlock(t *testing.T) {
}

func TestConcurrentAccessDifferentKeys(t *testing.T) {
provider := MemoryStringLockProvider{}
provider := MemoryKeyLockProvider{}
provider.lm = NewLockMap()

var wg sync.WaitGroup
Expand Down

0 comments on commit 8f6affb

Please sign in to comment.