From 1ef7d8cf76883266fbddc9be3a312aab7e14606a Mon Sep 17 00:00:00 2001 From: Jesse de Wit Date: Thu, 16 Nov 2023 12:37:46 +0100 Subject: [PATCH] cleanup expired notification urls --- main.go | 7 ++++-- notifications/cleanup.go | 41 +++++++++++++++++++++++++++++++ notifications/store.go | 2 ++ postgresql/notifications_store.go | 13 ++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 notifications/cleanup.go diff --git a/main.go b/main.go index 7b83427..d0bfa7e 100644 --- a/main.go +++ b/main.go @@ -103,8 +103,11 @@ func main() { notificationService := notifications.NewNotificationService(notificationsStore) go notificationService.Start(ctx) openingService := common.NewOpeningService(openingStore, nodesService) - cleanupService := lsps2.NewCleanupService(lsps2Store) - go cleanupService.Start(ctx) + lsps2CleanupService := lsps2.NewCleanupService(lsps2Store) + go lsps2CleanupService.Start(ctx) + notificationCleanupService := notifications.NewCleanupService(notificationsStore) + go notificationCleanupService.Start(ctx) + var interceptors []interceptor.HtlcInterceptor for _, node := range nodes { var htlcInterceptor interceptor.HtlcInterceptor diff --git a/notifications/cleanup.go b/notifications/cleanup.go new file mode 100644 index 0000000..8666447 --- /dev/null +++ b/notifications/cleanup.go @@ -0,0 +1,41 @@ +package notifications + +import ( + "context" + "log" + "time" +) + +type CleanupService struct { + store Store +} + +// The interval to clean unused promises and buy registrations. +var CleanupInterval time.Duration = time.Hour + +// The expiry duration is the time until a non-refreshed webhook url expires. +// Currently set to 4 weeks. +var ExpiryDuration time.Duration = time.Hour * 24 * 28 + +func NewCleanupService(store Store) *CleanupService { + return &CleanupService{ + store: store, + } +} + +// Periodically cleans up expired webhook urls. +func (c *CleanupService) Start(ctx context.Context) { + for { + before := time.Now().Add(-ExpiryDuration) + err := c.store.RemoveExpired(ctx, before) + if err != nil { + log.Printf("Failed to remove expired webhook urls before %v: %v", before, err) + } + select { + case <-time.After(CleanupInterval): + continue + case <-ctx.Done(): + return + } + } +} diff --git a/notifications/store.go b/notifications/store.go index fb9a352..b4a7e94 100644 --- a/notifications/store.go +++ b/notifications/store.go @@ -2,9 +2,11 @@ package notifications import ( "context" + "time" ) type Store interface { Register(ctx context.Context, pubkey string, url string) error GetRegistrations(ctx context.Context, pubkey string) ([]string, error) + RemoveExpired(ctx context.Context, before time.Time) error } diff --git a/postgresql/notifications_store.go b/postgresql/notifications_store.go index 6b76577..1cd00b7 100644 --- a/postgresql/notifications_store.go +++ b/postgresql/notifications_store.go @@ -74,3 +74,16 @@ func (s *NotificationsStore) GetRegistrations( return result, nil } + +func (s *NotificationsStore) RemoveExpired( + ctx context.Context, + before time.Time, +) error { + _, err := s.pool.Exec( + ctx, + `DELETE FROM public.notification_subscriptions + WHERE refreshed_at < $1`, + before.UnixMicro()) + + return err +}