2023-09-13 10:50:23 +00:00
|
|
|
package wakuv2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/wakuv2/common"
|
|
|
|
|
2023-10-12 19:21:49 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/exp/maps"
|
|
|
|
|
2024-06-14 12:41:45 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/api"
|
2024-06-28 13:54:48 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/onlinechecker"
|
2023-09-13 10:50:23 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/filter"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Methods on FilterManager maintain filter peer health
|
|
|
|
//
|
|
|
|
// runFilterLoop is the main event loop
|
|
|
|
//
|
|
|
|
// Filter Install/Uninstall events are pushed onto eventChan
|
|
|
|
// Subscribe, UnsubscribeWithSubscription, IsSubscriptionAlive calls
|
|
|
|
// are invoked from goroutines and request results pushed onto eventChan
|
|
|
|
//
|
|
|
|
// filterSubs is the map of filter IDs to subscriptions
|
|
|
|
|
|
|
|
type FilterManager struct {
|
2024-06-14 12:41:45 +00:00
|
|
|
sync.Mutex
|
|
|
|
ctx context.Context
|
|
|
|
cfg *Config
|
|
|
|
filters map[string]SubDetails // map of filters to apiSub details
|
|
|
|
onNewEnvelopes func(env *protocol.Envelope) error
|
|
|
|
logger *zap.Logger
|
|
|
|
node *filter.WakuFilterLightNode
|
2024-06-28 13:54:48 +00:00
|
|
|
onlineChecker *onlinechecker.DefaultOnlineChecker
|
2024-06-14 12:41:45 +00:00
|
|
|
filterQueue chan filterConfig
|
|
|
|
}
|
|
|
|
type SubDetails struct {
|
|
|
|
cancel func()
|
|
|
|
sub *api.Sub
|
|
|
|
}
|
|
|
|
|
|
|
|
const filterQueueSize = 1000
|
|
|
|
|
|
|
|
type filterConfig struct {
|
|
|
|
ID string
|
|
|
|
contentFilter protocol.ContentFilter
|
2023-09-13 10:50:23 +00:00
|
|
|
}
|
|
|
|
|
2024-06-14 12:41:45 +00:00
|
|
|
func newFilterManager(ctx context.Context, logger *zap.Logger, cfg *Config, onNewEnvelopes func(env *protocol.Envelope) error, node *filter.WakuFilterLightNode) *FilterManager {
|
2023-09-13 10:50:23 +00:00
|
|
|
// This fn is being mocked in test
|
|
|
|
mgr := new(FilterManager)
|
|
|
|
mgr.ctx = ctx
|
|
|
|
mgr.logger = logger
|
2024-06-14 12:41:45 +00:00
|
|
|
mgr.cfg = cfg
|
2023-09-13 10:50:23 +00:00
|
|
|
mgr.onNewEnvelopes = onNewEnvelopes
|
2024-06-14 12:41:45 +00:00
|
|
|
mgr.filters = make(map[string]SubDetails)
|
2023-09-13 10:50:23 +00:00
|
|
|
mgr.node = node
|
2024-06-14 12:41:45 +00:00
|
|
|
mgr.filterQueue = make(chan filterConfig, filterQueueSize)
|
2024-06-28 13:54:48 +00:00
|
|
|
mgr.onlineChecker = onlinechecker.NewDefaultOnlineChecker(false).(*onlinechecker.DefaultOnlineChecker)
|
|
|
|
|
|
|
|
mgr.node.SetOnlineChecker(mgr.onlineChecker)
|
2023-09-13 10:50:23 +00:00
|
|
|
|
|
|
|
return mgr
|
|
|
|
}
|
|
|
|
|
2024-06-14 12:41:45 +00:00
|
|
|
func (mgr *FilterManager) addFilter(filterID string, f *common.Filter) {
|
|
|
|
mgr.Lock()
|
|
|
|
defer mgr.Unlock()
|
2023-09-13 10:50:23 +00:00
|
|
|
contentFilter := mgr.buildContentFilter(f.PubsubTopic, f.ContentTopics)
|
2024-06-14 12:41:45 +00:00
|
|
|
mgr.logger.Debug("adding filter", zap.String("filter-id", filterID), zap.Stringer("content-filter", contentFilter))
|
2023-09-13 10:50:23 +00:00
|
|
|
|
2024-06-28 13:54:48 +00:00
|
|
|
if mgr.onlineChecker.IsOnline() {
|
2024-06-14 12:41:45 +00:00
|
|
|
go mgr.subscribeAndRunLoop(filterConfig{filterID, contentFilter})
|
2023-09-13 10:50:23 +00:00
|
|
|
} else {
|
2024-06-14 12:41:45 +00:00
|
|
|
mgr.logger.Debug("queuing filter as not online", zap.String("filter-id", filterID), zap.Stringer("content-filter", contentFilter))
|
|
|
|
mgr.filterQueue <- filterConfig{filterID, contentFilter}
|
2023-09-13 10:50:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-14 12:41:45 +00:00
|
|
|
func (mgr *FilterManager) subscribeAndRunLoop(f filterConfig) {
|
|
|
|
ctx, cancel := context.WithCancel(mgr.ctx)
|
|
|
|
config := api.FilterConfig{MaxPeers: mgr.cfg.MinPeersForFilter}
|
|
|
|
|
2024-06-28 13:54:48 +00:00
|
|
|
sub, err := api.Subscribe(ctx, mgr.node, f.contentFilter, config, mgr.logger)
|
2024-06-14 12:41:45 +00:00
|
|
|
mgr.Lock()
|
|
|
|
mgr.filters[f.ID] = SubDetails{cancel, sub}
|
|
|
|
mgr.Unlock()
|
|
|
|
if err == nil {
|
|
|
|
mgr.logger.Debug("subscription successful, running loop", zap.String("filter-id", f.ID), zap.Stringer("content-filter", f.contentFilter))
|
|
|
|
mgr.runFilterSubscriptionLoop(sub)
|
|
|
|
} else {
|
|
|
|
mgr.logger.Error("subscription fail, need to debug issue", zap.String("filter-id", f.ID), zap.Stringer("content-filter", f.contentFilter), zap.Error(err))
|
2023-09-13 10:50:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-14 12:41:45 +00:00
|
|
|
func (mgr *FilterManager) onConnectionStatusChange(pubsubTopic string, newStatus bool) {
|
|
|
|
mgr.logger.Debug("inside on connection status change", zap.Bool("new-status", newStatus),
|
|
|
|
zap.Int("filters count", len(mgr.filters)), zap.Int("filter-queue-len", len(mgr.filterQueue)))
|
|
|
|
//TODO: Needs optimization because only on transition from offline to online should trigger this logic.
|
|
|
|
if newStatus { //Online
|
|
|
|
if len(mgr.filterQueue) > 0 {
|
|
|
|
//Check if any filter subs are pending and subscribe them
|
|
|
|
for filter := range mgr.filterQueue {
|
|
|
|
mgr.logger.Debug("subscribing from filter queue", zap.String("filter-id", filter.ID), zap.Stringer("content-filter", filter.contentFilter))
|
|
|
|
go mgr.subscribeAndRunLoop(filter)
|
|
|
|
if len(mgr.filterQueue) == 0 {
|
|
|
|
mgr.logger.Debug("filter queue empty")
|
|
|
|
break
|
|
|
|
}
|
2023-10-10 11:31:05 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-14 12:41:45 +00:00
|
|
|
}
|
2024-06-28 13:54:48 +00:00
|
|
|
|
|
|
|
mgr.onlineChecker.SetOnline(newStatus)
|
2024-06-14 12:41:45 +00:00
|
|
|
}
|
2023-09-13 10:50:23 +00:00
|
|
|
|
2024-06-14 12:41:45 +00:00
|
|
|
func (mgr *FilterManager) removeFilter(filterID string) {
|
|
|
|
mgr.Lock()
|
|
|
|
defer mgr.Unlock()
|
|
|
|
mgr.logger.Debug("removing filter", zap.String("filter-id", filterID))
|
|
|
|
|
|
|
|
subDetails, ok := mgr.filters[filterID]
|
|
|
|
if ok {
|
|
|
|
delete(mgr.filters, filterID)
|
|
|
|
// close goroutine running runFilterSubscriptionLoop
|
|
|
|
// this will also close api.Sub
|
|
|
|
subDetails.cancel()
|
|
|
|
} else {
|
|
|
|
mgr.logger.Debug("filter removal: filter not found", zap.String("filter-id", filterID))
|
2023-09-13 10:50:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mgr *FilterManager) buildContentFilter(pubsubTopic string, contentTopicSet common.TopicSet) protocol.ContentFilter {
|
|
|
|
contentTopics := make([]string, len(contentTopicSet))
|
|
|
|
for i, ct := range maps.Keys(contentTopicSet) {
|
|
|
|
contentTopics[i] = ct.ContentTopic()
|
|
|
|
}
|
|
|
|
|
2023-11-10 00:29:15 +00:00
|
|
|
return protocol.NewContentFilter(pubsubTopic, contentTopics...)
|
2023-09-13 10:50:23 +00:00
|
|
|
}
|
|
|
|
|
2024-06-14 12:41:45 +00:00
|
|
|
func (mgr *FilterManager) runFilterSubscriptionLoop(sub *api.Sub) {
|
2023-09-13 10:50:23 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-mgr.ctx.Done():
|
2024-06-14 12:41:45 +00:00
|
|
|
mgr.logger.Debug("subscription loop ended", zap.Stringer("content-filter", sub.ContentFilter))
|
2023-09-13 10:50:23 +00:00
|
|
|
return
|
2024-06-14 12:41:45 +00:00
|
|
|
case env, ok := <-sub.DataCh:
|
2023-09-13 10:50:23 +00:00
|
|
|
if ok {
|
|
|
|
err := (mgr.onNewEnvelopes)(env)
|
|
|
|
if err != nil {
|
2024-06-14 12:41:45 +00:00
|
|
|
mgr.logger.Error("invoking onNewEnvelopes error", zap.Error(err))
|
2023-09-13 10:50:23 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-06-14 12:41:45 +00:00
|
|
|
mgr.logger.Debug("filter sub is closed", zap.Any("content-filter", sub.ContentFilter))
|
2023-09-13 10:50:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|