forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinder_swiper.go
193 lines (150 loc) · 4.07 KB
/
tinder_swiper.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
package bertyprotocol
import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/binary"
"io"
"time"
"berty.tech/berty/v2/go/internal/tinder"
"github.com/libp2p/go-libp2p-core/peer"
"go.uber.org/zap"
)
type swiper struct {
logger *zap.Logger
tinder tinder.Driver
interval time.Duration
}
func newSwiper(t tinder.Driver, logger *zap.Logger, interval time.Duration) *swiper {
return &swiper{
logger: logger,
tinder: t,
interval: interval,
}
}
func roundTimePeriod(date time.Time, interval time.Duration) time.Time {
if interval < 0 {
interval = -interval
}
intervalSeconds := int64(interval.Seconds())
periodsElapsed := date.Unix() / intervalSeconds
totalTime := periodsElapsed * intervalSeconds
return time.Unix(totalTime, 0).In(date.Location())
}
func nextTimePeriod(date time.Time, interval time.Duration) time.Time {
if interval < 0 {
interval = -interval
}
return roundTimePeriod(date, interval).Add(interval)
}
func generateRendezvousPointForPeriod(topic, seed []byte, date time.Time) []byte {
buf := make([]byte, 8)
mac := hmac.New(sha256.New, append(topic, seed...))
binary.BigEndian.PutUint64(buf, uint64(date.Unix()))
_, err := mac.Write(buf)
if err != nil {
panic(err)
}
sum := mac.Sum(nil)
return sum
}
// watchForPeriod looks for peers providing a resource for a given period
func (s *swiper) watchForPeriod(ctx context.Context, topic, seed []byte, t time.Time) (chan peer.AddrInfo, error) {
out := make(chan peer.AddrInfo)
roundedTime := roundTimePeriod(t, s.interval)
topicForTime := generateRendezvousPointForPeriod(topic, seed, roundedTime)
nextStart := nextTimePeriod(roundedTime, s.interval)
ctx, cancel := context.WithDeadline(ctx, nextStart)
ch, err := s.tinder.FindPeers(ctx, string(topicForTime))
if err != nil {
cancel()
return nil, err
}
go func() {
defer cancel()
for pid := range ch {
out <- pid
}
close(out)
}()
return out, nil
}
// watch looks for peers providing a resource
func (s *swiper) watch(ctx context.Context, topic, seed []byte) chan peer.AddrInfo {
out := make(chan peer.AddrInfo)
go func() {
for {
if ctx.Err() != nil {
break
}
periodEnd := nextTimePeriod(time.Now(), s.interval)
ctx, cancel := context.WithDeadline(ctx, periodEnd)
ch, err := s.watchForPeriod(ctx, topic, seed, time.Now())
if err != nil {
s.logger.Error("unable to watch for period", zap.Error(err))
<-ctx.Done()
} else {
for pid := range ch {
out <- pid
}
}
cancel()
}
close(out)
}()
return out
}
// announceForPeriod advertise a topic for a given period, will either stop when the context is done or when the period has ended
func (s *swiper) announceForPeriod(ctx context.Context, topic, seed []byte, t time.Time) (<-chan bool, <-chan error) {
announces := make(chan bool)
errs := make(chan error)
roundedTime := roundTimePeriod(t, s.interval)
topicForTime := generateRendezvousPointForPeriod(topic, seed, roundedTime)
nextStart := nextTimePeriod(roundedTime, s.interval)
go func() {
ctx, cancel := context.WithDeadline(ctx, nextStart)
defer cancel()
defer close(errs)
defer close(announces)
defer func() { errs <- io.EOF }()
for {
duration, err := s.tinder.Advertise(ctx, string(topicForTime))
if err != nil {
errs <- err
return
}
announces <- true
if ctx.Err() != nil || time.Now().Add(duration).UnixNano() > nextStart.UnixNano() {
return
}
}
}()
return announces, errs
}
// announce advertises availability on a topic indefinitely
func (s *swiper) announce(ctx context.Context, topic, seed []byte) (<-chan bool, <-chan error) {
announces := make(chan bool)
errs := make(chan error)
go func() {
defer close(announces)
defer close(errs)
defer func() { errs <- io.EOF }()
for {
select {
case <-ctx.Done():
return
default:
periodAnnounces, periodErrs := s.announceForPeriod(ctx, topic, seed, time.Now())
select {
case announce := <-periodAnnounces:
announces <- announce
break
case err := <-periodErrs:
errs <- err
break
}
}
}
}()
return announces, errs
}