Commit 6a009043c8e6527f0e340b457dc5b71e9a3fac01

Authored by zhouhaihai
2 parents 445aae2c 3f604f2e

Merge branch 'develop' into tr/ob

* develop:
  扩容 redis 和 log服务
  监听再前
  增加池子
  拆分网关
  清除引导功能顺便也清除关卡解锁触发的强制引导
robot/robot.conf
1 1 root = "./"
2   -thread = 8
  2 +thread = 4
3 3 logger = nil
4 4 harbor = 0
5   -start = "start" -- main script
  5 +start = "robot_main" -- main script
6 6 bootstrap = "snlua bootstrap" -- The service for bootstrap
7 7  
8 8 lua_path = root .."skynet/lualib/?.lua;"..root.."robot/?.lua;"..root.."src/?.lua"
... ...
robot/robot.lua
... ... @@ -16,6 +16,7 @@ local eventListener = {}
16 16  
17 17 local ignoreListener = {
18 18 ["Role.updateProperty"] = function(data)
  19 + if not client.role then return end
19 20 local msg = MsgPack.unpack(data)
20 21 for _, one in pairs(msg) do
21 22 client.role[one.key] = one.newValue
... ... @@ -80,7 +81,7 @@ function triggerListener(listener, data)
80 81 ignoreListener[listener](data)
81 82 end
82 83 else
83   - log(listener .. " no handle!!!")
  84 + -- log(listener .. " no handle!!!")
84 85 end
85 86 end
86 87  
... ... @@ -103,8 +104,8 @@ function sendServer(actionCode, bin)
103 104 end
104 105  
105 106 function requestServer(actionCode, bin, callback, isKeep)
106   - sendServer(actionCode, bin)
107 107 addListener(actionCode, callback, isKeep)
  108 + sendServer(actionCode, bin)
108 109 end
109 110  
110 111 local function heartBeat()
... ...
robot/robot_main.lua 0 → 100644
... ... @@ -0,0 +1,21 @@
  1 +local skynet = require "skynet"
  2 +local config = require "robot_config"
  3 +
  4 +local preOnlineCount = 10
  5 +
  6 +
  7 +skynet.start(function()
  8 + local need = math.ceil(config.online / preOnlineCount)
  9 + local inpre = math.ceil(config.inpre / need)
  10 + local idRange = math.ceil(config.max / need)
  11 + local curId = 1
  12 + local poold = skynet.newservice("robot_pool")
  13 + local pooldObj = skynet.call(poold, "lua", "start", config.online + preOnlineCount)
  14 +
  15 + for i = 1, need do
  16 + local start = skynet.newservice("start")
  17 + skynet.send(start, "lua", "start", poold, pooldObj, preOnlineCount, curId, curId + idRange - 1, inpre)
  18 + curId = curId + idRange
  19 + end
  20 + skynet.exit()
  21 +end)
0 22 \ No newline at end of file
... ...
robot/robot_pool.lua 0 → 100644
... ... @@ -0,0 +1,46 @@
  1 +--[[
  2 + deque 为了减少锁竞争,尽量保证 a服务 push;b服务 pop
  3 +]]
  4 +local skynet = require "skynet"
  5 +local deque = require "deque"
  6 +
  7 +local CMD = {}
  8 +local factory
  9 +
  10 +local PRE_FEED_COUNT = 5
  11 +local dead = 0
  12 +-- agent死亡,通知poold补充,当累计到5个agent时,马上生成5个agent放入poold中
  13 +-- 当然这里也可以写得更复杂,参考redis落地规则
  14 +function CMD.feed()
  15 + dead = dead + 1
  16 + if dead == PRE_FEED_COUNT then
  17 + dead = 0
  18 + for i=1, PRE_FEED_COUNT do
  19 + factory:push(skynet.newservice("robot"))
  20 + end
  21 + end
  22 +end
  23 +
  24 +-- 系统启动,生成count个agent放入双端队列中
  25 +function CMD.start(count)
  26 + factory, addr = deque.new(count)
  27 +
  28 + for i = 1, count do
  29 + factory:push(skynet.newservice("robot"))
  30 + end
  31 +
  32 + return addr
  33 +end
  34 +
  35 +local function __init__()
  36 + skynet.dispatch("lua", function (_, _, command, ...)
  37 + local f = CMD[command]
  38 + if command == "start" then
  39 + skynet.ret(skynet.pack(f(...)))
  40 + return
  41 + end
  42 + f(...)
  43 + end)
  44 +end
  45 +
  46 +skynet.start(__init__)
... ...
robot/start.lua
... ... @@ -5,13 +5,17 @@ local config = require "robot_config"
5 5 local skynet = require "skynet"
6 6 local netpack = require "skynet.netpack"
7 7 local socketdriver = require "skynet.socketdriver"
  8 +local deque = require "deque"
8 9 local string_format = string.format
9 10  
  11 +local poold, pooldObj, onlineCount, startId, endId, inpre
  12 +local factory
  13 +
10 14 local queue = {}
11 15 local fd2serv = {}
12 16 local id2fd = {}
13 17 local MSG = {}
14   -local id_max = 0 -- robot id
  18 +local id_max
15 19  
16 20  
17 21 function MSG.open( ... )
... ... @@ -21,9 +25,11 @@ end
21 25 function MSG.close(fd)
22 26 if fd2serv[fd] and not fd2serv[fd].closing then
23 27 fd2serv[fd].closing = true
  28 + print(fd2serv[fd].agent)
24 29 skynet.call(fd2serv[fd].agent, "lua", "exit")
25   - log(string_format("logout %s", fd2serv[fd].id))
  30 + pcall(skynet.send, poold, "lua", "feed")
26 31  
  32 + log(string_format("logout %s", fd2serv[fd].id))
27 33 id2fd[fd2serv[fd].id] = nil
28 34 fd2serv[fd] = nil
29 35 end
... ... @@ -72,13 +78,14 @@ skynet.register_protocol {
72 78 }
73 79  
74 80 local function add_robot()
75   - local robot = skynet.newservice("robot")
  81 + local time = skynet.time()
  82 + local robot = factory:pop()
76 83 local fd = socketdriver.connect(config.host, config.port)
77 84 socketdriver.start(fd)
78 85 local id
79   - if id_max >= config.max then
  86 + if id_max >= endId then
80 87 while true do
81   - id = math.randomInt(1, config.max)
  88 + id = math.randomInt(startId, endId)
82 89 if not id2fd[id] then
83 90 break
84 91 end
... ... @@ -89,9 +96,8 @@ local function add_robot()
89 96 end
90 97 fd2serv[fd] = {agent = robot, id = id}
91 98 id2fd[id] = fd
92   - pcall(skynet.call, robot, "lua", "start", fd, id)
  99 + pcall(skynet.call, robot, "lua", "start", fd, id, prefix)
93 100 log(string_format("login %s", fd2serv[fd].id))
94   -
95 101 -- 定时下线
96 102 skynet.timeout(math.randomInt(config.online_time[1], config.online_time[2]) * 100, function()
97 103 MSG.close(fd)
... ... @@ -106,8 +112,8 @@ local function online()
106 112 end
107 113  
108 114 -- 及时补充人数
109   - if curCount < config.online then
110   - for i = curCount + 1, config.online do
  115 + if curCount < onlineCount then
  116 + for i = curCount + 1, onlineCount do
111 117 add_robot()
112 118 end
113 119 end
... ... @@ -116,11 +122,15 @@ local function online()
116 122 skynet.timeout(100, online)
117 123 end
118 124  
119   -local function start()
  125 +local function start(pooldp, pooldObj, onlineCountp, startIdp, endIdp, inprep)
120 126 log("start testing ...")
121 127  
122   - for i = 1, config.online, config.inpre do
123   - for j = 1, config.inpre do
  128 + factory = deque.clone(pooldObj)
  129 + poold, onlineCount, startId, endId, inpre = pooldp, onlineCountp, startIdp, endIdp, inprep
  130 + id_max = startId - 1
  131 +
  132 + for i = 1, onlineCount, inpre do
  133 + for j = 1, inpre do
124 134 add_robot()
125 135 end
126 136 skynet.sleep(100)
... ... @@ -131,5 +141,10 @@ local function start()
131 141 end
132 142  
133 143 skynet.start(function()
134   - skynet.fork(start)
  144 + skynet.dispatch("lua", function (_, _, command, ...)
  145 + if command == "start" then
  146 + start(...)
  147 + return
  148 + end
  149 + end)
135 150 end)
136 151 \ No newline at end of file
... ...
robot/unitTest/item.lua
... ... @@ -14,7 +14,7 @@ end
14 14 local taskMap = {
15 15 saleItem = {100},
16 16 openItem = {100},
17   - openTimeBox = {100},
  17 + -- openTimeBox = {100},
18 18 }
19 19  
20 20 function _M:task()
... ...
src/actions/GmAction.lua
... ... @@ -502,6 +502,11 @@ function _M.guide(role, pms)
502 502 end
503 503 end
504 504 end
  505 + for _, data in pairs(csvdb["guide_unlockCsv"]) do
  506 + if data.type == 0 or data.type == 3 then
  507 + str = str:setv(data.id,1)
  508 + end
  509 + end
505 510 role:updateProperty({field = "funcGuide", value = str})
506 511 role:mylog("gm_action", {desc = "sguide", key1 = pms.sender})
507 512  
... ...
src/agent.lua
... ... @@ -356,11 +356,6 @@ skynet.start(function()
356 356 return info
357 357 end)
358 358  
359   - redisd = skynet.localname(".redis")
360   - if tonumber(skynet.getenv "logd") == 1 then
361   - logd = skynet.localname(".log")
362   - end
363   -
364 359 cs = queue()
365 360  
366 361 pvpd = skynet.localname(".pvpcross")
... ...
src/main.lua
... ... @@ -2,23 +2,41 @@ local skynet = require &quot;skynet&quot;
2 2  
3 3 local max_client = tonumber(skynet.getenv("max_client"))
4 4 local max_queue = tonumber(skynet.getenv("max_queue"))
  5 +local work_count = tonumber(skynet.getenv("thread"))
  6 +local use_logd = tonumber(skynet.getenv("logd"))
5 7  
6 8 skynet.start(function()
7 9 print("Server start")
8 10 skynet.newservice("debug_console", tonumber(skynet.getenv("debug_port")))
9 11  
  12 +
  13 + -- 启动redis
  14 + for i = 1, work_count do
  15 + local redisd = skynet.newservice("services/redisd", i)
  16 + skynet.call(redisd, "lua", "open", {
  17 + redishost = skynet.getenv("redis_host"),
  18 + redisport = tonumber(skynet.getenv("redis_port")),
  19 + redisdb = tonumber(skynet.getenv("redis_db")),
  20 + auth = skynet.getenv("redis_auth")
  21 + })
  22 + end
  23 +
  24 + --启动log
  25 + if use_logd == 1 then
  26 + for i = 1, work_count do
  27 + local logd = skynet.newservice("services/logd", i)
  28 + skynet.call(logd, "lua", "open")
  29 + end
  30 + end
  31 +
10 32 local httpd = skynet.newservice("services/httpweb", tonumber(skynet.getenv("httpweb_port")))
11 33 local watchdog = skynet.newservice("services/watchdog", max_client)
12 34  
  35 +
13 36 skynet.call(watchdog, "lua", "start", {
14 37 port = tonumber(skynet.getenv("server_port")),
15 38 maxclient = max_client + max_queue + 10,
16 39 httpd = httpd,
17   -
18   - redishost = skynet.getenv("redis_host"),
19   - redisport = tonumber(skynet.getenv("redis_port")),
20   - redisdb = tonumber(skynet.getenv("redis_db")),
21   - auth = skynet.getenv("redis_auth"),
22 40 })
23 41  
24 42 skynet.exit()
... ...
src/models/RoleLog.lua
1 1 local serverId = skynet.getenv("servId")
2 2 local server_id = (skynet.getenv("serverType") or "localtest") .. "_" .. serverId
  3 +local logproxy = require "shared.logproxy"
3 4  
4 5 --[[
5 6 100 购买/兑换行为
... ... @@ -765,8 +766,7 @@ function RoleLog.bind(Role)
765 766 end
766 767 end
767 768 end
768   - if not logd then return end
769   - pcall(skynet.send, logd, "lua", "log", doc, "bi")
  769 + logproxy:log(doc, "bi")
770 770 end
771 771  
772 772 function Role:logItems(itemId, before, after, log)
... ... @@ -825,8 +825,7 @@ function RoleLog.bind(Role)
825 825 end
826 826 end
827 827 doc["@type"] = logType
828   - if not logd then return end
829   - pcall(skynet.send, logd, "lua", "log", doc, "log")
  828 + logproxy:log(doc, "log")
830 829 end
831 830  
832 831  
... ...
src/services/agent_ctrl.lua
... ... @@ -6,6 +6,7 @@ local xxtea = require &quot;xxtea&quot;
6 6 local deque = require "deque"
7 7 local datacenter = require "skynet.datacenter"
8 8 local agent_queued = require "services.agent_queued"
  9 +local logproxy = require "shared.logproxy"
9 10  
10 11 local pcall = pcall
11 12 local string_format = string.format
... ... @@ -139,11 +140,11 @@ function _M:check_agent_status()
139 140 end
140 141 end
141 142  
142   - if now >= next_log_time and now % 60 == 0 and logd then
  143 + if now >= next_log_time and now % 60 == 0 then
143 144 next_log_time = now + 60
144 145 local count = table_nums(self.u2f)
145 146 datacenter.set("onlineCount", count)
146   - pcall(skynet.send, logd, "lua", "log", {["@type"] = "online", count = count}, "log")
  147 + logproxy:log({["@type"] = "online", count = count}, "log")
147 148 end
148 149 end
149 150  
... ...
src/services/dbseed.lua
... ... @@ -34,8 +34,6 @@ local steps = {
34 34 }
35 35  
36 36 skynet.start(function ()
37   - redisd = skynet.localname(".redis")
38   -
39 37 redisproxy = require("shared.redisproxy")
40 38  
41 39 local new = redisproxy:hsetnx("autoincrement_set", "server_start", os.date("%Y%m%d", skynet.timex())) == 1
... ...
src/services/globald.lua
... ... @@ -190,7 +190,6 @@ local function __init__()
190 190 end
191 191 end
192 192 end)
193   - redisd = skynet.localname(".redis")
194 193 skynet.register(".globald")
195 194  
196 195 end
... ...
src/services/httpweb.lua
... ... @@ -53,12 +53,8 @@ end
53 53 local CMD = require "actions.HttpAction"
54 54  
55 55 local function start()
56   - redisd = skynet.localname(".redis")
57 56 globalCsv = csvdb["GlobalDefineCsv"]
58 57  
59   - if tonumber(skynet.getenv "logd") == 1 then
60   - logd = skynet.localname(".log")
61   - end
62 58  
63 59 local listen_socket = socket.listen("0.0.0.0", port)
64 60 print("Listen web port " .. port)
... ...
src/services/logd.lua
... ... @@ -2,12 +2,12 @@ local skynet = require &quot;skynet&quot;
2 2 local queue = require "skynet.queue"
3 3 local bson = require "bson"
4 4 local socketdriver = require "skynet.socketdriver"
5   -
6 5 local serverId = tonumber(skynet.getenv("servId"))
7 6  
8 7 require "shared.init"
9 8 require "skynet.manager"
10 9  
  10 +local logdIdx = ...
11 11 local table_insert = table.insert
12 12 local pairs = pairs
13 13 local ipairs = ipairs
... ... @@ -15,6 +15,7 @@ local string_format = string.format
15 15  
16 16 local logId = 0
17 17 local CMD, cs = {}
  18 +local prefix = "wasteland" .. logdIdx .. "S" .. serverId .. "C"
18 19  
19 20 local logHandle = {
20 21 bi = {
... ... @@ -33,7 +34,7 @@ local logHandle = {
33 34 doc["game_name"] = "wasteland"
34 35 doc["env"] = "cb"
35 36 doc["game_name_type"] = "guaji"
36   - doc["log_id"] = "wastelandC" .. logId .. "S" .. serverId .. "T" .. now
  37 + doc["log_id"] = prefix .. logId .. "T" .. now
37 38 logId = (logId + 1) % 10000000
38 39 end
39 40 },
... ... @@ -135,7 +136,7 @@ local function __init__()
135 136 end)
136 137 cs = queue()
137 138  
138   - skynet.register(".log")
  139 + skynet.register(".logd" .. logdIdx)
139 140 end
140 141  
141 142 skynet.start(__init__)
... ...
src/services/pvpd.lua
... ... @@ -289,7 +289,6 @@ end
289 289  
290 290 ------------------------------------------------------
291 291 function CMD.start()
292   - redisd = skynet.localname(".redis")
293 292 globalCsv = csvdb["GlobalDefineCsv"]
294 293  
295 294 pvpInfo = require("models.Pvpd").new({key = "cross:pvpInfo"})
... ...
src/services/redisd.lua
... ... @@ -3,7 +3,7 @@ require &quot;skynet.manager&quot;
3 3 local redis = require "skynet.db.redis"
4 4  
5 5 local db
6   -
  6 +local idx = ...
7 7 local command = {}
8 8  
9 9 function command.open(conf)
... ... @@ -27,5 +27,5 @@ skynet.start(function()
27 27 skynet.info_func(function()
28 28 return skynet.stat("mqlen")
29 29 end)
30   - skynet.register ".redis"
  30 + skynet.register(".redis" .. idx)
31 31 end)
32 32 \ No newline at end of file
... ...
src/services/watchdog.lua
1 1 local skynet = require "skynet"
2 2 require "skynet.manager"
3   -local redisproxy = require "shared.redisproxy"
4 3 local socket = require "skynet.socket"
5 4 local netpack = require "skynet.netpack"
6 5 local datacenter = require "skynet.datacenter"
... ... @@ -47,7 +46,7 @@ function SOCKET.data(fd, msg)
47 46 end
48 47 end
49 48  
50   -local use_logd = tonumber(skynet.getenv "logd")
  49 +
51 50  
52 51 -- @desc: agent状态定时检测
53 52 function check_agent_status()
... ... @@ -65,11 +64,7 @@ end
65 64  
66 65 function CMD.start(conf)
67 66 skynet.call(gate_serv, "lua", "open" , conf)
68   - skynet.call(redisd, "lua", "open", conf)
69   -
70   - if use_logd == 1 then
71   - skynet.call(logd, "lua", "open")
72   - end
  67 +
73 68 skynet.call(pvpd, "lua", "start")
74 69 -- 开启agent状态检测定时器
75 70 check_agent_status()
... ... @@ -110,17 +105,11 @@ skynet.start(function()
110 105 end
111 106 end)
112 107 skynet.register ".watchdog"
113   - -- 数据库服务
114   - redisd = skynet.newservice("services/redisd")
115 108  
116 109 -- 提前加载好
117 110 csvdata.init()
118 111 print("launch csvdatad ...")
119 112  
120   - -- 日志服务
121   - if use_logd == 1 then
122   - logd = skynet.newservice("services/logd")
123   - end
124 113 -- pvp 服务
125 114 pvpd = skynet.newservice("services/pvpd")
126 115 cluster.register("pvpd", pvpd)
... ...
src/shared/logproxy.lua 0 → 100644
... ... @@ -0,0 +1,23 @@
  1 +
  2 +local skynet = require "skynet"
  3 +local logd_count = tonumber(skynet.getenv("thread"))
  4 +local use_logd = tonumber(skynet.getenv("logd"))
  5 +
  6 +
  7 +local logd
  8 +skynet.init(function()
  9 + if use_logd then
  10 + local idx = math.randomInt(1, logd_count)
  11 + logd = skynet.localname(".logd" .. idx)
  12 + end
  13 +end)
  14 +
  15 +local logproxy = {}
  16 +
  17 +function logproxy:log(doc, logTo)
  18 + if use_logd and logd then
  19 + pcall(skynet.send, logd, "lua", "log", doc, logTo)
  20 + end
  21 +end
  22 +
  23 +return logproxy
0 24 \ No newline at end of file
... ...
src/shared/redisproxy.lua
1 1 local skynet = require "skynet"
2   -local harbor = require "skynet.harbor"
  2 +require "utils.init"
  3 +
  4 +local redisd_count = tonumber(skynet.getenv("thread"))
  5 +local redisd
  6 +skynet.init(function()
  7 + local idx = math.randomInt(1, redisd_count)
  8 + redisd = skynet.localname(".redis" .. idx)
  9 +end)
3 10  
4 11 local table_insert = table.insert
5 12  
6 13 local redisproxy = {}
7 14  
8   -
9 15 local isUsePika = false
10 16  
11 17  
... ...