Skip to content

Commit 1c625a5

Browse files
committed
Option to set ping interval and connection health threshold
1 parent ca12176 commit 1c625a5

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

peer.go

Lines changed: 10 additions & 7 deletions
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{},
@@ -782,11 +785,11 @@ func (p *Peer) versionMessage(address string) *wire.MsgVersion {
782785
func (p *Peer) startMonitorPingPong() {
783786
p.healthMonitorWg.Add(1)
784787

785-
pingTicker := time.NewTicker(pingInterval)
788+
pingTicker := time.NewTicker(p.pingInterval)
786789

787790
go func() {
788791
// if no ping/pong signal is received for certain amount of time, mark peer as unhealthy
789-
monitorConnectionTicker := time.NewTicker(connectionHealthTickerDuration)
792+
monitorConnectionTicker := time.NewTicker(p.connectionHealthThreshold)
790793

791794
defer func() {
792795
p.healthMonitorWg.Done()
@@ -804,7 +807,7 @@ func (p *Peer) startMonitorPingPong() {
804807
p.writeChan <- wire.NewMsgPing(nonce)
805808
case <-p.pingPongAlive:
806809
// if ping/pong signal is received reset the ticker
807-
monitorConnectionTicker.Reset(connectionHealthTickerDuration)
810+
monitorConnectionTicker.Reset(p.connectionHealthThreshold)
808811
p.setHealthy()
809812
case <-monitorConnectionTicker.C:
810813

peer_manager.go

Lines changed: 0 additions & 1 deletion
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 11 additions & 0 deletions
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)