|
|
@ -28,20 +28,7 @@ func WithHeartbeat(interval time.Duration, hbdFn func(conn *conn.Connection) []b
|
|
|
|
panic("dataFn must not be nil")
|
|
|
|
panic("dataFn must not be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return func(ngin *nnet.Engine) {
|
|
|
|
var startFn event.OnConnectedFn = func(nc *conn.Connection) {
|
|
|
|
ngin.EventManager().RegisterEvent(event.EvtOnConnected, m.start)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ngin.Use(func(next rt.HandlerFunc) rt.HandlerFunc {
|
|
|
|
|
|
|
|
return func(conn *conn.Connection, pkg packet.IPacket) {
|
|
|
|
|
|
|
|
m.handle(conn, pkg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
next(conn, pkg)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (m *HeartbeatMiddleware) start(conn *conn.Connection) {
|
|
|
|
|
|
|
|
ticker := time.NewTicker(m.interval)
|
|
|
|
ticker := time.NewTicker(m.interval)
|
|
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
defer func() {
|
|
|
@ -54,15 +41,28 @@ func (m *HeartbeatMiddleware) start(conn *conn.Connection) {
|
|
|
|
deadline := time.Now().Add(-2 * m.interval).Unix()
|
|
|
|
deadline := time.Now().Add(-2 * m.interval).Unix()
|
|
|
|
if atomic.LoadInt64(&m.lastAt) < deadline {
|
|
|
|
if atomic.LoadInt64(&m.lastAt) < deadline {
|
|
|
|
nlog.Errorf("Heartbeat timeout, LastTime=%d, Deadline=%d", atomic.LoadInt64(&m.lastAt), deadline)
|
|
|
|
nlog.Errorf("Heartbeat timeout, LastTime=%d, Deadline=%d", atomic.LoadInt64(&m.lastAt), deadline)
|
|
|
|
return
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err := conn.SendBytes(m.hbdFn(conn))
|
|
|
|
err := nc.SendBytes(m.hbdFn(nc))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
nlog.Errorf("Heartbeat err: %v", err)
|
|
|
|
nlog.Errorf("Heartbeat err: %v", err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return func(ngin *nnet.Engine) {
|
|
|
|
|
|
|
|
ngin.EventManager().RegisterEvent(event.EvtOnConnected, startFn)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ngin.Use(func(next rt.HandlerFunc) rt.HandlerFunc {
|
|
|
|
|
|
|
|
return func(conn *conn.Connection, pkg packet.IPacket) {
|
|
|
|
|
|
|
|
m.handle(conn, pkg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
next(conn, pkg)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *HeartbeatMiddleware) handle(_ *conn.Connection, _ packet.IPacket) {
|
|
|
|
func (m *HeartbeatMiddleware) handle(_ *conn.Connection, _ packet.IPacket) {
|
|
|
|