forked from skycoin/skywire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmsg_tracker.go
371 lines (309 loc) · 8.16 KB
/
dmsg_tracker.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package dmsgtracker
import (
"context"
"io"
"sort"
"sync"
"time"
"github.com/skycoin/dmsg/pkg/dmsg"
"github.com/skycoin/dmsg/pkg/dmsgctrl"
"github.com/skycoin/skycoin/src/util/logging"
"github.com/skycoin/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire/pkg/skyenv"
)
// Default values for DmsgTrackerManager
const (
DefaultDTMUpdateInterval = time.Second * 30
DefaultDTMUpdateTimeout = time.Second * 10
)
// DmsgClientSummary summaries a dmsg client.
type DmsgClientSummary struct {
PK cipher.PubKey `json:"public_key"`
ServerPK cipher.PubKey `json:"server_public_key"`
RoundTrip time.Duration `json:"round_trip"`
}
// DmsgTracker tracks a dmsg client.
type DmsgTracker struct {
sum DmsgClientSummary // dmsg summary
ctrl *dmsgctrl.Control // dmsg ctrl
}
// newDmsgTracker creates a new DmsgTracker.
func newDmsgTracker(ctx context.Context, dmsgC *dmsg.Client, pk cipher.PubKey) (dt *DmsgTracker, err error) {
conn, err := dmsgC.DialStream(ctx, dmsg.Addr{PK: pk, Port: skyenv.DmsgCtrlPort})
if err != nil {
return nil, err
}
defer func() {
if err != nil {
_ = conn.Close() //nolint:errcheck
}
}()
ctrl := dmsgctrl.ControlStream(conn)
dur, err := ctrl.Ping(ctx)
if err != nil {
return nil, err
}
dt = &DmsgTracker{
sum: DmsgClientSummary{
PK: conn.RawRemoteAddr().PK,
ServerPK: conn.ServerPK(),
RoundTrip: dur,
},
ctrl: ctrl,
}
return dt, nil
}
// Update updates the dmsg client summary.
func (dt *DmsgTracker) Update(ctx context.Context) error {
dur, err := dt.ctrl.Ping(ctx)
if err != nil {
return err
}
dt.sum.RoundTrip = dur
return nil
}
// Manager tracks round trip durations for dmsg client connections.
type Manager struct {
updateInterval time.Duration
updateTimeout time.Duration
log *logging.Logger
dc *dmsg.Client
dts map[cipher.PubKey]*DmsgTracker
mx sync.Mutex
done chan struct{}
doneOnce sync.Once
}
// NewDmsgTrackerManager creates a new dmsg tracker manager.
func NewDmsgTrackerManager(mLog *logging.MasterLogger, dc *dmsg.Client, updateInterval, updateTimeout time.Duration) *Manager {
log := mLog.PackageLogger("dmsg_tracker_manager")
if updateInterval == 0 {
updateInterval = DefaultDTMUpdateInterval
}
if updateTimeout == 0 {
updateTimeout = DefaultDTMUpdateTimeout
}
dtm := &Manager{
updateInterval: updateInterval,
updateTimeout: updateTimeout,
log: log,
dc: dc,
dts: make(map[cipher.PubKey]*DmsgTracker),
done: make(chan struct{}),
}
if dc != nil {
go dtm.serve()
}
return dtm
}
// Serve serves the dmsg tracker manager.
func (dtm *Manager) serve() {
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-dtm.done
cancel()
}()
t := time.NewTicker(dtm.updateInterval)
defer t.Stop()
for {
select {
case <-dtm.done:
return
case <-t.C:
dtm.updateAllTrackers(ctx)
}
}
}
func (dtm *Manager) updateAllTrackers(ctx context.Context) {
dtm.mx.Lock()
defer dtm.mx.Unlock()
cancelCtx, cancel := context.WithDeadline(ctx, time.Now().Add(dtm.updateTimeout))
defer cancel()
log := dtm.log.WithField("func", "dtm.updateAllTrackers")
type errReport struct {
pk cipher.PubKey
err error
}
dtsLen := len(dtm.dts)
errCh := make(chan errReport, dtsLen)
defer close(errCh)
for _, dt := range dtm.dts {
dt := dt
go func() {
err := dt.Update(cancelCtx)
errCh <- errReport{pk: dt.sum.PK, err: err}
}()
}
for i := 0; i < dtsLen; i++ {
if r := <-errCh; r.err != nil {
log.WithError(r.err).
WithField("client_pk", r.pk).
Warn("Removing dmsg client tracker.")
delete(dtm.dts, r.pk)
}
}
}
// MustGet obtains a DmsgClientSummary of the client of given pk.
// If one is not found internally, a new tracker stream is to be established, returning error on failure.
func (dtm *Manager) MustGet(ctx context.Context, pk cipher.PubKey) (DmsgClientSummary, error) {
dtm.mx.Lock()
defer dtm.mx.Unlock()
if isDone(dtm.done) {
return DmsgClientSummary{}, io.ErrClosedPipe
}
if e, ok := dtm.dts[pk]; ok && !isDone(e.ctrl.Done()) {
return e.sum, nil
}
dt, err := dtm.mustEstablishTracker(ctx, pk)
if err != nil {
return DmsgClientSummary{}, err
}
dtm.dts[pk] = dt
return dt.sum, nil
}
// Get obtains a DmsgClientSummary of the client with given public key.
func (dtm *Manager) Get(pk cipher.PubKey) (DmsgClientSummary, bool) {
dtm.mx.Lock()
defer dtm.mx.Unlock()
if isDone(dtm.done) {
return DmsgClientSummary{}, false
}
return dtm.get(pk)
}
// mustEstablishTracker creates / re-creates tracker when dmsgTrackerMap entry got deleted, and reconnected.
// It returns *DmsgTracker on success or an error on failure.
func (dtm *Manager) mustEstablishTracker(ctx context.Context, pk cipher.PubKey) (*DmsgTracker, error) {
log := dtm.log.WithField("func", "dtm.mustEstablishTracker")
type errReport struct {
pk cipher.PubKey
err error
}
errCh := make(chan errReport)
defer close(errCh)
doneCh := make(chan struct{})
var dt *DmsgTracker
var err error
dCtx, cancel := context.WithDeadline(ctx, time.Now().Add(dtm.updateTimeout))
defer cancel()
go func() {
dt, err = newDmsgTracker(dCtx, dtm.dc, pk)
if err != nil {
select {
case <-ctx.Done():
return
default:
errCh <- errReport{pk: pk, err: err}
}
}
close(doneCh)
}()
select {
case r := <-errCh:
if r.err != nil {
log.WithError(r.err).WithField("client_pk", r.pk).Warn("Failed to re-create dmsgtracker client.")
}
return nil, r.err
case <-ctx.Done():
log.WithError(ctx.Err()).WithField("client_pk", pk).Warn("Failed to re-create dmsgtracker client.")
return nil, ctx.Err()
case <-doneCh:
log.WithField("client_pk", pk).Debug("Dmsgtracker client Established.")
return dt, nil
}
}
// mustEstablishTracker creates / re-creates tracker when dmsgTrackerMap entry got deleted, and reconnected.
// It is ment to be used as a goroutine and saves the new DmsgTracker to dtm.dts.
func (dtm *Manager) establishTracker(ctx context.Context, pk cipher.PubKey) {
log := dtm.log.WithField("func", "dtm.establishTracker")
type errReport struct {
pk cipher.PubKey
err error
}
errCh := make(chan errReport)
defer close(errCh)
doneCh := make(chan struct{})
dCtx, cancel := context.WithDeadline(ctx, time.Now().Add(dtm.updateTimeout))
defer cancel()
go func() {
dt, err := newDmsgTracker(dCtx, dtm.dc, pk)
if err != nil {
select {
case <-ctx.Done():
return
default:
errCh <- errReport{pk: pk, err: err}
}
}
dtm.mx.Lock()
if dt != nil {
dtm.dts[pk] = dt
}
dtm.mx.Unlock()
close(doneCh)
}()
select {
case r := <-errCh:
if r.err != nil {
log.WithError(r.err).WithField("client_pk", r.pk).Warn("Failed to re-create dmsgtracker client.")
}
case <-ctx.Done():
log.WithError(ctx.Err()).WithField("client_pk", pk).Warn("Failed to re-create dmsgtracker client.")
case <-doneCh:
log.WithField("client_pk", pk).Debug("Dmsgtracker client Established.")
}
}
// GetBulk obtains bulk dmsg client summaries.
// If one are not found internally, a new goroutine of tracker stream is to be established.
func (dtm *Manager) GetBulk(ctx context.Context, pks []cipher.PubKey) []DmsgClientSummary {
out := make([]DmsgClientSummary, 0)
for _, pk := range pks {
ds, ok := dtm.Get(pk)
if !ok {
// we establish tracker if there is none
go dtm.establishTracker(ctx, pk)
}
out = append(out, ds)
}
sort.Slice(out, func(i, j int) bool {
outI := out[i].PK.Big()
outJ := out[j].PK.Big()
return outI.Cmp(outJ) < 0
})
return out
}
func (dtm *Manager) get(pk cipher.PubKey) (DmsgClientSummary, bool) {
dt, ok := dtm.dts[pk]
if !ok {
return DmsgClientSummary{}, false
}
return dt.sum, true
}
// Close implements io.Closer
func (dtm *Manager) Close() error {
log := dtm.log.WithField("func", "dtm.Close")
dtm.mx.Lock()
defer dtm.mx.Unlock()
closed := false
dtm.doneOnce.Do(func() {
closed = true
close(dtm.done)
for pk, dt := range dtm.dts {
if err := dt.ctrl.Close(); err != nil {
log.WithError(err).
WithField("client_pk", pk).
Warn("Dmsg client closed with error.")
}
}
})
if !closed {
return io.ErrClosedPipe
}
return nil
}
func isDone(done <-chan struct{}) bool {
select {
case <-done:
return true
default:
return false
}
}