Skip to content

Commit

Permalink
Add mutex locks for dict read/write operations
Browse files Browse the repository at this point in the history
  • Loading branch information
vkuznet committed Aug 26, 2024
1 parent 36f7fac commit ce1b942
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions cric.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ import (
"os"
"sort"
"strings"
"sync"
)

// CricRecords defines type for CRIC records
type CricRecords map[string]CricEntry

// mutex keeps lock for cricRecords updates
var mutex sync.RWMutex

// CricEntry represents structure in CRIC entry (used by CMS headers)
type CricEntry struct {
DN string `json:"DN"` // CRIC DN
Expand Down Expand Up @@ -114,7 +118,10 @@ func getCricRecordsByKey(entries []CricEntry, key string, verbose bool) (map[str
return cricRecords, errors.New(msg)
}
recDNs := rec.DNs
if r, ok := cricRecords[k]; ok {
mutex.RLock()
r, ok := cricRecords[k]
mutex.RUnlock()
if ok {
recDNs = r.DNs
recDNs = append(recDNs, rec.DN)
rec.DNs = recDNs
Expand All @@ -125,7 +132,9 @@ func getCricRecordsByKey(entries []CricEntry, key string, verbose bool) (map[str
recDNs = append(recDNs, rec.DN)
rec.DNs = recDNs
}
mutex.Lock()
cricRecords[k] = rec
mutex.Unlock()
}
return cricRecords, nil
}
Expand Down Expand Up @@ -162,7 +171,10 @@ func getCricRecords(entries []CricEntry, verbose bool) (map[string]CricEntry, er
recDNs := rec.DNs
// the cricRecords map will contain sorted DN
sortedDN := GetSortedDN(rec.DN)
if r, ok := cricRecords[sortedDN]; ok {
mutex.RLock()
r, ok := cricRecords[sortedDN]
mutex.RUnlock()
if ok {
recDNs = r.DNs
recDNs = append(recDNs, rec.DN)
rec.DNs = recDNs
Expand All @@ -174,7 +186,9 @@ func getCricRecords(entries []CricEntry, verbose bool) (map[string]CricEntry, er
rec.DNs = recDNs
}
rec.SortedDN = sortedDN
mutex.Lock()
cricRecords[sortedDN] = rec
mutex.Unlock()
}
return cricRecords, nil
}
Expand All @@ -201,7 +215,9 @@ func ParseCric(fname string, verbose bool) (map[string]CricEntry, error) {
log.Println(err)
return cricRecords, err
}
mutex.Lock()
cricRecords = cmap
mutex.Unlock()
}
return cricRecords, nil
}
Expand All @@ -228,7 +244,9 @@ func ParseCricByKey(fname, key string, verbose bool) (map[string]CricEntry, erro
log.Println(err)
return cricRecords, err
}
mutex.Lock()
cricRecords = cmap
mutex.Unlock()
}
return cricRecords, nil
}

0 comments on commit ce1b942

Please sign in to comment.