/
ping.go
175 lines (157 loc) · 3.65 KB
/
ping.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
package p2p
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
ma "github.com/multiformats/go-multiaddr"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)
const (
pingProtectTag = "spacemesh-ping"
)
type pingStat struct {
numSuccess int
numFail int
}
type Ping struct {
sync.Mutex
logger *zap.Logger
h host.Host
peers []peer.ID
pr routing.PeerRouting
interval time.Duration
stats map[peer.ID]*pingStat
cancel context.CancelFunc
eg errgroup.Group
}
type PingOpt func(p *Ping)
func WithPingInterval(d time.Duration) PingOpt {
return func(p *Ping) {
p.interval = d
}
}
func NewPing(logger *zap.Logger, h host.Host, peers []peer.ID, pr routing.PeerRouting, opts ...PingOpt) *Ping {
p := &Ping{
logger: logger,
h: h,
peers: peers,
pr: pr,
stats: make(map[peer.ID]*pingStat),
interval: time.Second,
}
for _, opt := range opts {
opt(p)
}
return p
}
func (p *Ping) Start() {
if p.cancel != nil {
return
}
var startCtx context.Context
startCtx, p.cancel = context.WithCancel(context.Background())
for _, peer := range p.peers {
p.startForPeer(startCtx, peer)
}
}
func (p *Ping) Stop() {
if p.cancel == nil {
return
}
p.cancel()
p.cancel = nil
p.eg.Wait()
}
func (p *Ping) doPing(ctx context.Context, peerID peer.ID) (<-chan ping.Result, error) {
_, err := p.pr.FindPeer(ctx, peerID)
if err != nil {
return nil, fmt.Errorf("failed to find peer %s: %w", peerID, err)
}
return ping.Ping(ctx, p.h, peerID), nil
}
// runForPeer runs ping for the specific peer till an error occurs
// or the context is canceled.
func (p *Ping) runForPeer(ctx context.Context, peerID peer.ID, addrs []ma.Multiaddr) error {
// go-libp2p Ping tends to stop working after an error is received on the channel.
// This behavior is probably not intended and may change later.
// So, we create a child context here and cancel it when we receive an error
// on the channel returned by Ping, and then after a delay we restart
// Ping for this peer.
pingCtx, cancel := context.WithCancel(ctx)
defer cancel()
ch, err := p.doPing(pingCtx, peerID)
if err != nil {
return err
}
for r := range ch {
p.record(peerID, r.Error == nil)
if r.Error != nil {
return r.Error
} else {
p.logger.Info("PING succeeded",
zap.Stringer("peer", peerID),
zap.Any("addrs", addrs),
zap.Duration("rtt", r.RTT))
}
select {
case <-ctx.Done():
return nil
case <-time.After(p.interval):
}
}
return nil
}
func (p *Ping) startForPeer(ctx context.Context, peerID peer.ID) {
p.h.ConnManager().Protect(peerID, pingProtectTag)
p.eg.Go(func() error {
for {
var addrs []ma.Multiaddr
for _, c := range p.h.Network().ConnsToPeer(peerID) {
addrs = append(addrs, c.RemoteMultiaddr())
}
err := p.runForPeer(ctx, peerID, addrs)
if errors.Is(err, context.Canceled) {
return nil
}
p.logger.Error("PING failed",
zap.Stringer("peer", peerID),
zap.Any("addrs", addrs),
zap.Error(err))
select {
case <-ctx.Done():
return nil
case <-time.After(p.interval):
}
}
})
}
func (p *Ping) record(peerID peer.ID, success bool) {
p.Lock()
defer p.Unlock()
st := p.stats[peerID]
if st == nil {
st = &pingStat{}
p.stats[peerID] = st
}
if success {
p.stats[peerID].numSuccess++
} else {
p.stats[peerID].numFail++
}
}
func (p *Ping) Stats(peerID peer.ID) (numSuccess, numFail int) {
p.Lock()
defer p.Unlock()
st, found := p.stats[peerID]
if !found {
return 0, 0
}
return st.numSuccess, st.numFail
}