diff --git a/pkg/probod/notifications_config.go b/pkg/probod/notifications_config.go index 14d790ba2..ac7e373db 100644 --- a/pkg/probod/notifications_config.go +++ b/pkg/probod/notifications_config.go @@ -22,4 +22,5 @@ type notificationsConfig struct { type webhookConfig struct { SenderInterval int `json:"sender-interval"` + CacheTTL int `json:"cache-ttl"` } diff --git a/pkg/probod/probod.go b/pkg/probod/probod.go index f9819f506..13a5a5b2b 100644 --- a/pkg/probod/probod.go +++ b/pkg/probod/probod.go @@ -157,6 +157,7 @@ func New() *Implm { }, Webhook: webhookConfig{ SenderInterval: 5, + CacheTTL: 86400, }, }, CustomDomains: customDomainsConfig{ @@ -486,6 +487,7 @@ func (impl *Implm) Run( webhookSenderCtx, stopWebhookSender := context.WithCancel(context.Background()) webhookSender := webhook.NewSender(pgClient, l.Named("webhook-sender"), webhook.Config{ Interval: time.Duration(impl.cfg.Notifications.Webhook.SenderInterval) * time.Second, + CacheTTL: time.Duration(impl.cfg.Notifications.Webhook.CacheTTL) * time.Second, EncryptionKey: impl.cfg.EncryptionKey, }) wg.Go( diff --git a/pkg/webhook/sender.go b/pkg/webhook/sender.go index 908ff455b..0344c1084 100644 --- a/pkg/webhook/sender.go +++ b/pkg/webhook/sender.go @@ -39,13 +39,15 @@ import ( type ( Sender struct { - pg *pg.Client - logger *log.Logger - httpClient *http.Client - encryptionKey cipher.EncryptionKey - cache sync.Map - interval time.Duration - timeout time.Duration + pg *pg.Client + logger *log.Logger + httpClient *http.Client + encryptionKey cipher.EncryptionKey + cache sync.Map + cacheCreatedAt time.Time + cacheTTL time.Duration + interval time.Duration + timeout time.Duration } cachedSecret struct { @@ -56,6 +58,7 @@ type ( Config struct { Interval time.Duration Timeout time.Duration + CacheTTL time.Duration EncryptionKey cipher.EncryptionKey } ) @@ -71,13 +74,19 @@ func NewSender(pg *pg.Client, logger *log.Logger, cfg Config) *Sender { cfg.Timeout = 30 * time.Second } + if cfg.CacheTTL <= 0 { + cfg.CacheTTL = 24 * time.Hour + } + return &Sender{ - pg: pg, - logger: logger, - httpClient: httpclient.DefaultPooledClient(httpclient.WithLogger(logger)), - encryptionKey: cfg.EncryptionKey, - interval: cfg.Interval, - timeout: cfg.Timeout, + pg: pg, + logger: logger, + httpClient: httpclient.DefaultPooledClient(httpclient.WithLogger(logger)), + encryptionKey: cfg.EncryptionKey, + cacheCreatedAt: time.Now(), + cacheTTL: cfg.CacheTTL, + interval: cfg.Interval, + timeout: cfg.Timeout, } } @@ -95,6 +104,11 @@ func (s *Sender) Run(ctx context.Context) error { } func (s *Sender) processEvents(ctx context.Context) error { + if time.Since(s.cacheCreatedAt) >= s.cacheTTL { + s.cache = sync.Map{} + s.cacheCreatedAt = time.Now() + } + for { webhookData, err := s.claimNextWebhookData(ctx) if err != nil {