mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 12:43:09 +00:00
add wait queue
This commit is contained in:
@@ -8,6 +8,7 @@ local channel
|
|||||||
local session = 1
|
local session = 1
|
||||||
|
|
||||||
local command = {}
|
local command = {}
|
||||||
|
local waiting = {}
|
||||||
|
|
||||||
local function send_request(addr, msg, sz)
|
local function send_request(addr, msg, sz)
|
||||||
-- msg is a local pointer, cluster.packrequest will free it
|
-- msg is a local pointer, cluster.packrequest will free it
|
||||||
@@ -30,7 +31,16 @@ local function send_request(addr, msg, sz)
|
|||||||
return channel:request(request, current_session, padding)
|
return channel:request(request, current_session, padding)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function wait()
|
||||||
|
local co = coroutine.running()
|
||||||
|
table.insert(waiting, co)
|
||||||
|
skynet.wait(co)
|
||||||
|
end
|
||||||
|
|
||||||
function command.req(...)
|
function command.req(...)
|
||||||
|
if channel == nil then
|
||||||
|
wait()
|
||||||
|
end
|
||||||
local ok, msg = pcall(send_request, ...)
|
local ok, msg = pcall(send_request, ...)
|
||||||
if ok then
|
if ok then
|
||||||
if type(msg) == "table" then
|
if type(msg) == "table" then
|
||||||
@@ -45,6 +55,9 @@ function command.req(...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function command.push(addr, msg, sz)
|
function command.push(addr, msg, sz)
|
||||||
|
if channel == nil then
|
||||||
|
wait()
|
||||||
|
end
|
||||||
local request, new_session, padding = cluster.packpush(addr, session, msg, sz)
|
local request, new_session, padding = cluster.packpush(addr, session, msg, sz)
|
||||||
if padding then -- is multi push
|
if padding then -- is multi push
|
||||||
session = new_session
|
session = new_session
|
||||||
@@ -68,10 +81,18 @@ function command.changenode(host, port)
|
|||||||
nodelay = true,
|
nodelay = true,
|
||||||
}
|
}
|
||||||
succ, err = pcall(c.connect, c, true)
|
succ, err = pcall(c.connect, c, true)
|
||||||
|
if channel then
|
||||||
|
channel:close()
|
||||||
|
end
|
||||||
if succ then
|
if succ then
|
||||||
channel = c
|
channel = c
|
||||||
|
for k, co in ipairs(waiting) do
|
||||||
|
waiting[k] = nil
|
||||||
|
skynet.wakeup(co)
|
||||||
|
end
|
||||||
skynet.ret(skynet.pack(nil))
|
skynet.ret(skynet.pack(nil))
|
||||||
else
|
else
|
||||||
|
channel = nil -- reset channel
|
||||||
skynet.response()(false)
|
skynet.response()(false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user