Commit e24d1abd1d3f1a01389a4f114585ec5348c6e647

Authored by zhouhaihai
1 parent 987d660e

修改 排队

src/services/agent_ctrl.lua
... ... @@ -5,6 +5,7 @@ local netpack = require "skynet.netpack"
5 5 local xxtea = require "xxtea"
6 6 local deque = require "deque"
7 7 local datacenter = require "skynet.datacenter"
  8 +local agent_queued = require "services.agent_queued"
8 9  
9 10 local pcall = pcall
10 11 local string_format = string.format
... ... @@ -59,16 +60,14 @@ function _M:exit_agent(fd)
59 60  
60 61 pcall(skynet.send, agent, "lua", "exit")
61 62  
62   - -- 这里检查是否有排队用户
63   - local nuid, nfd = skynet.call(queued_serv, "lua", "pop")
  63 + self.f2u[fd] = nil
  64 + self.u2f[uid] = nil
  65 + local nuid, nfd = agent_queued.pop()
64 66 if not nuid then
65 67 pcall(skynet.send, poold, "lua", "feed")
66 68 else
67 69 self:query_agent(nfd, nuid, true)
68 70 end
69   -
70   - self.f2u[fd] = nil
71   - self.u2f[uid] = nil
72 71 end
73 72  
74 73 -- @desc: 客户端连入
... ... @@ -82,7 +81,7 @@ function _M:socket_close(fd)
82 81 local uid = self.f2u[fd]
83 82 if not uid then
84 83 -- 排队中?
85   - pcall(skynet.call, queued_serv, "lua", "socket_close", fd)
  84 + agent_queued.socket_close(fd)
86 85 return
87 86 end
88 87 self.f2e[fd] = skynet.timex() + AGENT_EXPIRE_TIME
... ... @@ -102,7 +101,7 @@ function _M:socket_error(fd)
102 101 local uid = self.f2u[fd]
103 102 if not uid then
104 103 -- 排队中?
105   - pcall(skynet.call, queued_serv, "lua", "socket_close", fd)
  104 + agent_queued.socket_close(fd)
106 105 return
107 106 end
108 107  
... ... @@ -129,6 +128,7 @@ local next_log_time = 0
129 128 local CHECK_AGENT_STATUS_INTERVAL = 100 -- 检查agent状态的定时间隔
130 129 -- @desc: 检查agent状态,若过期,则让agent退出;并定时打日志统计在线人数
131 130 function _M:check_agent_status()
  131 + agent_queued.handle_timeout()
132 132 local now = skynet.timex()
133 133 if now >= next_check_time then
134 134 next_check_time = now + CHECK_AGENT_STATUS_INTERVAL
... ... @@ -196,16 +196,26 @@ function _M:query_agent(fd, uid, isQueue)
196 196 self.f2u[f] = nil
197 197 self.u2f[uid] = set_pack(fd, agent)
198 198 else
199   - -- 该uid未存储,则说明至少超过10分钟未登陆,由agent池服务pop出一个agent
200   - local agent
  199 + local agent
201 200 if isQueue then
202   - agent = skynet.newservice("agent")
  201 + agent = self.factory:pop()
  202 + if agent then
  203 + pcall(skynet.send, poold, "lua", "feed")
  204 + else
  205 + agent = skynet.newservice("agent")
  206 + end
203 207 else
  208 + -- 该uid未存储,则说明至少超过10分钟未登陆,由agent池服务pop出一个agent
  209 + if agent_queued.count() > 0 then
  210 + -- 服务器满 开始排队
  211 + local rank = agent_queued.push(uid, fd)
  212 + query_agent_response(fd, {ret = "RET_SERVER_FULL", rank = rank})
  213 + return
  214 + end
  215 +
204 216 agent = self.factory:pop()
205 217 if not agent then
206   - -- 服务器满
207   - -- 开始排队
208   - local ok, rank = pcall(skynet.call, queued_serv, "lua", "push", uid, fd)
  218 + local rank = agent_queued.push(uid, fd)
209 219 query_agent_response(fd, {ret = "RET_SERVER_FULL", rank = rank})
210 220 return
211 221 end
... ...
src/services/queued.lua renamed to src/services/agent_queued.lua
... ... @@ -18,7 +18,6 @@ local MAX_COUNT = tonumber(skynet.getenv("max_queue"))
18 18 -- 心跳定时间隔
19 19 local HEART_TIMER_INTERVAL = 30
20 20 local HEART_TIMEOUT_COUNT_MAX = 3
21   -local gate_serv
22 21  
23 22 local CMD = {}
24 23 local f2u = {}
... ... @@ -28,7 +27,6 @@ local curIdx = 0 -- 下一个即将进入游戏的玩家索引
28 27 local nextIdx = 0 -- 新加的位置
29 28  
30 29  
31   -
32 30 local function getRank(uid)
33 31 local info = u2i[uid]
34 32 if not info then return -1 end
... ... @@ -63,18 +61,12 @@ skynet.register_protocol {
63 61 end,
64 62 dispatch = function(session, address, cmd, data)
65 63 skynet.ignoreret()
66   - cs(function()
67   - if cmd == actionCodes.Sys_checkQueue then
68   - checkQueue(session)
69   - end
70   - end)
  64 + if cmd == actionCodes.Sys_checkQueue then
  65 + checkQueue(session)
  66 + end
71 67 end
72 68 }
73 69  
74   -function CMD.open(serv)
75   - gate_serv = serv
76   -end
77   -
78 70 function CMD.push(uid, fd)
79 71 uid = tostring(uid)
80 72 if u2i[uid] then -- 存在]
... ... @@ -129,6 +121,7 @@ function CMD.pop()
129 121 end
130 122 end
131 123  
  124 +
132 125 -- 下线了
133 126 function CMD.socket_close(fd)
134 127 local uid = f2u[fd]
... ... @@ -138,8 +131,7 @@ function CMD.socket_close(fd)
138 131 info[2] = nil
139 132 end
140 133  
141   -
142   -local function handle_timeout()
  134 +function CMD.handle_timeout()
143 135 local now = skynet.timex()
144 136 for uid, info in pairs(u2i) do
145 137 if info[2] and info[3] and now >= info[3][3] then --存在fd 检查心跳
... ... @@ -153,26 +145,10 @@ local function handle_timeout()
153 145 end
154 146 end
155 147 end
156   - skynet.timeout(100, handle_timeout)
157 148 end
158 149  
  150 +function CMD.count()
  151 + return (nextIdx + MAX_COUNT - curIdx) % MAX_COUNT
  152 +end
159 153  
160   -skynet.start(function()
161   - skynet.dispatch("lua", function(session, source, command, ...)
162   - -- skynet.trace() --执行序的跟踪统计
163   - local f = CMD[command]
164   - if f then
165   - skynet.ret(skynet.pack(f(...)))
166   - end
167   - end)
168   -
169   - skynet.info_func(function()
170   - local info = {}
171   - info.count = (nextIdx + MAX_COUNT - curIdx) % MAX_COUNT
172   - return info
173   - end)
174   -
175   - cs = queue()
176   -
177   - skynet.timeout(100, handle_timeout)
178   -end)
179 154 \ No newline at end of file
  155 +return CMD
... ...
src/services/poold.lua
... ... @@ -7,7 +7,7 @@ local deque = require "deque"
7 7 local CMD = {}
8 8 local factory
9 9  
10   -local PRE_FEED_COUNT = 1
  10 +local PRE_FEED_COUNT = 5
11 11 local dead = 0
12 12 -- agent死亡,通知poold补充,当累计到5个agent时,马上生成5个agent放入poold中
13 13 -- 当然这里也可以写得更复杂,参考redis落地规则
... ...
src/services/watchdog.lua
... ... @@ -66,7 +66,6 @@ end
66 66 function CMD.start(conf)
67 67 skynet.call(gate_serv, "lua", "open" , conf)
68 68 skynet.call(redisd, "lua", "open", conf)
69   - skynet.call(queued_serv, "lua", "open", gate_serv)
70 69  
71 70 if use_logd == 1 then
72 71 skynet.call(logd, "lua", "open")
... ... @@ -138,5 +137,4 @@ skynet.start(function()
138 137 skynet.newservice("services/chated")
139 138 -- 网关服务
140 139 gate_serv = skynet.newservice("gate")
141   - queued_serv = skynet.newservice("services/queued")
142 140 end)
... ...