cleanup expired notification urls

This commit is contained in:
Jesse de Wit
2023-11-16 12:37:46 +01:00
parent 6d307179f1
commit 1ef7d8cf76
4 changed files with 61 additions and 2 deletions

View File

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

41
notifications/cleanup.go Normal file
View File

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

View File

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

View File

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