2019-05-07 07:05:38 +00:00
|
|
|
package subscriptions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2024-10-28 20:54:17 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2024-09-26 22:37:32 +00:00
|
|
|
gocommon "github.com/status-im/status-go/common"
|
2024-10-28 20:54:17 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2019-05-07 07:05:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Subscriptions struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
subs map[SubscriptionID]*Subscription
|
|
|
|
checkPeriod time.Duration
|
2024-10-28 20:54:17 +00:00
|
|
|
logger *zap.Logger
|
2019-05-07 07:05:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSubscriptions(period time.Duration) *Subscriptions {
|
|
|
|
return &Subscriptions{
|
|
|
|
subs: make(map[SubscriptionID]*Subscription),
|
|
|
|
checkPeriod: period,
|
2024-10-28 20:54:17 +00:00
|
|
|
logger: logutils.ZapLogger().Named("subscriptionsService"),
|
2019-05-07 07:05:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Subscriptions) Create(namespace string, filter filter) (SubscriptionID, error) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
newSub := NewSubscription(namespace, filter)
|
|
|
|
|
|
|
|
go func() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer gocommon.LogOnPanic()
|
2019-05-07 07:05:38 +00:00
|
|
|
err := newSub.Start(s.checkPeriod)
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
s.logger.Error("error while starting subscription", zap.Error(err))
|
2019-05-07 07:05:38 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
s.subs[newSub.id] = newSub
|
|
|
|
|
|
|
|
return newSub.id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Subscriptions) Remove(id SubscriptionID) error {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
found, err := s.stopSubscription(id, true)
|
|
|
|
if found {
|
|
|
|
delete(s.subs, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Subscriptions) removeAll() error {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
unsubscribeErrors := make(map[SubscriptionID]error)
|
|
|
|
|
|
|
|
for id := range s.subs {
|
|
|
|
_, err := s.stopSubscription(id, false)
|
|
|
|
if err != nil {
|
|
|
|
unsubscribeErrors[id] = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.subs = make(map[SubscriptionID]*Subscription)
|
|
|
|
|
|
|
|
if len(unsubscribeErrors) > 0 {
|
|
|
|
return fmt.Errorf("errors while cleaning up subscriptions: %+v", unsubscribeErrors)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Subscriptions) stopSubscription(id SubscriptionID, uninstall bool) (bool, error) {
|
|
|
|
sub, found := s.subs[id]
|
|
|
|
if !found {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return true, sub.Stop(uninstall)
|
|
|
|
}
|