From 5d0f3ffa0e83460364e19b03d5a77b4208f8285b Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Sun, 27 Sep 2015 12:28:03 +0200 Subject: [PATCH] Generalize iterator interface, locking fixes --- provider/mem.go | 7 +++++-- provider/provider.go | 31 ++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/provider/mem.go b/provider/mem.go index 5d9c0234..eedf91e5 100644 --- a/provider/mem.go +++ b/provider/mem.go @@ -85,6 +85,9 @@ func (a *MemAlerts) IterActive() AlertIterator { } func (a *MemAlerts) All() ([]*types.Alert, error) { + a.mtx.RLock() + defer a.mtx.RUnlock() + var alerts []*types.Alert for _, a := range a.alerts { alerts = append(alerts, a) @@ -93,8 +96,8 @@ func (a *MemAlerts) All() ([]*types.Alert, error) { } func (a *MemAlerts) Put(alerts ...*types.Alert) error { - a.mtx.RLock() - defer a.mtx.RUnlock() + a.mtx.Lock() + defer a.mtx.Unlock() for _, alert := range alerts { a.alerts[alert.Fingerprint()] = alert diff --git a/provider/provider.go b/provider/provider.go index 69b80e24..ca1cd610 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -20,12 +20,16 @@ import ( "github.com/prometheus/alertmanager/types" ) -type AlertIterator interface { - Next() <-chan *types.Alert +type Iterator interface { Err() error Close() } +type AlertIterator interface { + Iterator + Next() <-chan *types.Alert +} + // Alerts gives access to a set of alerts. type Alerts interface { // IterActive returns an iterator over active alerts from the @@ -48,7 +52,7 @@ type Silences interface { types.Silencer // All returns all existing silences. - All() []*types.Silence + All() ([]*types.Silence, error) // Set a new silence. Set(*types.Silence) error // Del removes a silence. @@ -57,6 +61,27 @@ type Silences interface { Get(model.Fingerprint) (*types.Silence, error) } +type Notify struct { + Target string + Alerts []model.Fingerprint + Pending bool +} + +type NotifyIterator interface { + Iterator + Next() <-chan *Notify +} + +// Notifies provides information about pending and successful +// notifications. +type Notifies interface { + // IterPending returns an iterator over all notifies that have not + // yet been sent successfully. + IterPending() NotifyIterator + // + Set(*Notify) error +} + // Reloadable is a component that can change its state based // on a new configuration. type Reloadable interface {