/
orbitdb_topic.go
235 lines (184 loc) · 4.54 KB
/
orbitdb_topic.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package bertyprotocol
import (
"context"
"fmt"
"sync"
"time"
"github.com/libp2p/go-libp2p-core/peer"
"berty.tech/berty/v2/go/internal/rendezvous"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/go-orbit-db/events"
"berty.tech/go-orbit-db/iface"
)
const DefaultRotationInterval = time.Hour * 24
type PubSubWithRotation struct {
impl iface.PubSubInterface
keys map[string][]byte
mu sync.RWMutex
interval time.Duration
}
func (p *PubSubWithRotation) TopicSubscribe(ctx context.Context, topic string) (iface.PubSubTopic, error) {
p.mu.Lock()
defer p.mu.Unlock()
key, ok := p.keys[topic]
if !ok {
return nil, errcode.ErrInvalidInput.Wrap(fmt.Errorf("key for topic not found"))
}
return p.NewPubSubTopic(ctx, p.interval, topic, key)
}
func (p *PubSubWithRotation) RegisterGroupLinkKey(dbAddress string, linkKey []byte) {
p.mu.Lock()
defer p.mu.Unlock()
p.keys[dbAddress] = linkKey
}
type PubSubTopicWithRotate struct {
mu sync.Mutex
impl iface.PubSubTopic
subCtx context.Context
topic string
}
func (p *PubSubTopicWithRotate) Publish(ctx context.Context, message []byte) error {
p.mu.Lock()
impl := p.impl
p.mu.Unlock()
return impl.Publish(ctx, message)
}
func (p *PubSubTopicWithRotate) Peers(ctx context.Context) ([]peer.ID, error) {
p.mu.Lock()
impl := p.impl
p.mu.Unlock()
return impl.Peers(ctx)
}
func (p *PubSubTopicWithRotate) WatchPeers(ctx context.Context) (<-chan events.Event, error) {
ch := make(chan events.Event)
subCh, err := p.watchPeers(ctx)
if err != nil {
close(ch)
return nil, err
}
go func() {
defer close(ch)
for {
for e := range subCh {
ch <- e
}
if ctx.Err() != nil {
return
}
subCh, err = p.watchPeers(ctx)
if err != nil {
return
}
}
}()
return ch, nil
}
func (p *PubSubTopicWithRotate) watchPeers(ctx context.Context) (<-chan events.Event, error) {
p.mu.Lock()
impl := p.impl
subCtx := p.subCtx
p.mu.Unlock()
if err := subCtx.Err(); err != nil {
return nil, err
}
subCh, err := impl.WatchPeers(ctx)
if err != nil {
return nil, err
}
return subCh, nil
}
func (p *PubSubTopicWithRotate) WatchMessages(ctx context.Context) (<-chan *iface.EventPubSubMessage, error) {
ch := make(chan *iface.EventPubSubMessage)
subCh, err := p.watchMessages(ctx)
if err != nil {
close(ch)
return nil, err
}
go func() {
defer close(ch)
for {
for e := range subCh {
ch <- e
}
if ctx.Err() != nil {
return
}
subCh, err = p.watchMessages(ctx)
if err != nil {
return
}
}
}()
return ch, nil
}
func (p *PubSubTopicWithRotate) watchMessages(ctx context.Context) (<-chan *iface.EventPubSubMessage, error) {
p.mu.Lock()
impl := p.impl
subCtx := p.subCtx
p.mu.Unlock()
if err := subCtx.Err(); err != nil {
return nil, err
}
subCh, err := impl.WatchMessages(ctx)
if err != nil {
return nil, err
}
return subCh, nil
}
func (p *PubSubTopicWithRotate) Topic() string {
return p.topic
}
func (p *PubSubWithRotation) NewPubSubTopic(ctx context.Context, interval time.Duration, topic string, key []byte) (*PubSubTopicWithRotate, error) {
psTopicImpl, subCtx, cancel, err := p.getNextTopic(ctx, interval, topic, key)
if err != nil {
return nil, err
}
psTopic := &PubSubTopicWithRotate{
topic: topic,
subCtx: ctx,
impl: psTopicImpl,
}
go func() {
for {
<-subCtx.Done()
cancel()
psTopic.mu.Lock()
psTopicImpl, subCtx, cancel, err = p.getNextTopic(ctx, interval, topic, key)
if err != nil {
return
}
psTopic.impl = psTopicImpl
psTopic.subCtx = subCtx
psTopic.mu.Unlock()
}
}()
return psTopic, nil
}
func (p *PubSubWithRotation) getNextTopic(ctx context.Context, interval time.Duration, topic string, key []byte) (iface.PubSubTopic, context.Context, context.CancelFunc, error) {
if err := ctx.Err(); err != nil {
return nil, nil, nil, err
}
topicBytes := []byte(topic)
now := time.Now()
start := rendezvous.RoundTimePeriod(now, interval)
next := rendezvous.NextTimePeriod(now, interval)
rendezvous.GenerateRendezvousPointForPeriod(topicBytes, key, start)
subCtx, cancel := context.WithDeadline(ctx, next)
t, err := p.impl.TopicSubscribe(subCtx, topic)
if err != nil {
cancel()
return nil, nil, nil, err
}
return t, subCtx, cancel, nil
}
func NewPubSubWithRotation(impl iface.PubSubInterface, interval time.Duration) *PubSubWithRotation {
return &PubSubWithRotation{
impl: impl,
interval: interval,
keys: map[string][]byte{},
}
}
var (
_ iface.PubSubInterface = (*PubSubWithRotation)(nil)
_ iface.PubSubTopic = (*PubSubTopicWithRotate)(nil)
)