Commit e24d1abd1d3f1a01389a4f114585ec5348c6e647
1 parent
987d660e
修改 排队
Showing
4 changed files
with
33 additions
and
49 deletions
Show diff stats
src/services/agent_ctrl.lua
@@ -5,6 +5,7 @@ local netpack = require "skynet.netpack" | @@ -5,6 +5,7 @@ local netpack = require "skynet.netpack" | ||
5 | local xxtea = require "xxtea" | 5 | local xxtea = require "xxtea" |
6 | local deque = require "deque" | 6 | local deque = require "deque" |
7 | local datacenter = require "skynet.datacenter" | 7 | local datacenter = require "skynet.datacenter" |
8 | +local agent_queued = require "services.agent_queued" | ||
8 | 9 | ||
9 | local pcall = pcall | 10 | local pcall = pcall |
10 | local string_format = string.format | 11 | local string_format = string.format |
@@ -59,16 +60,14 @@ function _M:exit_agent(fd) | @@ -59,16 +60,14 @@ function _M:exit_agent(fd) | ||
59 | 60 | ||
60 | pcall(skynet.send, agent, "lua", "exit") | 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 | if not nuid then | 66 | if not nuid then |
65 | pcall(skynet.send, poold, "lua", "feed") | 67 | pcall(skynet.send, poold, "lua", "feed") |
66 | else | 68 | else |
67 | self:query_agent(nfd, nuid, true) | 69 | self:query_agent(nfd, nuid, true) |
68 | end | 70 | end |
69 | - | ||
70 | - self.f2u[fd] = nil | ||
71 | - self.u2f[uid] = nil | ||
72 | end | 71 | end |
73 | 72 | ||
74 | -- @desc: 客户端连入 | 73 | -- @desc: 客户端连入 |
@@ -82,7 +81,7 @@ function _M:socket_close(fd) | @@ -82,7 +81,7 @@ function _M:socket_close(fd) | ||
82 | local uid = self.f2u[fd] | 81 | local uid = self.f2u[fd] |
83 | if not uid then | 82 | if not uid then |
84 | -- 排队中? | 83 | -- 排队中? |
85 | - pcall(skynet.call, queued_serv, "lua", "socket_close", fd) | 84 | + agent_queued.socket_close(fd) |
86 | return | 85 | return |
87 | end | 86 | end |
88 | self.f2e[fd] = skynet.timex() + AGENT_EXPIRE_TIME | 87 | self.f2e[fd] = skynet.timex() + AGENT_EXPIRE_TIME |
@@ -102,7 +101,7 @@ function _M:socket_error(fd) | @@ -102,7 +101,7 @@ function _M:socket_error(fd) | ||
102 | local uid = self.f2u[fd] | 101 | local uid = self.f2u[fd] |
103 | if not uid then | 102 | if not uid then |
104 | -- 排队中? | 103 | -- 排队中? |
105 | - pcall(skynet.call, queued_serv, "lua", "socket_close", fd) | 104 | + agent_queued.socket_close(fd) |
106 | return | 105 | return |
107 | end | 106 | end |
108 | 107 | ||
@@ -129,6 +128,7 @@ local next_log_time = 0 | @@ -129,6 +128,7 @@ local next_log_time = 0 | ||
129 | local CHECK_AGENT_STATUS_INTERVAL = 100 -- 检查agent状态的定时间隔 | 128 | local CHECK_AGENT_STATUS_INTERVAL = 100 -- 检查agent状态的定时间隔 |
130 | -- @desc: 检查agent状态,若过期,则让agent退出;并定时打日志统计在线人数 | 129 | -- @desc: 检查agent状态,若过期,则让agent退出;并定时打日志统计在线人数 |
131 | function _M:check_agent_status() | 130 | function _M:check_agent_status() |
131 | + agent_queued.handle_timeout() | ||
132 | local now = skynet.timex() | 132 | local now = skynet.timex() |
133 | if now >= next_check_time then | 133 | if now >= next_check_time then |
134 | next_check_time = now + CHECK_AGENT_STATUS_INTERVAL | 134 | next_check_time = now + CHECK_AGENT_STATUS_INTERVAL |
@@ -196,16 +196,26 @@ function _M:query_agent(fd, uid, isQueue) | @@ -196,16 +196,26 @@ function _M:query_agent(fd, uid, isQueue) | ||
196 | self.f2u[f] = nil | 196 | self.f2u[f] = nil |
197 | self.u2f[uid] = set_pack(fd, agent) | 197 | self.u2f[uid] = set_pack(fd, agent) |
198 | else | 198 | else |
199 | - -- 该uid未存储,则说明至少超过10分钟未登陆,由agent池服务pop出一个agent | ||
200 | - local agent | 199 | + local agent |
201 | if isQueue then | 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 | else | 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 | agent = self.factory:pop() | 216 | agent = self.factory:pop() |
205 | if not agent then | 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 | query_agent_response(fd, {ret = "RET_SERVER_FULL", rank = rank}) | 219 | query_agent_response(fd, {ret = "RET_SERVER_FULL", rank = rank}) |
210 | return | 220 | return |
211 | end | 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,7 +18,6 @@ local MAX_COUNT = tonumber(skynet.getenv("max_queue")) | ||
18 | -- 心跳定时间隔 | 18 | -- 心跳定时间隔 |
19 | local HEART_TIMER_INTERVAL = 30 | 19 | local HEART_TIMER_INTERVAL = 30 |
20 | local HEART_TIMEOUT_COUNT_MAX = 3 | 20 | local HEART_TIMEOUT_COUNT_MAX = 3 |
21 | -local gate_serv | ||
22 | 21 | ||
23 | local CMD = {} | 22 | local CMD = {} |
24 | local f2u = {} | 23 | local f2u = {} |
@@ -28,7 +27,6 @@ local curIdx = 0 -- 下一个即将进入游戏的玩家索引 | @@ -28,7 +27,6 @@ local curIdx = 0 -- 下一个即将进入游戏的玩家索引 | ||
28 | local nextIdx = 0 -- 新加的位置 | 27 | local nextIdx = 0 -- 新加的位置 |
29 | 28 | ||
30 | 29 | ||
31 | - | ||
32 | local function getRank(uid) | 30 | local function getRank(uid) |
33 | local info = u2i[uid] | 31 | local info = u2i[uid] |
34 | if not info then return -1 end | 32 | if not info then return -1 end |
@@ -63,18 +61,12 @@ skynet.register_protocol { | @@ -63,18 +61,12 @@ skynet.register_protocol { | ||
63 | end, | 61 | end, |
64 | dispatch = function(session, address, cmd, data) | 62 | dispatch = function(session, address, cmd, data) |
65 | skynet.ignoreret() | 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 | end | 67 | end |
72 | } | 68 | } |
73 | 69 | ||
74 | -function CMD.open(serv) | ||
75 | - gate_serv = serv | ||
76 | -end | ||
77 | - | ||
78 | function CMD.push(uid, fd) | 70 | function CMD.push(uid, fd) |
79 | uid = tostring(uid) | 71 | uid = tostring(uid) |
80 | if u2i[uid] then -- 存在] | 72 | if u2i[uid] then -- 存在] |
@@ -129,6 +121,7 @@ function CMD.pop() | @@ -129,6 +121,7 @@ function CMD.pop() | ||
129 | end | 121 | end |
130 | end | 122 | end |
131 | 123 | ||
124 | + | ||
132 | -- 下线了 | 125 | -- 下线了 |
133 | function CMD.socket_close(fd) | 126 | function CMD.socket_close(fd) |
134 | local uid = f2u[fd] | 127 | local uid = f2u[fd] |
@@ -138,8 +131,7 @@ function CMD.socket_close(fd) | @@ -138,8 +131,7 @@ function CMD.socket_close(fd) | ||
138 | info[2] = nil | 131 | info[2] = nil |
139 | end | 132 | end |
140 | 133 | ||
141 | - | ||
142 | -local function handle_timeout() | 134 | +function CMD.handle_timeout() |
143 | local now = skynet.timex() | 135 | local now = skynet.timex() |
144 | for uid, info in pairs(u2i) do | 136 | for uid, info in pairs(u2i) do |
145 | if info[2] and info[3] and now >= info[3][3] then --存在fd 检查心跳 | 137 | if info[2] and info[3] and now >= info[3][3] then --存在fd 检查心跳 |
@@ -153,26 +145,10 @@ local function handle_timeout() | @@ -153,26 +145,10 @@ local function handle_timeout() | ||
153 | end | 145 | end |
154 | end | 146 | end |
155 | end | 147 | end |
156 | - skynet.timeout(100, handle_timeout) | ||
157 | end | 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 | \ No newline at end of file | 154 | \ No newline at end of file |
155 | +return CMD |
src/services/poold.lua
@@ -7,7 +7,7 @@ local deque = require "deque" | @@ -7,7 +7,7 @@ local deque = require "deque" | ||
7 | local CMD = {} | 7 | local CMD = {} |
8 | local factory | 8 | local factory |
9 | 9 | ||
10 | -local PRE_FEED_COUNT = 1 | 10 | +local PRE_FEED_COUNT = 5 |
11 | local dead = 0 | 11 | local dead = 0 |
12 | -- agent死亡,通知poold补充,当累计到5个agent时,马上生成5个agent放入poold中 | 12 | -- agent死亡,通知poold补充,当累计到5个agent时,马上生成5个agent放入poold中 |
13 | -- 当然这里也可以写得更复杂,参考redis落地规则 | 13 | -- 当然这里也可以写得更复杂,参考redis落地规则 |
src/services/watchdog.lua
@@ -66,7 +66,6 @@ end | @@ -66,7 +66,6 @@ end | ||
66 | function CMD.start(conf) | 66 | function CMD.start(conf) |
67 | skynet.call(gate_serv, "lua", "open" , conf) | 67 | skynet.call(gate_serv, "lua", "open" , conf) |
68 | skynet.call(redisd, "lua", "open", conf) | 68 | skynet.call(redisd, "lua", "open", conf) |
69 | - skynet.call(queued_serv, "lua", "open", gate_serv) | ||
70 | 69 | ||
71 | if use_logd == 1 then | 70 | if use_logd == 1 then |
72 | skynet.call(logd, "lua", "open") | 71 | skynet.call(logd, "lua", "open") |
@@ -138,5 +137,4 @@ skynet.start(function() | @@ -138,5 +137,4 @@ skynet.start(function() | ||
138 | skynet.newservice("services/chated") | 137 | skynet.newservice("services/chated") |
139 | -- 网关服务 | 138 | -- 网关服务 |
140 | gate_serv = skynet.newservice("gate") | 139 | gate_serv = skynet.newservice("gate") |
141 | - queued_serv = skynet.newservice("services/queued") | ||
142 | end) | 140 | end) |