-
Notifications
You must be signed in to change notification settings - Fork 26
/
service_advertises.go
95 lines (78 loc) · 1.9 KB
/
service_advertises.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
package tinder
import (
"context"
"fmt"
"time"
"go.uber.org/zap"
"berty.tech/weshnet/pkg/logutil"
)
const defaultTTL = time.Hour
// StartAdvertises topic on each of service drivers
func (s *Service) StartAdvertises(ctx context.Context, topic string, opts ...Option) error {
if len(s.drivers) == 0 {
return fmt.Errorf("no driver available to advertise")
}
var aopts Options
if err := aopts.apply(opts...); err != nil {
return fmt.Errorf("failed to advertise: %w", err)
}
for _, driver := range s.drivers {
// skip filter driver
if aopts.DriverFilters.ShouldFilter(driver.Name()) {
continue
}
// start background job
go func(driver IDriver) {
if err := s.advertise(ctx, driver, topic); err != nil {
s.logger.Debug("advertise ended", zap.Error(err))
}
}(driver)
}
return nil
}
func (s *Service) advertise(ctx context.Context, d IDriver, topic string) error {
for {
currentAddrs := s.networkNotify.GetLastUpdatedAddrs(ctx)
now := time.Now()
ttl, err := d.Advertise(ctx, topic)
took := time.Since(now)
var deadline time.Duration
if err != nil {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
// retry in 30 seconds
deadline = time.Second * 30
} else {
if ttl == 0 {
ttl = defaultTTL
}
deadline = 4 * ttl / 5
}
s.logger.Debug("advertise",
zap.String("driver", d.Name()),
logutil.PrivateString("topic", topic),
zap.Duration("ttl", ttl),
zap.Duration("took", took),
zap.Duration("next", deadline),
zap.Error(err),
)
waitctx, cancel := context.WithTimeout(ctx, deadline)
// wait for network update or waitctx expire
_, ok := s.networkNotify.WaitForUpdate(waitctx, currentAddrs)
cancel()
// filter addrs
if !ok {
select {
// check for parent ctx
case <-ctx.Done():
// main context has expire, stop
return ctx.Err()
default:
// waitContext has expire, continue
}
}
}
}