Skip to content

Commit 6bdf125

Browse files
committed
refactor: extract ping interface
1 parent 38be0dc commit 6bdf125

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

waku/v2/api/common/pinger.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package common
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/libp2p/go-libp2p/core/host"
8+
"github.com/libp2p/go-libp2p/core/peer"
9+
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
10+
)
11+
12+
type Pinger interface {
13+
PingPeer(ctx context.Context, peerID peer.ID) (time.Duration, error)
14+
}
15+
16+
type defaultPingImpl struct {
17+
host host.Host
18+
}
19+
20+
func NewDefaultPinger(host host.Host) Pinger {
21+
return &defaultPingImpl{
22+
host: host,
23+
}
24+
}
25+
26+
func (d *defaultPingImpl) PingPeer(ctx context.Context, peerID peer.ID) (time.Duration, error) {
27+
pingResultCh := ping.Ping(ctx, d.host, peerID)
28+
select {
29+
case <-ctx.Done():
30+
return 0, ctx.Err()
31+
case r := <-pingResultCh:
32+
if r.Error != nil {
33+
return 0, r.Error
34+
}
35+
return r.RTT, nil
36+
}
37+
}

waku/v2/api/history/cycle.go

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ import (
1414
"sync"
1515
"time"
1616

17-
"github.com/libp2p/go-libp2p/core/host"
1817
"github.com/libp2p/go-libp2p/core/peer"
19-
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
18+
"github.com/waku-org/go-waku/waku/v2/api/common"
2019
"github.com/waku-org/go-waku/waku/v2/protocol/store"
2120
"go.uber.org/zap"
2221
)
@@ -55,9 +54,8 @@ type StorenodeCycle struct {
5554

5655
logger *zap.Logger
5756

58-
host host.Host
59-
6057
storenodeConfigProvider StorenodeConfigProvider
58+
pinger common.Pinger
6159

6260
StorenodeAvailableOneshotEmitter *OneShotEmitter[struct{}]
6361
StorenodeChangedEmitter *Emitter[peer.ID]
@@ -71,7 +69,7 @@ type StorenodeCycle struct {
7169
peers map[peer.ID]peerStatus
7270
}
7371

74-
func NewStorenodeCycle(logger *zap.Logger) *StorenodeCycle {
72+
func NewStorenodeCycle(logger *zap.Logger, pinger common.Pinger) *StorenodeCycle {
7573
return &StorenodeCycle{
7674
StorenodeAvailableOneshotEmitter: NewOneshotEmitter[struct{}](),
7775
StorenodeChangedEmitter: NewEmitter[peer.ID](),
@@ -81,9 +79,8 @@ func NewStorenodeCycle(logger *zap.Logger) *StorenodeCycle {
8179
}
8280
}
8381

84-
func (m *StorenodeCycle) Start(ctx context.Context, h host.Host) {
82+
func (m *StorenodeCycle) Start(ctx context.Context) {
8583
m.logger.Debug("starting storenode cycle")
86-
m.host = h
8784
m.failedRequests = make(map[peer.ID]uint)
8885
m.peers = make(map[peer.ID]peerStatus)
8986

@@ -194,7 +191,7 @@ func (m *StorenodeCycle) getAvailableStorenodesSortedByRTT(ctx context.Context,
194191
ctx, cancel := context.WithTimeout(ctx, 4*time.Second)
195192
defer cancel()
196193

197-
rtt, err := m.pingPeer(ctx, peerID)
194+
rtt, err := m.pinger.PingPeer(ctx, peerID)
198195
if err == nil { // pinging storenodes might fail, but we don't care
199196
availableStorenodesMutex.Lock()
200197
availableStorenodes[peerID] = rtt
@@ -233,19 +230,6 @@ func (m *StorenodeCycle) getAvailableStorenodesSortedByRTT(ctx context.Context,
233230
return result
234231
}
235232

236-
func (m *StorenodeCycle) pingPeer(ctx context.Context, peerID peer.ID) (time.Duration, error) {
237-
pingResultCh := ping.Ping(ctx, m.host, peerID)
238-
select {
239-
case <-ctx.Done():
240-
return 0, ctx.Err()
241-
case r := <-pingResultCh:
242-
if r.Error != nil {
243-
return 0, r.Error
244-
}
245-
return r.RTT, nil
246-
}
247-
}
248-
249233
func (m *StorenodeCycle) findNewStorenode(ctx context.Context) error {
250234
// we have to override DNS manually because of https://github.com/status-im/status-mobile/issues/19581
251235
if overrideDNS {

0 commit comments

Comments
 (0)