Skip to content

Commit

Permalink
Fix lock usage in the types package
Browse files Browse the repository at this point in the history
Locking is a bit tricky there since some state-modifying methods will call to other methods that also modify marker state, simple defer won't work.
  • Loading branch information
prymitive committed May 1, 2017
1 parent 5aeaf2c commit 2c14247
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ type memMarker struct {

// SetSilenced sets the AlertStatus to suppressed and stores the associated silence IDs.
func (m *memMarker) SetSilenced(alert model.Fingerprint, ids ...string) {
m.mtx.RLock()
defer m.mtx.RUnlock()
m.mtx.Lock()

s, found := m.m[alert]
if !found {
Expand All @@ -122,18 +121,20 @@ func (m *memMarker) SetSilenced(alert model.Fingerprint, ids ...string) {
// fingerprint, it is suppressed. Otherwise, set it to
// AlertStateUnprocessed.
if len(ids) == 0 && len(s.InhibitedBy) == 0 {
m.mtx.Unlock()
m.SetActive(alert)
return
}

s.Status = AlertStateSuppressed
s.SilencedBy = ids

m.mtx.Unlock()
}

// SetInhibited sets the AlertStatus to suppressed and stores the associated alert IDs.
func (m *memMarker) SetInhibited(alert model.Fingerprint, ids ...string) {
m.mtx.RLock()
defer m.mtx.RUnlock()
m.mtx.Lock()

s, found := m.m[alert]
if !found {
Expand All @@ -145,16 +146,19 @@ func (m *memMarker) SetInhibited(alert model.Fingerprint, ids ...string) {
// fingerprint, it is suppressed. Otherwise, set it to
// AlertStateUnprocessed.
if len(ids) == 0 && len(s.SilencedBy) == 0 {
m.mtx.Unlock()
m.SetActive(alert)
return
}

s.Status = AlertStateSuppressed
s.InhibitedBy = ids

m.mtx.Unlock()
}
func (m *memMarker) SetActive(alert model.Fingerprint) {
m.mtx.RLock()
defer m.mtx.RUnlock()
m.mtx.Lock()
defer m.mtx.Unlock()

s, found := m.m[alert]
if !found {
Expand All @@ -175,16 +179,15 @@ func (m *memMarker) Status(alert model.Fingerprint) AlertStatus {
s, found := m.m[alert]
if !found {
s = &AlertStatus{}
m.m[alert] = s
}

return *s
}

// Delete deletes the given Fingerprint from the internal cache.
func (m *memMarker) Delete(alert model.Fingerprint) {
m.mtx.RLock()
defer m.mtx.RUnlock()
m.mtx.Lock()
defer m.mtx.Unlock()

delete(m.m, alert)
}
Expand Down

0 comments on commit 2c14247

Please sign in to comment.