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 {
SenderInterval int `json:"sender-interval"`
CacheTTL int `json:"cache-ttl"`
}

View File

@@ -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(

View File

@@ -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 {