From 924c065a87929acfc828d224da7fd259d9bba946 Mon Sep 17 00:00:00 2001 From: im-adithya Date: Fri, 20 Dec 2024 20:59:27 +0530 Subject: [PATCH] chore: add subCancelFnMap --- internal/nostr/nostr.go | 27 +++++++++++++++++++++++---- internal/nostr/push.go | 7 ++++++- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/internal/nostr/nostr.go b/internal/nostr/nostr.go index da521e1..b2b3aa0 100644 --- a/internal/nostr/nostr.go +++ b/internal/nostr/nostr.go @@ -50,6 +50,7 @@ type Service struct { subscriptionsMutex sync.Mutex relayMutex sync.Mutex client *expo.PushClient + subCancelFnMap map[string]context.CancelFunc } func NewService(ctx context.Context) (*Service, error) { @@ -138,7 +139,7 @@ func NewService(ctx context.Context) (*Service, error) { logger.WithError(err).Error("Failed to query open subscriptions") return nil, err } - + cancelFnMap := make(map[string]context.CancelFunc) for _, sub := range openSubscriptions { // Create a copy of the loop variable to // avoid passing address of the same variable @@ -147,8 +148,11 @@ func NewService(ctx context.Context) (*Service, error) { if sub.PushToken != "" { handleEvent = svc.handleSubscribedEventForPushNotification } - go svc.startSubscription(svc.Ctx, &subscription, nil, handleEvent) + subCtx, subCancelFn := context.WithCancel(svc.Ctx) + cancelFnMap[subscription.Uuid] = subCancelFn + go svc.startSubscription(subCtx, &subscription, nil, handleEvent) } + svc.subCancelFnMap = cancelFnMap return svc, nil } @@ -565,7 +569,11 @@ func (svc *Service) NIP47NotificationHandler(c echo.Context) error { }) } - go svc.startSubscription(svc.Ctx, &subscription, nil, svc.handleSubscribedEvent) + subCtx, subCancelFn := context.WithCancel(svc.Ctx) + svc.subscriptionsMutex.Lock() + svc.subCancelFnMap[subscription.Uuid] = subCancelFn + svc.subscriptionsMutex.Unlock() + go svc.startSubscription(subCtx, &subscription, nil, svc.handleSubscribedEvent) return c.JSON(http.StatusOK, SubscriptionResponse{ SubscriptionId: subscription.Uuid, @@ -624,7 +632,11 @@ func (svc *Service) SubscriptionHandler(c echo.Context) error { }) } - go svc.startSubscription(svc.Ctx, &subscription, nil, svc.handleSubscribedEvent) + subCtx, subCancelFn := context.WithCancel(svc.Ctx) + svc.subscriptionsMutex.Lock() + svc.subCancelFnMap[subscription.Uuid] = subCancelFn + svc.subscriptionsMutex.Unlock() + go svc.startSubscription(subCtx, &subscription, nil, svc.handleSubscribedEvent) return c.JSON(http.StatusOK, SubscriptionResponse{ SubscriptionId: subscription.Uuid, @@ -681,6 +693,13 @@ func (svc *Service) StopSubscriptionHandler(c echo.Context) error { } func (svc *Service) stopSubscription(subscription *Subscription) error { + svc.subscriptionsMutex.Lock() + cancelFn, exists := svc.subCancelFnMap[subscription.Uuid] + svc.subscriptionsMutex.Unlock() + if exists { + cancelFn() + } + if subscription.RelaySubscription != nil { subscription.RelaySubscription.Unsub() } diff --git a/internal/nostr/push.go b/internal/nostr/push.go index 1439f90..d473528 100644 --- a/internal/nostr/push.go +++ b/internal/nostr/push.go @@ -1,6 +1,7 @@ package nostr import ( + "context" "net/http" "time" @@ -107,7 +108,11 @@ func (svc *Service) NIP47PushNotificationHandler(c echo.Context) error { }) } - go svc.startSubscription(svc.Ctx, &subscription, nil, svc.handleSubscribedEventForPushNotification) + subCtx, subCancelFn := context.WithCancel(svc.Ctx) + svc.subscriptionsMutex.Lock() + svc.subCancelFnMap[subscription.Uuid] = subCancelFn + svc.subscriptionsMutex.Unlock() + go svc.startSubscription(subCtx, &subscription, nil, svc.handleSubscribedEventForPushNotification) return c.JSON(http.StatusOK, PushSubscriptionResponse{ SubscriptionId: subscription.Uuid,