RICPLT-2910
[ric-plt/submgr.git] / pkg / control / tracker.go
index dfab96e..1682ae7 100644 (file)
@@ -24,13 +24,12 @@ import (
        "sync"
 )
 
-var trackerMutex = &sync.Mutex{}
-
 /*
 Implements a record of ongoing transactions and helper functions to CRUD the records.
 */
 type Tracker struct {
        transactionTable map[TransactionKey]Transaction
+       mutex            sync.Mutex
 }
 
 func (t *Tracker) Init() {
@@ -42,8 +41,8 @@ Checks if a tranascation with similar type has been ongoing. If not then creates
 Returns error if there is similar transatcion ongoing.
 */
 func (t *Tracker) TrackTransaction(key TransactionKey, xact Transaction) error {
-       trackerMutex.Lock()
-       defer trackerMutex.Unlock()
+       t.mutex.Lock()
+       defer t.mutex.Unlock()
        if _, ok := t.transactionTable[key]; ok {
                // TODO: Implement merge related check here. If the key is same but the value is different.
                err := fmt.Errorf("transaction tracker: Similar transaction with sub id %d and type %s is ongoing", key.SubID, key.transType)
@@ -59,8 +58,8 @@ Returns error in case the transaction cannot be found.
 */
 func (t *Tracker) UpdateTransaction(SubID uint16, transType Action, xact Transaction) error {
        key := TransactionKey{SubID, transType}
-       trackerMutex.Lock()
-       defer trackerMutex.Unlock()
+       t.mutex.Lock()
+       defer t.mutex.Unlock()
        if _, ok := t.transactionTable[key]; ok {
                // TODO: Implement merge related check here. If the key is same but the value is different.
                err := fmt.Errorf("transaction tracker: Similar transaction with sub id %d and type %v is ongoing", key.SubID, key.transType)
@@ -76,8 +75,8 @@ Returns error in case the transaction cannot be found.
 */
 func (t *Tracker) RetriveTransaction(subID uint16, act Action) (Transaction, error) {
        key := TransactionKey{subID, act}
-       trackerMutex.Lock()
-       defer trackerMutex.Unlock()
+       t.mutex.Lock()
+       defer t.mutex.Unlock()
        var xact Transaction
        if xact, ok := t.transactionTable[key]; ok {
                return xact, nil
@@ -93,8 +92,8 @@ Returns error in case the transaction cannot be found.
 func (t *Tracker) completeTransaction(subID uint16, act Action) (Transaction, error) {
        key := TransactionKey{subID, act}
        var emptyTransaction Transaction
-       trackerMutex.Lock()
-       defer trackerMutex.Unlock()
+       t.mutex.Lock()
+       defer t.mutex.Unlock()
        if xact, ok := t.transactionTable[key]; ok {
                delete(t.transactionTable, key)
                return xact, nil