Skip to content

Commit 8733372

Browse files
committed
ARCO-105: Option to set ping interval and connection health threshold
1 parent 922042b commit 8733372

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

peer.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ const (
4040
retryReadWriteMessageAttempts = 5
4141
reconnectInterval = 10 * time.Second
4242

43-
pingInterval = 30 * time.Second
44-
connectionHealthTickerDuration = 1 * time.Minute
43+
pingIntervalDefault = 30 * time.Second
44+
connectionHealthTickerDurationDefault = 1 * time.Minute
4545
)
4646

4747
type Block struct {
@@ -77,8 +77,9 @@ type Peer struct {
7777
retryReadWriteMessageInterval time.Duration
7878
nrWriteHandlers int
7979
isUnhealthyCh chan struct{}
80-
81-
ctx context.Context
80+
pingInterval time.Duration
81+
connectionHealthThreshold time.Duration
82+
ctx context.Context
8283

8384
cancelReadHandler context.CancelFunc
8485
cancelWriteHandler context.CancelFunc
@@ -114,6 +115,8 @@ func NewPeer(logger *slog.Logger, address string, peerHandler PeerHandlerI, netw
114115
maximumMessageSize: defaultMaximumMessageSize,
115116
batchDelay: defaultBatchDelayMilliseconds * time.Millisecond,
116117
retryReadWriteMessageInterval: retryReadWriteMessageIntervalDefault,
118+
pingInterval: pingIntervalDefault,
119+
connectionHealthThreshold: connectionHealthTickerDurationDefault,
117120
writerWg: &sync.WaitGroup{},
118121
readerWg: &sync.WaitGroup{},
119122
reconnectingWg: &sync.WaitGroup{},
@@ -790,11 +793,11 @@ func (p *Peer) versionMessage(address string) *wire.MsgVersion {
790793
func (p *Peer) startMonitorPingPong() {
791794
p.healthMonitorWg.Add(1)
792795

793-
pingTicker := time.NewTicker(pingInterval)
796+
pingTicker := time.NewTicker(p.pingInterval)
794797

795798
go func() {
796799
// if no ping/pong signal is received for certain amount of time, mark peer as unhealthy
797-
monitorConnectionTicker := time.NewTicker(connectionHealthTickerDuration)
800+
monitorConnectionTicker := time.NewTicker(p.connectionHealthThreshold)
798801

799802
defer func() {
800803
p.healthMonitorWg.Done()
@@ -812,7 +815,7 @@ func (p *Peer) startMonitorPingPong() {
812815
p.writeChan <- wire.NewMsgPing(nonce)
813816
case <-p.pingPongAlive:
814817
// if ping/pong signal is received reset the ticker
815-
monitorConnectionTicker.Reset(connectionHealthTickerDuration)
818+
monitorConnectionTicker.Reset(p.connectionHealthThreshold)
816819
p.setHealthy()
817820
case <-monitorConnectionTicker.C:
818821

peer_manager.go

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ type PeerManager struct {
2121
logger *slog.Logger
2222
ebs int64
2323
restartUnhealthyPeers bool
24-
monitorPeersInterval time.Duration
2524
waitGroup sync.WaitGroup
2625
cancelAll context.CancelFunc
2726
ctx context.Context

peer_manager_options.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ func WithExcessiveBlockSize(ebs int64) PeerManagerOptions {
1616
}
1717
}
1818

19-
func WithRestartUnhealthyPeers(monitorPeersInterval time.Duration) PeerManagerOptions {
19+
func WithRestartUnhealthyPeers() PeerManagerOptions {
2020
return func(p *PeerManager) {
2121
p.restartUnhealthyPeers = true
22-
p.monitorPeersInterval = monitorPeersInterval
2322
}
2423
}

peer_options.go

+11
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,14 @@ func WithNrOfWriteHandlers(NrWriteHandlers int) PeerOptions {
6161
return nil
6262
}
6363
}
64+
65+
// WithPingInterval sets the optional time duration ping interval and connection health threshold
66+
// ping interval is the time interval in which the peer sends a ping
67+
// connection health threshold is the time duration after which the connection is marked unhealthy if no signal is received
68+
func WithPingInterval(pingInterval time.Duration, connectionHealthThreshold time.Duration) PeerOptions {
69+
return func(p *Peer) error {
70+
p.pingInterval = pingInterval
71+
p.connectionHealthThreshold = connectionHealthThreshold
72+
return nil
73+
}
74+
}

0 commit comments

Comments
 (0)