diff --git a/src/agent.lua b/src/agent.lua index 2b37c5d..cf6e8ac 100644 --- a/src/agent.lua +++ b/src/agent.lua @@ -29,12 +29,6 @@ _codeSession = {} --- {{{ 定时器相关 local function handle_timeout() if not agentInfo.open_timer then return end - - if not agentInfo.role then - skynet.timeout(100, handle_timeout) - return - end - agent_util:update(agentInfo) skynet.timeout(100, handle_timeout) end @@ -274,6 +268,8 @@ function CMD.start(session, source, gate, fd, ip, hotfixs) end end + start_agent_timer() + -- 这里将消息伪装成 watchdog 发出,这样就由 A->B->C->B->A 变成 A->B->C->A skynet.redirect(gate, source, "lua", session, skynet.pack("forward", fd, 0, skynet.self())) end diff --git a/src/services/agent_util.lua b/src/services/agent_util.lua index 933dd8a..6c05154 100644 --- a/src/services/agent_util.lua +++ b/src/services/agent_util.lua @@ -17,15 +17,32 @@ local HEART_TIMEOUT_COUNT_MAX = 20 local HEART_QUICK_COUNT_MAX = 5 -- 心跳定时间隔 local HEART_TIMER_INTERVAL = 5 +-- 忽略心跳的等待时间 +local WAIT_IGNORE_HEART = 300 + +--开始忽略心跳的时间 +local ignoreHeartTime = nil local function check_heart_beat(agent, now) -- 充值等操作不检查心跳 local role = agent.role - if role.ignoreHeartbeat then + if not role or role.ignoreHeartbeat then lastHeartCheckTime = now - HEART_TIMER_INTERVAL heartTimeoutCount = 0 + + if not ignoreHeartTime then + ignoreHeartTime = now + end + + if now - ignoreHeartTime >= WAIT_IGNORE_HEART then -- 等待太久了 踢掉 + skynet.error("timeout! then agent will shut down by self with ignoreHeartbeat or no login", agent.client_fd) + skynet.call(agent.gate_serv, "lua", "kick", agent.client_fd) + ignoreHeartTime = nil + end return end + ignoreHeartTime = nil + if lastHeartCheckTime - now > HEART_TIMER_INTERVAL or now - lastHeartCheckTime > HEART_TIMER_INTERVAL then heartTimeoutCount = heartTimeoutCount + 1 @@ -46,6 +63,7 @@ function _M:update(agent) pcall(check_heart_beat, agent, now) nextCheckTime = now + HEART_TIMER_INTERVAL end + if not role then return end pcall(role.onRecoverTimer, role, now) end @@ -73,6 +91,7 @@ end function _M:reset() heartTimeoutCount = 0 heartQuickCount = 0 + ignoreHeartTime = nil lastHeartCheckTime = skynet.timex() end -- libgit2 0.21.2