-
Notifications
You must be signed in to change notification settings - Fork 26
/
service_adaptater.go
182 lines (149 loc) · 4.4 KB
/
service_adaptater.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
package tinder
import (
"context"
"sync"
"time"
"github.com/libp2p/go-libp2p/core/discovery"
"github.com/libp2p/go-libp2p/core/peer"
"go.uber.org/zap"
"berty.tech/weshnet/pkg/logutil"
)
var _ discovery.Discovery = (*DiscoveryAdaptater)(nil)
type discoverySubscribtion struct {
sub *Subscription
timer *time.Timer
}
type DiscoveryAdaptater struct {
ctx context.Context
cancel context.CancelFunc
logger *zap.Logger
defaultOpts []Option
service *Service
// watchdogDiscover
watchdogDiscover map[string]*discoverySubscribtion
muDiscover sync.Mutex
// advertise
watchdogAdvertise map[string]*time.Timer
muAdvertiser sync.Mutex
resetInterval time.Duration
ttl time.Duration
closeOnce sync.Once
}
func NewDiscoveryAdaptater(logger *zap.Logger, service *Service, defaultOpts ...Option) *DiscoveryAdaptater {
ctx, cancel := context.WithCancel(context.Background())
return &DiscoveryAdaptater{
ctx: ctx,
cancel: cancel,
logger: logger.Named("disc"),
watchdogDiscover: make(map[string]*discoverySubscribtion),
watchdogAdvertise: make(map[string]*time.Timer),
service: service,
resetInterval: time.Minute * 10,
ttl: time.Minute * 5,
defaultOpts: defaultOpts,
}
}
func (a *DiscoveryAdaptater) FindPeers(ctx context.Context, topic string, opts ...discovery.Option) (<-chan peer.AddrInfo, error) {
a.muDiscover.Lock()
defer a.muDiscover.Unlock()
if st, ok := a.watchdogDiscover[topic]; ok {
// already running FindPeers, reset watchdog
if !st.timer.Stop() {
<-st.timer.C
}
st.timer.Reset(a.resetInterval)
// watch again for new peers until the method expire
return st.sub.Out(), nil
}
start := time.Now()
a.logger.Debug("watchdogs looking for peers", logutil.PrivateString("topic", topic))
// filter out local discovery
sub := a.service.Subscribe(topic, a.defaultOpts...)
// pull to fetch previous peers (FindPeers)
go func() {
if err := sub.Pull(); err != nil {
a.logger.Error("unable to pull topic", zap.String("topic", topic), zap.Error(err))
}
}()
// create a new watchdog
timer := time.AfterFunc(a.resetInterval, func() {
a.logger.Debug("findpeers expired",
logutil.PrivateString("topic", topic),
zap.Duration("duration", time.Since(start)))
// watchdog has expired, cancel lookup+topic_watcher
a.muDiscover.Lock()
sub.Close()
delete(a.watchdogDiscover, topic)
a.muDiscover.Unlock()
})
a.watchdogDiscover[topic] = &discoverySubscribtion{
timer: timer,
sub: sub,
}
// watch for new peers until method context expire
return sub.Out(), nil
}
func (a *DiscoveryAdaptater) Advertise(_ context.Context, topic string, opts ...discovery.Option) (time.Duration, error) {
ctx := a.ctx
a.muAdvertiser.Lock()
defer a.muAdvertiser.Unlock()
start := time.Now()
if t, ok := a.watchdogAdvertise[topic]; ok {
// if we already advertise on this topic, reset the watchdog
if !t.Stop() {
<-t.C
}
t.Reset(a.resetInterval)
} else {
wctx, cancel := context.WithCancel(ctx)
// start advertising on this topic
// filter out local discovery
if err := a.service.StartAdvertises(wctx, topic, a.defaultOpts...); err != nil {
a.logger.Error("advertise failed", logutil.PrivateString("topic", topic), zap.Error(err))
cancel()
return time.Minute, err
}
a.logger.Debug("advertise started", logutil.PrivateString("topic", topic))
// create a new watchdog
a.watchdogAdvertise[topic] = time.AfterFunc(a.resetInterval, func() {
// watchdog has expired, cancel advertise
a.muAdvertiser.Lock()
cancel()
a.logger.Debug("advertise expired",
logutil.PrivateString("topic", topic),
zap.Duration("duration", time.Since(start)),
)
delete(a.watchdogAdvertise, topic)
a.muAdvertiser.Unlock()
// unregister from this topic if possible
if err := a.service.Unregister(ctx, topic); err != nil {
a.logger.Debug("unregister failed",
logutil.PrivateString("topic", topic),
zap.Error(err),
)
}
})
}
return a.ttl, nil
}
func (a *DiscoveryAdaptater) Close() error {
a.closeOnce.Do(func() {
a.muDiscover.Lock()
a.cancel()
for _, st := range a.watchdogDiscover {
if !st.timer.Stop() {
<-st.timer.C
}
_ = st.sub.Close()
}
a.muDiscover.Unlock()
a.muAdvertiser.Lock()
for _, t := range a.watchdogAdvertise {
if !t.Stop() {
<-t.C
}
}
a.muAdvertiser.Unlock()
})
return nil
}