aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaran <maran.hidskes@gmail.com>2014-06-10 19:51:34 +0800
committerMaran <maran.hidskes@gmail.com>2014-06-10 19:51:34 +0800
commit771f64397fd8638e9a40a4b9ecc64a9b70d6f2e4 (patch)
tree5d6b82a5629d3ce24ebdd315d462ee8d6fac76f9
parentd8339fa29a884516166993a6d29caed59f32ca1b (diff)
downloadgo-tangerine-771f64397fd8638e9a40a4b9ecc64a9b70d6f2e4.tar.gz
go-tangerine-771f64397fd8638e9a40a4b9ecc64a9b70d6f2e4.tar.zst
go-tangerine-771f64397fd8638e9a40a4b9ecc64a9b70d6f2e4.zip
Stop peers when they don't respond to ping/pong. Might fix ethereum/go-ethereum#78
-rw-r--r--peer.go15
1 files changed, 12 insertions, 3 deletions
diff --git a/peer.go b/peer.go
index eed5bec30..9cc892d8b 100644
--- a/peer.go
+++ b/peer.go
@@ -18,6 +18,8 @@ const (
outputBufferSize = 50
// Current protocol version
ProtocolVersion = 17
+ // Interval for ping/pong message
+ pingPongTimer = 30 * time.Second
)
type DiscReason byte
@@ -243,7 +245,7 @@ func (p *Peer) writeMessage(msg *ethwire.Msg) {
err := ethwire.WriteMessage(p.conn, msg)
if err != nil {
- ethutil.Config.Log.Debugln("Can't send message:", err)
+ ethutil.Config.Log.Debugln("[PEER] Can't send message:", err)
// Stop the client if there was an error writing to it
p.Stop()
return
@@ -253,7 +255,7 @@ func (p *Peer) writeMessage(msg *ethwire.Msg) {
// Outbound message handler. Outbound messages are handled here
func (p *Peer) HandleOutbound() {
// The ping timer. Makes sure that every 2 minutes a ping is send to the peer
- pingTimer := time.NewTicker(30 * time.Second)
+ pingTimer := time.NewTicker(pingPongTimer)
serviceTimer := time.NewTicker(5 * time.Minute)
out:
@@ -264,8 +266,14 @@ out:
p.writeMessage(msg)
p.lastSend = time.Now()
- // Ping timer sends a ping to the peer each 2 minutes
+ // Ping timer
case <-pingTimer.C:
+ timeSince := time.Since(time.Unix(p.lastPong, 0))
+ if p.pingStartTime.IsZero() == false && timeSince > (pingPongTimer+10*time.Second) {
+ ethutil.Config.Log.Infof("[PEER] Peer did not respond to latest pong fast enough, it took %s, disconnecting.\n", timeSince)
+ p.Stop()
+ return
+ }
p.writeMessage(ethwire.NewMessage(ethwire.MsgPingTy, ""))
p.pingStartTime = time.Now()
@@ -563,6 +571,7 @@ func (p *Peer) Stop() {
// Pre-emptively remove the peer; don't wait for reaping. We already know it's dead if we are here
p.ethereum.RemovePeer(p)
+ ethutil.Config.Log.Debugln("[PEER] Stopped peer:", p.conn.RemoteAddr())
}
func (p *Peer) pushHandshake() error {