ping/pong timeout in config

This commit is contained in:
zbo14 2017-11-09 14:27:17 -05:00 committed by Anton Kaliaev
parent 9b554fb2c4
commit 91e4f4b786
No known key found for this signature in database
GPG Key ID: 7B6881D965918214
2 changed files with 14 additions and 6 deletions

View File

@ -22,8 +22,6 @@ const (
minReadBufferSize = 1024 minReadBufferSize = 1024
minWriteBufferSize = 65536 minWriteBufferSize = 65536
updateStats = 2 * time.Second updateStats = 2 * time.Second
pingTimeout = 40 * time.Second
pongTimeout = 60 * time.Second
// some of these defaults are written in the user config // some of these defaults are written in the user config
// flushThrottle, sendRate, recvRate // flushThrottle, sendRate, recvRate
@ -36,6 +34,8 @@ const (
defaultSendRate = int64(512000) // 500KB/s defaultSendRate = int64(512000) // 500KB/s
defaultRecvRate = int64(512000) // 500KB/s defaultRecvRate = int64(512000) // 500KB/s
defaultSendTimeout = 10 * time.Second defaultSendTimeout = 10 * time.Second
defaultPingTimeout = 40 * time.Second
defaultPongTimeout = 60 * time.Second
) )
type receiveCbFunc func(chID byte, msgBytes []byte) type receiveCbFunc func(chID byte, msgBytes []byte)
@ -100,6 +100,9 @@ type MConnConfig struct {
MaxMsgPacketPayloadSize int MaxMsgPacketPayloadSize int
FlushThrottle time.Duration FlushThrottle time.Duration
pingTimeout time.Duration
pongTimeout time.Duration
} }
func (cfg *MConnConfig) maxMsgPacketTotalSize() int { func (cfg *MConnConfig) maxMsgPacketTotalSize() int {
@ -113,6 +116,8 @@ func DefaultMConnConfig() *MConnConfig {
RecvRate: defaultRecvRate, RecvRate: defaultRecvRate,
MaxMsgPacketPayloadSize: defaultMaxMsgPacketPayloadSize, MaxMsgPacketPayloadSize: defaultMaxMsgPacketPayloadSize,
FlushThrottle: defaultFlushThrottle, FlushThrottle: defaultFlushThrottle,
pingTimeout: defaultPingTimeout,
pongTimeout: defaultPongTimeout,
} }
} }
@ -172,8 +177,8 @@ func (c *MConnection) OnStart() error {
} }
c.quit = make(chan struct{}) c.quit = make(chan struct{})
c.flushTimer = cmn.NewThrottleTimer("flush", c.config.FlushThrottle) c.flushTimer = cmn.NewThrottleTimer("flush", c.config.FlushThrottle)
c.pingTimer = cmn.NewRepeatTimer("ping", pingTimeout) c.pingTimer = cmn.NewRepeatTimer("ping", c.config.pingTimeout)
c.pongTimer = cmn.NewThrottleTimer("pong", pongTimeout) c.pongTimer = cmn.NewThrottleTimer("pong", c.config.pongTimeout)
c.chStatsTimer = cmn.NewRepeatTimer("chStats", updateStats) c.chStatsTimer = cmn.NewRepeatTimer("chStats", updateStats)
go c.sendRoutine() go c.sendRoutine()
go c.recvRoutine() go c.recvRoutine()

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 { 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}} 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()) c.SetLogger(log.TestingLogger())
return c return c
} }
@ -142,7 +145,7 @@ func TestPingPongTimeout(t *testing.T) {
case err := <-errorsCh: case err := <-errorsCh:
assert.NotNil(err) assert.NotNil(err)
assert.False(mconn.IsRunning()) 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") t.Fatal("Did not receive error in ~(pingTimeout + pongTimeout) seconds")
} }
} }