Skip to content

Commit 3dbd320

Browse files
committed
p2p: enable devp2p ping
This should prevent connection drops.
1 parent 1ec6190 commit 3dbd320

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

p2p/peer.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const (
2121
baseProtocolMaxMsgSize = 10 * 1024 * 1024
2222

2323
disconnectGracePeriod = 2 * time.Second
24+
pingInterval = 15 * time.Second
2425
)
2526

2627
const (
@@ -118,19 +119,33 @@ func (p *Peer) run() DiscReason {
118119
p.startProtocols()
119120
go func() { readErr <- p.readLoop() }()
120121

122+
ping := time.NewTicker(pingInterval)
123+
defer ping.Stop()
124+
121125
// Wait for an error or disconnect.
122126
var reason DiscReason
123-
select {
124-
case err := <-readErr:
125-
// We rely on protocols to abort if there is a write error. It
126-
// might be more robust to handle them here as well.
127-
p.DebugDetailf("Read error: %v\n", err)
128-
p.rw.Close()
129-
return DiscNetworkError
130-
131-
case err := <-p.protoErr:
132-
reason = discReasonForError(err)
133-
case reason = <-p.disc:
127+
loop:
128+
for {
129+
select {
130+
case <-ping.C:
131+
go func() {
132+
if err := EncodeMsg(p.rw, pingMsg, nil); err != nil {
133+
p.protoErr <- err
134+
return
135+
}
136+
}()
137+
case err := <-readErr:
138+
// We rely on protocols to abort if there is a write error. It
139+
// might be more robust to handle them here as well.
140+
p.DebugDetailf("Read error: %v\n", err)
141+
p.rw.Close()
142+
return DiscNetworkError
143+
case err := <-p.protoErr:
144+
reason = discReasonForError(err)
145+
break loop
146+
case reason = <-p.disc:
147+
break loop
148+
}
134149
}
135150
p.politeDisconnect(reason)
136151

0 commit comments

Comments
 (0)