go-waku/waku/v2/protocol/relay/subscription.go
Prem Chaitanya Prathi b5be83a02e
feat : autoshard relay api (#807)
* fix: using relay without bcaster should consume and drop messages

* update relay api usage

* move subscription to broadcaster

* move filter logic under subscription

* Support more than 1 relay subscription for a pubSubTopic

* modify relay Publish API to derive pubSubTopic based on autosharding

* implement relay RPC methods for autosharding

* remove relay msgChannel and relay on pubsub buffersize for subscription

Co-authored-by: richΛrd <info@richardramos.me>

* handle relay subscribe with noConsumer and address issue reported in code review

* chore: reorg relay code

---------

Co-authored-by: richΛrd <info@richardramos.me>
2023-10-21 01:26:18 +05:30

58 lines
1.6 KiB
Go

package relay
import (
"context"
"github.com/waku-org/go-waku/waku/v2/protocol"
"golang.org/x/exp/slices"
)
// Subscription handles the details of a particular Topic subscription. There may be many subscriptions for a given topic.
type Subscription struct {
ID int
Unsubscribe func() //for internal use only. For relay Subscription use relay protocol's unsubscribe
Ch chan *protocol.Envelope
contentFilter protocol.ContentFilter
subType SubscriptionType
noConsume bool
}
type SubscriptionType int
const (
SpecificContentTopics SubscriptionType = iota
AllContentTopics
)
// Submit allows a message to be submitted for a subscription
func (s *Subscription) Submit(ctx context.Context, msg *protocol.Envelope) {
//Filter and notify
// - if contentFilter doesn't have a contentTopic
// - if contentFilter has contentTopics and it matches with message
if !s.noConsume && (len(s.contentFilter.ContentTopicsList()) == 0 ||
(len(s.contentFilter.ContentTopicsList()) > 0 && slices.Contains[string](s.contentFilter.ContentTopicsList(), msg.Message().ContentTopic))) {
select {
case <-ctx.Done():
return
case s.Ch <- msg:
}
}
}
// NewSubscription creates a subscription that will only receive messages based on the contentFilter
func NewSubscription(contentFilter protocol.ContentFilter) *Subscription {
ch := make(chan *protocol.Envelope)
var subType SubscriptionType
if len(contentFilter.ContentTopicsList()) == 0 {
subType = AllContentTopics
}
return &Subscription{
Unsubscribe: func() {
close(ch)
},
Ch: ch,
contentFilter: contentFilter,
subType: subType,
}
}