-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving locks to util and some refactoring
Signed-off-by: Annaraya Narasagond <annarayanarasagond@gmail.com> Signed-off-by: Annaraya Narasagond <annarayanarasagond@gmail.com> Annaraya Narasagond <annarayanarasagond@gmail.com>
- Loading branch information
Showing
3 changed files
with
92 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// SPDX-FileCopyrightText: The RamenDR authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package util | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// NamespaceLock implements atomic operation for namespace. It will have the namespace | ||
// having multiple vrgs in which VRGs are being processed. | ||
type NamespaceLock struct { | ||
namespace string | ||
mux sync.Mutex | ||
} | ||
|
||
// NewNamespaceLock returns new NamespaceLock | ||
func NewNamespaceLock() *NamespaceLock { | ||
return &NamespaceLock{} | ||
} | ||
|
||
// TryToAcquireLock tries to acquire the lock for processing VRG in a namespace having | ||
// multiple VRGs and returns true if successful. | ||
// If processing has already begun in the namespace, returns false. | ||
func (nl *NamespaceLock) TryToAcquireLock(namespace string) bool { | ||
nl.mux.Lock() | ||
defer nl.mux.Unlock() | ||
|
||
if nl.namespace == namespace { | ||
return false | ||
} | ||
|
||
if nl.namespace == "" { | ||
nl.namespace = namespace | ||
} | ||
|
||
return true | ||
} | ||
|
||
// Release removes lock on the namespace | ||
func (nl *NamespaceLock) Release(namespace string) { | ||
nl.mux.Lock() | ||
defer nl.mux.Unlock() | ||
nl.namespace = "" | ||
} |
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,18 @@ | ||
// SPDX-FileCopyrightText: The RamenDR authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package util_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/ramendr/ramen/internal/controller/util" | ||
) | ||
|
||
var _ = Describe("Testing Locks", func() { | ||
nsLock := util.NewNamespaceLock() | ||
Expect(nsLock.TryToAcquireLock("test")).To(BeTrue()) | ||
Expect(nsLock.TryToAcquireLock("test")).To(BeFalse()) | ||
nsLock.Release("test") | ||
Expect(nsLock.TryToAcquireLock("test")).To(BeTrue()) | ||
}) |
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