-
Notifications
You must be signed in to change notification settings - Fork 42
/
subscription_details.go
122 lines (103 loc) · 2.97 KB
/
subscription_details.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package subscription
import (
"encoding/json"
"sync"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/waku-org/go-waku/waku/v2/protocol"
)
// Map of SubscriptionDetails.ID to subscriptions
type SubscriptionSet map[string]*SubscriptionDetails
type PeerSubscription struct {
PeerID peer.ID
SubsPerPubsubTopic map[string]SubscriptionSet
}
type PeerContentFilter struct {
PeerID peer.ID `json:"peerID"`
PubsubTopic string `json:"pubsubTopics"`
ContentTopics []string `json:"contentTopics"`
}
type SubscriptionDetails struct {
sync.RWMutex
ID string `json:"subscriptionID"`
mapRef *SubscriptionsMap
Closed bool `json:"-"`
once sync.Once
PeerID peer.ID `json:"peerID"`
ContentFilter protocol.ContentFilter `json:"contentFilters"`
C chan *protocol.Envelope `json:"-"`
}
func (s *SubscriptionDetails) Add(contentTopics ...string) {
s.Lock()
defer s.Unlock()
for _, ct := range contentTopics {
if _, ok := s.ContentFilter.ContentTopics[ct]; !ok {
s.ContentFilter.ContentTopics[ct] = struct{}{}
// Increase the number of subscriptions for this (pubsubTopic, contentTopic) pair
s.mapRef.Lock()
s.mapRef.increaseSubFor(s.ContentFilter.PubsubTopic, ct)
s.mapRef.Unlock()
}
}
}
func (s *SubscriptionDetails) Remove(contentTopics ...string) {
s.Lock()
defer s.Unlock()
for _, ct := range contentTopics {
if _, ok := s.ContentFilter.ContentTopics[ct]; ok {
delete(s.ContentFilter.ContentTopics, ct)
// Decrease the number of subscriptions for this (pubsubTopic, contentTopic) pair
s.mapRef.Lock()
s.mapRef.decreaseSubFor(s.ContentFilter.PubsubTopic, ct)
s.mapRef.Unlock()
}
}
if len(s.ContentFilter.ContentTopics) == 0 {
// err doesn't matter
_ = s.mapRef.Delete(s)
}
}
// C1 if contentFilter is empty, it means that given subscription is part of contentFilter
// C2 if not empty, check matching pubsubsTopic and atleast 1 contentTopic
func (s *SubscriptionDetails) isPartOf(contentFilter protocol.ContentFilter) bool {
s.RLock()
defer s.RUnlock()
if contentFilter.PubsubTopic != "" && // C1
s.ContentFilter.PubsubTopic != contentFilter.PubsubTopic { // C2
return false
}
// C1
if len(contentFilter.ContentTopics) == 0 {
return true
}
// C2
for cTopic := range contentFilter.ContentTopics {
if _, ok := s.ContentFilter.ContentTopics[cTopic]; ok {
return true
}
}
return false
}
func (s *SubscriptionDetails) CloseC() {
s.once.Do(func() {
s.Lock()
defer s.Unlock()
s.Closed = true
close(s.C)
})
}
func (s *SubscriptionDetails) Close() error {
s.CloseC()
return s.mapRef.Delete(s)
}
func (s *SubscriptionDetails) MarshalJSON() ([]byte, error) {
result := struct {
PeerID peer.ID `json:"peerID"`
PubsubTopic string `json:"pubsubTopics"`
ContentTopics []string `json:"contentTopics"`
}{
PeerID: s.PeerID,
PubsubTopic: s.ContentFilter.PubsubTopic,
ContentTopics: s.ContentFilter.ContentTopics.ToList(),
}
return json.Marshal(result)
}