Commit 7cde0c44fafe86df431485731e28473d4f313017

Authored by zhouhaihai
1 parent 5e6af9d6

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

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