diff --git a/p2p/conn/connection.go b/p2p/conn/connection.go index 04ca4228d..b49a45db0 100644 --- a/p2p/conn/connection.go +++ b/p2p/conn/connection.go @@ -22,8 +22,6 @@ const ( minReadBufferSize = 1024 minWriteBufferSize = 65536 updateStats = 2 * time.Second - pingTimeout = 40 * time.Second - pongTimeout = 60 * time.Second // some of these defaults are written in the user config // flushThrottle, sendRate, recvRate @@ -36,6 +34,8 @@ const ( defaultSendRate = int64(512000) // 500KB/s defaultRecvRate = int64(512000) // 500KB/s defaultSendTimeout = 10 * time.Second + defaultPingTimeout = 40 * time.Second + defaultPongTimeout = 60 * time.Second ) type receiveCbFunc func(chID byte, msgBytes []byte) @@ -100,6 +100,9 @@ type MConnConfig struct { MaxMsgPacketPayloadSize int FlushThrottle time.Duration + + pingTimeout time.Duration + pongTimeout time.Duration } func (cfg *MConnConfig) maxMsgPacketTotalSize() int { @@ -113,6 +116,8 @@ func DefaultMConnConfig() *MConnConfig { RecvRate: defaultRecvRate, MaxMsgPacketPayloadSize: defaultMaxMsgPacketPayloadSize, FlushThrottle: defaultFlushThrottle, + pingTimeout: defaultPingTimeout, + pongTimeout: defaultPongTimeout, } } @@ -172,8 +177,8 @@ func (c *MConnection) OnStart() error { } c.quit = make(chan struct{}) c.flushTimer = cmn.NewThrottleTimer("flush", c.config.FlushThrottle) - c.pingTimer = cmn.NewRepeatTimer("ping", pingTimeout) - c.pongTimer = cmn.NewThrottleTimer("pong", pongTimeout) + c.pingTimer = cmn.NewRepeatTimer("ping", c.config.pingTimeout) + c.pongTimer = cmn.NewThrottleTimer("pong", c.config.pongTimeout) c.chStatsTimer = cmn.NewRepeatTimer("chStats", updateStats) go c.sendRoutine() go c.recvRoutine() diff --git a/p2p/conn/connection_test.go b/p2p/conn/connection_test.go index 3bac0bd61..5686af6a6 100644 --- a/p2p/conn/connection_test.go +++ b/p2p/conn/connection_test.go @@ -23,7 +23,10 @@ func createTestMConnection(conn net.Conn) *MConnection { func createMConnectionWithCallbacks(conn net.Conn, onReceive func(chID byte, msgBytes []byte), onError func(r interface{})) *MConnection { chDescs := []*ChannelDescriptor{&ChannelDescriptor{ID: 0x01, Priority: 1, SendQueueCapacity: 1}} - c := NewMConnection(conn, chDescs, onReceive, onError) + cfg := DefaultMConnConfig() + cfg.pingTimeout = 40 * time.Millisecond + cfg.pongTimeout = 60 * time.Millisecond + c := NewMConnectionWithConfig(conn, chDescs, onReceive, onError, cfg) c.SetLogger(log.TestingLogger()) return c } @@ -142,7 +145,7 @@ func TestPingPongTimeout(t *testing.T) { case err := <-errorsCh: assert.NotNil(err) assert.False(mconn.IsRunning()) - case <-time.After(500*time.Millisecond + 100*time.Second): + case <-time.After(10*time.Millisecond + mconn.config.pingTimeout + mconn.config.pongTimeout): t.Fatal("Did not receive error in ~(pingTimeout + pongTimeout) seconds") } }