Add cache ttl

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2026-02-13 13:59:36 +01:00
parent 007f58d76f
commit c163ca9bf0
3 changed files with 30 additions and 13 deletions

View File

@@ -22,4 +22,5 @@ type notificationsConfig struct {
type webhookConfig struct { type webhookConfig struct {
SenderInterval int `json:"sender-interval"` SenderInterval int `json:"sender-interval"`
CacheTTL int `json:"cache-ttl"`
} }

View File

@@ -157,6 +157,7 @@ func New() *Implm {
}, },
Webhook: webhookConfig{ Webhook: webhookConfig{
SenderInterval: 5, SenderInterval: 5,
CacheTTL: 86400,
}, },
}, },
CustomDomains: customDomainsConfig{ CustomDomains: customDomainsConfig{
@@ -486,6 +487,7 @@ func (impl *Implm) Run(
webhookSenderCtx, stopWebhookSender := context.WithCancel(context.Background()) webhookSenderCtx, stopWebhookSender := context.WithCancel(context.Background())
webhookSender := webhook.NewSender(pgClient, l.Named("webhook-sender"), webhook.Config{ webhookSender := webhook.NewSender(pgClient, l.Named("webhook-sender"), webhook.Config{
Interval: time.Duration(impl.cfg.Notifications.Webhook.SenderInterval) * time.Second, Interval: time.Duration(impl.cfg.Notifications.Webhook.SenderInterval) * time.Second,
CacheTTL: time.Duration(impl.cfg.Notifications.Webhook.CacheTTL) * time.Second,
EncryptionKey: impl.cfg.EncryptionKey, EncryptionKey: impl.cfg.EncryptionKey,
}) })
wg.Go( wg.Go(

View File

@@ -39,13 +39,15 @@ import (
type ( type (
Sender struct { Sender struct {
pg *pg.Client pg *pg.Client
logger *log.Logger logger *log.Logger
httpClient *http.Client httpClient *http.Client
encryptionKey cipher.EncryptionKey encryptionKey cipher.EncryptionKey
cache sync.Map cache sync.Map
interval time.Duration cacheCreatedAt time.Time
timeout time.Duration cacheTTL time.Duration
interval time.Duration
timeout time.Duration
} }
cachedSecret struct { cachedSecret struct {
@@ -56,6 +58,7 @@ type (
Config struct { Config struct {
Interval time.Duration Interval time.Duration
Timeout time.Duration Timeout time.Duration
CacheTTL time.Duration
EncryptionKey cipher.EncryptionKey EncryptionKey cipher.EncryptionKey
} }
) )
@@ -71,13 +74,19 @@ func NewSender(pg *pg.Client, logger *log.Logger, cfg Config) *Sender {
cfg.Timeout = 30 * time.Second cfg.Timeout = 30 * time.Second
} }
if cfg.CacheTTL <= 0 {
cfg.CacheTTL = 24 * time.Hour
}
return &Sender{ return &Sender{
pg: pg, pg: pg,
logger: logger, logger: logger,
httpClient: httpclient.DefaultPooledClient(httpclient.WithLogger(logger)), httpClient: httpclient.DefaultPooledClient(httpclient.WithLogger(logger)),
encryptionKey: cfg.EncryptionKey, encryptionKey: cfg.EncryptionKey,
interval: cfg.Interval, cacheCreatedAt: time.Now(),
timeout: cfg.Timeout, 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 { func (s *Sender) processEvents(ctx context.Context) error {
if time.Since(s.cacheCreatedAt) >= s.cacheTTL {
s.cache = sync.Map{}
s.cacheCreatedAt = time.Now()
}
for { for {
webhookData, err := s.claimNextWebhookData(ctx) webhookData, err := s.claimNextWebhookData(ctx)
if err != nil { if err != nil {