Skip to content

Commit

Permalink
add no handle collect option to alert topics
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielc committed Feb 17, 2017
1 parent e4bf8af commit f6721cf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
20 changes: 18 additions & 2 deletions alert/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,17 @@ func (s *Topics) EventState(topic, event string) (EventState, bool) {
return t.EventState(event)
}

// Collect collects an event and handles the event.
func (s *Topics) Collect(event Event) error {
return s.collect(event, true)
}

// CollectNoHandle collects an event and updates state but does no handling of the event
func (s *Topics) CollectNoHandle(event Event) error {
return s.collect(event, false)
}

func (s *Topics) collect(event Event, handle bool) error {
s.mu.RLock()
topic := s.topics[event.Topic]
s.mu.RUnlock()
Expand All @@ -102,7 +112,7 @@ func (s *Topics) Collect(event Event) error {
s.mu.Unlock()
}

return topic.handleEvent(event)
return topic.collect(event, handle)
}

func (s *Topics) DeleteTopic(topic string) {
Expand Down Expand Up @@ -338,14 +348,20 @@ func (t *Topic) close() {
vars.DeleteStatistic(t.statsKey)
}

func (t *Topic) handleEvent(event Event) error {
func (t *Topic) collect(event Event, handle bool) error {
prev, ok := t.updateEvent(event.State)
if ok {
event.previousState = prev
}

t.collected.Add(1)
if handle {
return t.handleEvent(event)
}
return nil
}

func (t *Topic) handleEvent(event Event) error {
t.mu.RLock()
defer t.mu.RUnlock()

Expand Down
15 changes: 14 additions & 1 deletion services/alert/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,14 @@ func (s *Service) EventState(topic, event string) (alert.EventState, bool) {
}

func (s *Service) Collect(event alert.Event) error {
return s.collect(event, true)
}

func (s *Service) CollectNoHandle(event alert.Event) error {
return s.collect(event, false)
}

func (s *Service) collect(event alert.Event, handle bool) error {
s.mu.RLock()
closed := s.closedTopics[event.Topic]
s.mu.RUnlock()
Expand All @@ -671,7 +679,12 @@ func (s *Service) Collect(event alert.Event) error {
}
}

err := s.topics.Collect(event)
var err error
if handle {
err = s.topics.Collect(event)
} else {
err = s.topics.CollectNoHandle(event)
}
if err != nil {
return err
}
Expand Down

0 comments on commit f6721cf

Please sign in to comment.