aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/peer.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2015-02-19 23:53:52 +0800
committerFelix Lange <fjl@twurst.com>2015-02-19 23:54:54 +0800
commit3dbd32093cd1060c339b3351fbb676b8c7cc31f0 (patch)
tree41f92007b2e08aee7826f147fc8ac5ccdd1bf35a /p2p/peer.go
parent1ec6190e866eeefd91ea82b31da9f94753685a4a (diff)
downloadgo-tangerine-3dbd32093cd1060c339b3351fbb676b8c7cc31f0.tar
go-tangerine-3dbd32093cd1060c339b3351fbb676b8c7cc31f0.tar.gz
go-tangerine-3dbd32093cd1060c339b3351fbb676b8c7cc31f0.tar.bz2
go-tangerine-3dbd32093cd1060c339b3351fbb676b8c7cc31f0.tar.lz
go-tangerine-3dbd32093cd1060c339b3351fbb676b8c7cc31f0.tar.xz
go-tangerine-3dbd32093cd1060c339b3351fbb676b8c7cc31f0.tar.zst
go-tangerine-3dbd32093cd1060c339b3351fbb676b8c7cc31f0.zip
p2p: enable devp2p ping
This should prevent connection drops.
Diffstat (limited to 'p2p/peer.go')
-rw-r--r--p2p/peer.go37
1 files changed, 26 insertions, 11 deletions
diff --git a/p2p/peer.go b/p2p/peer.go
index b9bf0fd73..fb027c834 100644
--- a/p2p/peer.go
+++ b/p2p/peer.go
@@ -21,6 +21,7 @@ const (
baseProtocolMaxMsgSize = 10 * 1024 * 1024
disconnectGracePeriod = 2 * time.Second
+ pingInterval = 15 * time.Second
)
const (
@@ -118,19 +119,33 @@ func (p *Peer) run() DiscReason {
p.startProtocols()
go func() { readErr <- p.readLoop() }()
+ ping := time.NewTicker(pingInterval)
+ defer ping.Stop()
+
// Wait for an error or disconnect.
var reason DiscReason
- select {
- case err := <-readErr:
- // We rely on protocols to abort if there is a write error. It
- // might be more robust to handle them here as well.
- p.DebugDetailf("Read error: %v\n", err)
- p.rw.Close()
- return DiscNetworkError
-
- case err := <-p.protoErr:
- reason = discReasonForError(err)
- case reason = <-p.disc:
+loop:
+ for {
+ select {
+ case <-ping.C:
+ go func() {
+ if err := EncodeMsg(p.rw, pingMsg, nil); err != nil {
+ p.protoErr <- err
+ return
+ }
+ }()
+ case err := <-readErr:
+ // We rely on protocols to abort if there is a write error. It
+ // might be more robust to handle them here as well.
+ p.DebugDetailf("Read error: %v\n", err)
+ p.rw.Close()
+ return DiscNetworkError
+ case err := <-p.protoErr:
+ reason = discReasonForError(err)
+ break loop
+ case reason = <-p.disc:
+ break loop
+ }
}
p.politeDisconnect(reason)