From cbfd05138190544c646e5ac28cae90a6e5e8ab1a Mon Sep 17 00:00:00 2001 From: zhouhaihai Date: Wed, 16 Dec 2020 20:07:17 +0800 Subject: [PATCH] 增加池子 --- robot/robot.lua | 3 ++- robot/robot_main.lua | 6 +++++- robot/robot_pool.lua | 46 ++++++++++++++++++++++++++++++++++++++++++++++ robot/start.lua | 31 ++++++++++++++++++++----------- 4 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 robot/robot_pool.lua diff --git a/robot/robot.lua b/robot/robot.lua index 408b144..b1c714d 100644 --- a/robot/robot.lua +++ b/robot/robot.lua @@ -16,6 +16,7 @@ local eventListener = {} local ignoreListener = { ["Role.updateProperty"] = function(data) + if not client.role then return end local msg = MsgPack.unpack(data) for _, one in pairs(msg) do client.role[one.key] = one.newValue @@ -80,7 +81,7 @@ function triggerListener(listener, data) ignoreListener[listener](data) end else - log(listener .. " no handle!!!") + -- log(listener .. " no handle!!!") end end diff --git a/robot/robot_main.lua b/robot/robot_main.lua index 275909c..031af53 100644 --- a/robot/robot_main.lua +++ b/robot/robot_main.lua @@ -9,8 +9,12 @@ skynet.start(function() local inpre = math.ceil(config.inpre / need) local idRange = math.ceil(config.max / need) local curId = 1 + local poold = skynet.newservice("robot_pool") + local pooldObj = skynet.call(poold, "lua", "start", config.online + preOnlineCount) + for i = 1, need do - skynet.newservice("start", preOnlineCount, curId, curId + idRange - 1, inpre) + local start = skynet.newservice("start") + skynet.send(start, "lua", "start", poold, pooldObj, preOnlineCount, curId, curId + idRange - 1, inpre) curId = curId + idRange end skynet.exit() diff --git a/robot/robot_pool.lua b/robot/robot_pool.lua new file mode 100644 index 0000000..c011d0f --- /dev/null +++ b/robot/robot_pool.lua @@ -0,0 +1,46 @@ +--[[ + deque 为了减少锁竞争,尽量保证 a服务 push;b服务 pop +]] +local skynet = require "skynet" +local deque = require "deque" + +local CMD = {} +local factory + +local PRE_FEED_COUNT = 5 +local dead = 0 +-- agent死亡,通知poold补充,当累计到5个agent时,马上生成5个agent放入poold中 +-- 当然这里也可以写得更复杂,参考redis落地规则 +function CMD.feed() + dead = dead + 1 + if dead == PRE_FEED_COUNT then + dead = 0 + for i=1, PRE_FEED_COUNT do + factory:push(skynet.newservice("robot")) + end + end +end + +-- 系统启动,生成count个agent放入双端队列中 +function CMD.start(count) + factory, addr = deque.new(count) + + for i = 1, count do + factory:push(skynet.newservice("robot")) + end + + return addr +end + +local function __init__() + skynet.dispatch("lua", function (_, _, command, ...) + local f = CMD[command] + if command == "start" then + skynet.ret(skynet.pack(f(...))) + return + end + f(...) + end) +end + +skynet.start(__init__) diff --git a/robot/start.lua b/robot/start.lua index 3579734..a23d7d4 100644 --- a/robot/start.lua +++ b/robot/start.lua @@ -5,19 +5,17 @@ local config = require "robot_config" local skynet = require "skynet" local netpack = require "skynet.netpack" local socketdriver = require "skynet.socketdriver" +local deque = require "deque" local string_format = string.format -local onlineCount, startId, endId, inpre = ... -onlineCount = tonumber(onlineCount) -startId = tonumber(startId) -endId = tonumber(endId) -inpre = tonumber(inpre) +local poold, pooldObj, onlineCount, startId, endId, inpre +local factory local queue = {} local fd2serv = {} local id2fd = {} local MSG = {} -local id_max = startId - 1 -- robot id +local id_max function MSG.open( ... ) @@ -27,9 +25,11 @@ end function MSG.close(fd) if fd2serv[fd] and not fd2serv[fd].closing then fd2serv[fd].closing = true + print(fd2serv[fd].agent) skynet.call(fd2serv[fd].agent, "lua", "exit") - log(string_format("logout %s", fd2serv[fd].id)) + pcall(skynet.send, poold, "lua", "feed") + log(string_format("logout %s", fd2serv[fd].id)) id2fd[fd2serv[fd].id] = nil fd2serv[fd] = nil end @@ -78,7 +78,8 @@ skynet.register_protocol { } local function add_robot() - local robot = skynet.newservice("robot") + local time = skynet.time() + local robot = factory:pop() local fd = socketdriver.connect(config.host, config.port) socketdriver.start(fd) local id @@ -97,7 +98,6 @@ local function add_robot() id2fd[id] = fd pcall(skynet.call, robot, "lua", "start", fd, id, prefix) log(string_format("login %s", fd2serv[fd].id)) - -- 定时下线 skynet.timeout(math.randomInt(config.online_time[1], config.online_time[2]) * 100, function() MSG.close(fd) @@ -122,9 +122,13 @@ local function online() skynet.timeout(100, online) end -local function start() +local function start(pooldp, pooldObj, onlineCountp, startIdp, endIdp, inprep) log("start testing ...") + factory = deque.clone(pooldObj) + poold, onlineCount, startId, endId, inpre = pooldp, onlineCountp, startIdp, endIdp, inprep + id_max = startId - 1 + for i = 1, onlineCount, inpre do for j = 1, inpre do add_robot() @@ -137,5 +141,10 @@ local function start() end skynet.start(function() - skynet.fork(start) + skynet.dispatch("lua", function (_, _, command, ...) + if command == "start" then + start(...) + return + end + end) end) \ No newline at end of file -- libgit2 0.21.2