Browse Source

ping/pong timeout in config

pull/1095/head
zbo14 7 years ago
committed by Anton Kaliaev
parent
commit
91e4f4b786
No known key found for this signature in database GPG Key ID: 7B6881D965918214
2 changed files with 14 additions and 6 deletions
  1. +9
    -4
      p2p/conn/connection.go
  2. +5
    -2
      p2p/conn/connection_test.go

+ 9
- 4
p2p/conn/connection.go View File

@ -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()


+ 5
- 2
p2p/conn/connection_test.go View File

@ -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")
}
}


Loading…
Cancel
Save