Blame view

src/services/agent_util.lua 2.68 KB
314bc5df   zhengshouren   提交服务器初始代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  
  local _M = { }
  
  -- 超时次数
  local heartTimeoutCount 		= 0
  -- 加速次数
  local heartQuickCount 			= 0
  -- 上次检查心跳时间
  local lastHeartCheckTime		= 0
  -- 下次进入定时检查的时间
  local nextCheckTime				= 0
  -- 心跳误差允许范围
  local HEART_BEAT_ERROR_LIMIT 	= 1
  -- 最大超时次数
  local HEART_TIMEOUT_COUNT_MAX 	= 20
  -- 最大加速次数
  local HEART_QUICK_COUNT_MAX 	= 5
  -- 心跳定时间隔
  local HEART_TIMER_INTERVAL 		= 5
7cde0c44   zhouhaihai   忽略心跳和未登录状态 超时处理
20
21
22
23
24
  -- 忽略心跳的等待时间
  local WAIT_IGNORE_HEART			= 300
  
  --开始忽略心跳的时间
  local ignoreHeartTime = nil
314bc5df   zhengshouren   提交服务器初始代码
25
26
27
28
  
  local function check_heart_beat(agent, now)
  	-- 充值等操作不检查心跳
  	local role = agent.role
7cde0c44   zhouhaihai   忽略心跳和未登录状态 超时处理
29
  	if not role or role.ignoreHeartbeat then
314bc5df   zhengshouren   提交服务器初始代码
30
31
  		lastHeartCheckTime = now - HEART_TIMER_INTERVAL
  		heartTimeoutCount = 0
7cde0c44   zhouhaihai   忽略心跳和未登录状态 超时处理
32
33
34
35
36
37
38
39
40
41
  
  		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
314bc5df   zhengshouren   提交服务器初始代码
42
43
  		return
  	end
7cde0c44   zhouhaihai   忽略心跳和未登录状态 超时处理
44
45
  	ignoreHeartTime = nil
  
314bc5df   zhengshouren   提交服务器初始代码
46
47
48
49
50
51
52
53
54
55
56
57
58
  	if lastHeartCheckTime - now > HEART_TIMER_INTERVAL or
  		now - lastHeartCheckTime > HEART_TIMER_INTERVAL then
  		heartTimeoutCount = heartTimeoutCount + 1
  		if heartTimeoutCount >= HEART_TIMEOUT_COUNT_MAX then
  			skynet.error("timeout! then agent will shut down by self", agent.client_fd, role:getProperty("name"), role:getProperty("id"))
  			skynet.call(agent.gate_serv, "lua", "kick", agent.client_fd)
  			heartTimeoutCount = 0
  		end
  	else
  		heartTimeoutCount = 0
  	end
  end
  
314bc5df   zhengshouren   提交服务器初始代码
59
60
61
62
63
64
65
  function _M:update(agent)
  	local now = skynet.timex()
  	local role = agent.role
  	if now >= nextCheckTime then
  		pcall(check_heart_beat, agent, now)
  		nextCheckTime = now + HEART_TIMER_INTERVAL
  	end
7cde0c44   zhouhaihai   忽略心跳和未登录状态 超时处理
66
  	if not role then return end
dc9d814f   zhouhaihai   邮件
67
  	pcall(role.onRecoverTimer, role, now)
314bc5df   zhengshouren   提交服务器初始代码
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  end
  
  function _M:heart_beat(agent)
  	local now = skynet.timex()
  	if now == lastHeartCheckTime then
  		return
  	end
  	if now - lastHeartCheckTime <= HEART_TIMER_INTERVAL - HEART_BEAT_ERROR_LIMIT then
  		heartQuickCount = heartQuickCount + 1
  		if heartQuickCount == HEART_QUICK_COUNT_MAX then
  			-- 将错误写入日志
  			local role = agent.role
  			skynet.error("Warning, heart beating is too quick, shut down the agent", agent.client_fd, role:getProperty("name"), role:getProperty("id"))
  			-- skynet.call(agent.gate_serv, "lua", "kick", agent.client_fd)
  			role:warningHeartTooQuick()
  			heartQuickCount = 0
  		end
  	else
  		heartQuickCount = 0
  	end
  	lastHeartCheckTime = now
  end
  
  function _M:reset()
  	heartTimeoutCount = 0
  	heartQuickCount = 0
7cde0c44   zhouhaihai   忽略心跳和未登录状态 超时处理
94
  	ignoreHeartTime = nil
314bc5df   zhengshouren   提交服务器初始代码
95
96
97
98
  	lastHeartCheckTime = skynet.timex()
  end
  
  return _M