cache: Add config options for duration and cleanup interval

This commit is contained in:
Thorben Günther 2023-02-08 14:46:14 +01:00
parent d20d76d2b3
commit 0508f37896
No known key found for this signature in database
GPG key ID: 415CD778D8C5AFED
5 changed files with 78 additions and 11 deletions

View file

@ -60,6 +60,15 @@ ntfy {
user user user user
password pass password pass
} }
# When the alert-mode is set to single, ntfy-alertmanager will cache each single alert
# to avoid sending recurrences.
cache {
# How long messages stay in the cache for
duration 24h
# Interval in which the cache is cleaned up
cleanup-interval 1h
}
``` ```
### Alertmanager config ### Alertmanager config

View file

@ -13,6 +13,7 @@ type cachedAlert struct {
type cache struct { type cache struct {
mu sync.Mutex mu sync.Mutex
duration time.Duration
alerts map[fingerprint]*cachedAlert alerts map[fingerprint]*cachedAlert
} }
@ -20,8 +21,9 @@ func (a *cachedAlert) expired() bool {
return a.expires.Before(time.Now()) return a.expires.Before(time.Now())
} }
func newCache() *cache { func newCache(d time.Duration) *cache {
c := new(cache) c := new(cache)
c.duration = d
c.alerts = make(map[fingerprint]*cachedAlert) c.alerts = make(map[fingerprint]*cachedAlert)
return c return c
@ -37,11 +39,11 @@ func (c *cache) cleanup() {
} }
} }
func (c *cache) set(f fingerprint, d time.Duration) { func (c *cache) set(f fingerprint) {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
alert := new(cachedAlert) alert := new(cachedAlert)
alert.expires = time.Now().Add(d) alert.expires = time.Now().Add(c.duration)
c.alerts[f] = alert c.alerts[f] = alert
} }

View file

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"strings" "strings"
"time"
"git.sr.ht/~emersion/go-scfg" "git.sr.ht/~emersion/go-scfg"
) )
@ -22,6 +23,7 @@ type config struct {
Password string Password string
ntfy ntfyConfig ntfy ntfyConfig
labels labels labels labels
cache cacheConfig
} }
type ntfyConfig struct { type ntfyConfig struct {
@ -40,6 +42,11 @@ type labelConfig struct {
Tags []string Tags []string
} }
type cacheConfig struct {
CleanupInterval time.Duration
Duration time.Duration
}
func readConfig(path string) (*config, error) { func readConfig(path string) (*config, error) {
cfg, err := scfg.Load(path) cfg, err := scfg.Load(path)
if err != nil { if err != nil {
@ -52,6 +59,9 @@ func readConfig(path string) (*config, error) {
config.LogLevel = "info" config.LogLevel = "info"
config.alertMode = single config.alertMode = single
config.cache.CleanupInterval = time.Hour
config.cache.Duration = time.Hour * 24
d := cfg.Get("log-level") d := cfg.Get("log-level")
if d != nil { if d != nil {
if err := d.ParseParams(&config.LogLevel); err != nil { if err := d.ParseParams(&config.LogLevel); err != nil {
@ -172,5 +182,39 @@ func readConfig(path string) (*config, error) {
} }
} }
cacheDir := cfg.Get("cache")
if cacheDir != nil {
var durationString string
d = cacheDir.Children.Get("duration")
if d != nil {
if err := d.ParseParams(&durationString); err != nil {
return nil, err
}
duration, err := time.ParseDuration(durationString)
if err != nil {
return nil, err
}
config.cache.Duration = duration
}
var cleanupIntervalString string
d = cacheDir.Children.Get("cleanup-interval")
if d != nil {
if err := d.ParseParams(&cleanupIntervalString); err != nil {
return nil, err
}
interval, err := time.ParseDuration(cleanupIntervalString)
if err != nil {
return nil, err
}
config.cache.CleanupInterval = interval
}
}
return config, nil return config, nil
} }

View file

@ -5,6 +5,7 @@ import (
"path/filepath" "path/filepath"
"reflect" "reflect"
"testing" "testing"
"time"
) )
func TestReadConfig(t *testing.T) { func TestReadConfig(t *testing.T) {
@ -44,6 +45,15 @@ ntfy {
user user user user
password pass password pass
} }
# When the alert-mode is set to single, ntfy-alertmanager will cache each single alert
# to avoid sending recurrences.
cache {
# How long messages stay in the cache for
duration 48h
# Interval in which the cache is cleaned up
# cleanup-interval 1h
}
` `
expectedCfg := &config{ expectedCfg := &config{
@ -53,7 +63,11 @@ ntfy {
Label: map[string]labelConfig{ Label: map[string]labelConfig{
"severity:critical": {Priority: "5", Tags: []string{"rotating_light"}}, "severity:critical": {Priority: "5", Tags: []string{"rotating_light"}},
"severity:info": {Priority: "1"}, "severity:info": {Priority: "1"},
"instance:example.com": {Tags: []string{"computer", "example"}}}}} "instance:example.com": {Tags: []string{"computer", "example"}},
},
},
cache: cacheConfig{CleanupInterval: time.Hour, Duration: 48 * time.Hour},
}
configPath := filepath.Join(t.TempDir(), "config") configPath := filepath.Join(t.TempDir(), "config")
err := os.WriteFile(configPath, []byte(configContent), 0600) err := os.WriteFile(configPath, []byte(configContent), 0600)

View file

@ -58,8 +58,7 @@ func (rcv *receiver) singleAlertNotifications(p *payload) []*notification {
rcv.logger.Debugf("Alert %s skipped: Still in cache", alert.Fingerprint) rcv.logger.Debugf("Alert %s skipped: Still in cache", alert.Fingerprint)
continue continue
} }
// TODO: Make configurable rcv.cache.set(alert.Fingerprint)
rcv.cache.set(alert.Fingerprint, time.Hour*24)
n := new(notification) n := new(notification)
@ -286,8 +285,7 @@ func (rcv *receiver) basicAuthMiddleware(handler http.HandlerFunc) http.HandlerF
func (rcv *receiver) runCleanup() { func (rcv *receiver) runCleanup() {
for { for {
// TODO: Make configurable time.Sleep(rcv.cfg.cache.CleanupInterval)
time.Sleep(time.Hour)
rcv.logger.Info("Pruning cache") rcv.logger.Info("Pruning cache")
rcv.cache.cleanup() rcv.cache.cleanup()
} }
@ -316,7 +314,7 @@ func main() {
logger.Errorf("config: %v", err) logger.Errorf("config: %v", err)
} }
receiver := &receiver{cfg: cfg, logger: logger, cache: newCache()} receiver := &receiver{cfg: cfg, logger: logger, cache: newCache(cfg.cache.Duration)}
logger.Infof("Listening on %s, ntfy-alertmanager %s", cfg.HTTPAddress, version) logger.Infof("Listening on %s, ntfy-alertmanager %s", cfg.HTTPAddress, version)