Commit cbfd05138190544c646e5ac28cae90a6e5e8ab1a
1 parent
3a646dea
增加池子
Showing
4 changed files
with
73 additions
and
13 deletions
Show diff stats
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 | ... | ... |
robot/robot_main.lua
| ... | ... | @@ -9,8 +9,12 @@ skynet.start(function() |
| 9 | 9 | local inpre = math.ceil(config.inpre / need) |
| 10 | 10 | local idRange = math.ceil(config.max / need) |
| 11 | 11 | local curId = 1 |
| 12 | + local poold = skynet.newservice("robot_pool") | |
| 13 | + local pooldObj = skynet.call(poold, "lua", "start", config.online + preOnlineCount) | |
| 14 | + | |
| 12 | 15 | for i = 1, need do |
| 13 | - skynet.newservice("start", preOnlineCount, curId, curId + idRange - 1, inpre) | |
| 16 | + local start = skynet.newservice("start") | |
| 17 | + skynet.send(start, "lua", "start", poold, pooldObj, preOnlineCount, curId, curId + idRange - 1, inpre) | |
| 14 | 18 | curId = curId + idRange |
| 15 | 19 | end |
| 16 | 20 | skynet.exit() | ... | ... |
| ... | ... | @@ -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,19 +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 | |
| 10 | -local onlineCount, startId, endId, inpre = ... | |
| 11 | -onlineCount = tonumber(onlineCount) | |
| 12 | -startId = tonumber(startId) | |
| 13 | -endId = tonumber(endId) | |
| 14 | -inpre = tonumber(inpre) | |
| 11 | +local poold, pooldObj, onlineCount, startId, endId, inpre | |
| 12 | +local factory | |
| 15 | 13 | |
| 16 | 14 | local queue = {} |
| 17 | 15 | local fd2serv = {} |
| 18 | 16 | local id2fd = {} |
| 19 | 17 | local MSG = {} |
| 20 | -local id_max = startId - 1 -- robot id | |
| 18 | +local id_max | |
| 21 | 19 | |
| 22 | 20 | |
| 23 | 21 | function MSG.open( ... ) |
| ... | ... | @@ -27,9 +25,11 @@ end |
| 27 | 25 | function MSG.close(fd) |
| 28 | 26 | if fd2serv[fd] and not fd2serv[fd].closing then |
| 29 | 27 | fd2serv[fd].closing = true |
| 28 | + print(fd2serv[fd].agent) | |
| 30 | 29 | skynet.call(fd2serv[fd].agent, "lua", "exit") |
| 31 | - log(string_format("logout %s", fd2serv[fd].id)) | |
| 30 | + pcall(skynet.send, poold, "lua", "feed") | |
| 32 | 31 | |
| 32 | + log(string_format("logout %s", fd2serv[fd].id)) | |
| 33 | 33 | id2fd[fd2serv[fd].id] = nil |
| 34 | 34 | fd2serv[fd] = nil |
| 35 | 35 | end |
| ... | ... | @@ -78,7 +78,8 @@ skynet.register_protocol { |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | local function add_robot() |
| 81 | - local robot = skynet.newservice("robot") | |
| 81 | + local time = skynet.time() | |
| 82 | + local robot = factory:pop() | |
| 82 | 83 | local fd = socketdriver.connect(config.host, config.port) |
| 83 | 84 | socketdriver.start(fd) |
| 84 | 85 | local id |
| ... | ... | @@ -97,7 +98,6 @@ local function add_robot() |
| 97 | 98 | id2fd[id] = fd |
| 98 | 99 | pcall(skynet.call, robot, "lua", "start", fd, id, prefix) |
| 99 | 100 | log(string_format("login %s", fd2serv[fd].id)) |
| 100 | - | |
| 101 | 101 | -- 定时下线 |
| 102 | 102 | skynet.timeout(math.randomInt(config.online_time[1], config.online_time[2]) * 100, function() |
| 103 | 103 | MSG.close(fd) |
| ... | ... | @@ -122,9 +122,13 @@ local function online() |
| 122 | 122 | skynet.timeout(100, online) |
| 123 | 123 | end |
| 124 | 124 | |
| 125 | -local function start() | |
| 125 | +local function start(pooldp, pooldObj, onlineCountp, startIdp, endIdp, inprep) | |
| 126 | 126 | log("start testing ...") |
| 127 | 127 | |
| 128 | + factory = deque.clone(pooldObj) | |
| 129 | + poold, onlineCount, startId, endId, inpre = pooldp, onlineCountp, startIdp, endIdp, inprep | |
| 130 | + id_max = startId - 1 | |
| 131 | + | |
| 128 | 132 | for i = 1, onlineCount, inpre do |
| 129 | 133 | for j = 1, inpre do |
| 130 | 134 | add_robot() |
| ... | ... | @@ -137,5 +141,10 @@ local function start() |
| 137 | 141 | end |
| 138 | 142 | |
| 139 | 143 | skynet.start(function() |
| 140 | - skynet.fork(start) | |
| 144 | + skynet.dispatch("lua", function (_, _, command, ...) | |
| 145 | + if command == "start" then | |
| 146 | + start(...) | |
| 147 | + return | |
| 148 | + end | |
| 149 | + end) | |
| 141 | 150 | end) |
| 142 | 151 | \ No newline at end of file | ... | ... |