Skip to content

Commit

Permalink
Use RLock/Unlock on read path
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaochao Dong (@damnever) <[email protected]>
  • Loading branch information
damnever committed Mar 12, 2024
1 parent da64bf9 commit 5f20c84
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions provider/mem/mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const alertChannelLength = 200
type Alerts struct {
cancel context.CancelFunc

mtx sync.Mutex
mtx sync.RWMutex

alerts *store.Alerts
marker types.Marker
Expand Down Expand Up @@ -194,9 +194,9 @@ func (a *Alerts) GetPending() provider.AlertIterator {

go func() {
defer close(ch)
a.mtx.Lock()
a.mtx.RLock()
alerts := a.alerts.List()
a.mtx.Unlock()
a.mtx.RUnlock()

for _, a := range alerts {
select {
Expand All @@ -212,8 +212,8 @@ func (a *Alerts) GetPending() provider.AlertIterator {

// Get returns the alert for a given fingerprint.
func (a *Alerts) Get(fp model.Fingerprint) (*types.Alert, error) {
a.mtx.Lock()
defer a.mtx.Unlock()
a.mtx.RLock()
defer a.mtx.RUnlock()
return a.alerts.Get(fp)
}

Expand Down Expand Up @@ -265,8 +265,8 @@ func (a *Alerts) Put(alerts ...*types.Alert) error {

// count returns the number of non-resolved alerts we currently have stored filtered by the provided state.
func (a *Alerts) count(state types.AlertState) int {
a.mtx.Lock()
defer a.mtx.Unlock()
a.mtx.RLock()
defer a.mtx.RUnlock()

var count int
for _, alert := range a.alerts.List() {
Expand Down

0 comments on commit 5f20c84

Please sign in to comment.