Commit 7cde0c44fafe86df431485731e28473d4f313017

Authored by zhouhaihai
1 parent 5e6af9d6

忽略心跳和未登录状态 超时处理

Showing 2 changed files with 22 additions and 7 deletions   Show diff stats
src/agent.lua
... ... @@ -29,12 +29,6 @@ _codeSession = {}
29 29 --- {{{ 定时器相关
30 30 local function handle_timeout()
31 31 if not agentInfo.open_timer then return end
32   -
33   - if not agentInfo.role then
34   - skynet.timeout(100, handle_timeout)
35   - return
36   - end
37   -
38 32 agent_util:update(agentInfo)
39 33 skynet.timeout(100, handle_timeout)
40 34 end
... ... @@ -274,6 +268,8 @@ function CMD.start(session, source, gate, fd, ip, hotfixs)
274 268 end
275 269 end
276 270  
  271 + start_agent_timer()
  272 +
277 273 -- 这里将消息伪装成 watchdog 发出,这样就由 A->B->C->B->A 变成 A->B->C->A
278 274 skynet.redirect(gate, source, "lua", session, skynet.pack("forward", fd, 0, skynet.self()))
279 275 end
... ...
src/services/agent_util.lua
... ... @@ -17,15 +17,32 @@ local HEART_TIMEOUT_COUNT_MAX = 20
17 17 local HEART_QUICK_COUNT_MAX = 5
18 18 -- 心跳定时间隔
19 19 local HEART_TIMER_INTERVAL = 5
  20 +-- 忽略心跳的等待时间
  21 +local WAIT_IGNORE_HEART = 300
  22 +
  23 +--开始忽略心跳的时间
  24 +local ignoreHeartTime = nil
20 25  
21 26 local function check_heart_beat(agent, now)
22 27 -- 充值等操作不检查心跳
23 28 local role = agent.role
24   - if role.ignoreHeartbeat then
  29 + if not role or role.ignoreHeartbeat then
25 30 lastHeartCheckTime = now - HEART_TIMER_INTERVAL
26 31 heartTimeoutCount = 0
  32 +
  33 + if not ignoreHeartTime then
  34 + ignoreHeartTime = now
  35 + end
  36 +
  37 + if now - ignoreHeartTime >= WAIT_IGNORE_HEART then -- 等待太久了 踢掉
  38 + skynet.error("timeout! then agent will shut down by self with ignoreHeartbeat or no login", agent.client_fd)
  39 + skynet.call(agent.gate_serv, "lua", "kick", agent.client_fd)
  40 + ignoreHeartTime = nil
  41 + end
27 42 return
28 43 end
  44 + ignoreHeartTime = nil
  45 +
29 46 if lastHeartCheckTime - now > HEART_TIMER_INTERVAL or
30 47 now - lastHeartCheckTime > HEART_TIMER_INTERVAL then
31 48 heartTimeoutCount = heartTimeoutCount + 1
... ... @@ -46,6 +63,7 @@ function _M:update(agent)
46 63 pcall(check_heart_beat, agent, now)
47 64 nextCheckTime = now + HEART_TIMER_INTERVAL
48 65 end
  66 + if not role then return end
49 67 pcall(role.onRecoverTimer, role, now)
50 68 end
51 69  
... ... @@ -73,6 +91,7 @@ end
73 91 function _M:reset()
74 92 heartTimeoutCount = 0
75 93 heartQuickCount = 0
  94 + ignoreHeartTime = nil
76 95 lastHeartCheckTime = skynet.timex()
77 96 end
78 97  
... ...