diff --git a/coa/pkg/apis/v1alpha2/providers/keylock/keylock.go b/coa/pkg/apis/v1alpha2/providers/keylock/keylock.go index e2d5dc307..6a7615595 100644 --- a/coa/pkg/apis/v1alpha2/providers/keylock/keylock.go +++ b/coa/pkg/apis/v1alpha2/providers/keylock/keylock.go @@ -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) diff --git a/coa/pkg/apis/v1alpha2/providers/keylock/memory/memory.go b/coa/pkg/apis/v1alpha2/providers/keylock/memory/memory.go index 880d2081d..92f0925c7 100644 --- a/coa/pkg/apis/v1alpha2/providers/keylock/memory/memory.go +++ b/coa/pkg/apis/v1alpha2/providers/keylock/memory/memory.go @@ -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 @@ -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 } @@ -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) { diff --git a/coa/pkg/apis/v1alpha2/providers/keylock/memory/memory_test.go b/coa/pkg/apis/v1alpha2/providers/keylock/memory/memory_test.go index 7c9c491a8..63320d6a9 100644 --- a/coa/pkg/apis/v1alpha2/providers/keylock/memory/memory_test.go +++ b/coa/pkg/apis/v1alpha2/providers/keylock/memory/memory_test.go @@ -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, } @@ -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) @@ -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) @@ -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) @@ -95,7 +95,7 @@ func TestInitDefaultValues(t *testing.T) { } func TestLockAndUnlock(t *testing.T) { - provider := MemoryStringLockProvider{} + provider := MemoryKeyLockProvider{} provider.lm = NewLockMap() provider.Lock("testKey") @@ -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 @@ -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