mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 12:20:41 +00:00
optimize global unique service query
This commit is contained in:
@@ -410,26 +410,12 @@ function skynet.newservice(name, ...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function skynet.uniqueservice(global, ...)
|
function skynet.uniqueservice(...)
|
||||||
local handle
|
return assert(skynet.call(".service", "lua", "LAUNCH", ...))
|
||||||
if global == true then
|
|
||||||
handle = skynet.call("SERVICE", "lua", "LAUNCH", ...)
|
|
||||||
else
|
|
||||||
handle = skynet.call(".service", "lua", "LAUNCH", global, ...)
|
|
||||||
end
|
|
||||||
assert(handle , "Unique service launch failed")
|
|
||||||
return handle
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function skynet.queryservice(global, ...)
|
function skynet.queryservice(global, name)
|
||||||
local handle
|
return assert(skynet.call(".service", "lua", "QUERY", global, name))
|
||||||
if global == true then
|
|
||||||
handle = skynet.call("SERVICE", "lua", "QUERY", ...)
|
|
||||||
else
|
|
||||||
handle = skynet.call(".service", "lua", "QUERY", global, ...)
|
|
||||||
end
|
|
||||||
assert(handle , "Unique service query failed")
|
|
||||||
return handle
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function group_command(cmd, handle, address)
|
local function group_command(cmd, handle, address)
|
||||||
|
|||||||
@@ -3,53 +3,91 @@ local skynet = require "skynet"
|
|||||||
local cmd = {}
|
local cmd = {}
|
||||||
local service = {}
|
local service = {}
|
||||||
|
|
||||||
function cmd.LAUNCH(service_name, ...)
|
local GLOBAL = false
|
||||||
local s = service[service_name]
|
|
||||||
if type(s) == "number" then
|
local function request(name, func, ...)
|
||||||
return s
|
local ok, handle = pcall(func, ...)
|
||||||
|
local s = service[name]
|
||||||
|
assert(type(s) == "table")
|
||||||
|
if ok then
|
||||||
|
service[name] = handle
|
||||||
|
else
|
||||||
|
service[name] = tostring(handle)
|
||||||
end
|
end
|
||||||
|
|
||||||
if s == nil then
|
|
||||||
s = { launch = true }
|
|
||||||
service[service_name] = s
|
|
||||||
elseif s.launch then
|
|
||||||
assert(type(s) == "table")
|
|
||||||
local co = coroutine.running()
|
|
||||||
table.insert(s, co)
|
|
||||||
skynet.wait()
|
|
||||||
s = service[service_name]
|
|
||||||
assert(type(s) == "number")
|
|
||||||
return s
|
|
||||||
end
|
|
||||||
|
|
||||||
local handle = skynet.newservice(service_name, ...)
|
|
||||||
for _,v in ipairs(s) do
|
for _,v in ipairs(s) do
|
||||||
skynet.wakeup(v)
|
skynet.wakeup(v)
|
||||||
end
|
end
|
||||||
|
|
||||||
service[service_name] = handle
|
if ok then
|
||||||
|
return handle
|
||||||
return handle
|
else
|
||||||
|
error(tostring(handle))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function cmd.QUERY(service_name)
|
local function waitfor(name , func, ...)
|
||||||
local s = service[service_name]
|
local s = service[name]
|
||||||
if type(s) == "number" then
|
if type(s) == "number" then
|
||||||
return s
|
return s
|
||||||
end
|
end
|
||||||
|
local co = coroutine.running()
|
||||||
|
|
||||||
if s == nil then
|
if s == nil then
|
||||||
s = {}
|
s = {}
|
||||||
service[service_name] = s
|
service[name] = s
|
||||||
|
elseif type(s) == "string" then
|
||||||
|
error(s)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(type(s) == "table")
|
assert(type(s) == "table")
|
||||||
local co = coroutine.running()
|
|
||||||
|
if not s.launch and func then
|
||||||
|
s.launch = true
|
||||||
|
return request(name, func, ...)
|
||||||
|
end
|
||||||
|
|
||||||
table.insert(s, co)
|
table.insert(s, co)
|
||||||
skynet.wait()
|
skynet.wait()
|
||||||
s = service[service_name]
|
s = service[name]
|
||||||
|
if type(s) == "string" then
|
||||||
|
error(s)
|
||||||
|
end
|
||||||
assert(type(s) == "number")
|
assert(type(s) == "number")
|
||||||
return s
|
return s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function GQUERY(service_name)
|
||||||
|
if GLOBAL then
|
||||||
|
return cmd.QUERY(service_name)
|
||||||
|
else
|
||||||
|
return waitfor(service_name, skynet.call, "SERVICE", "lua", "QUERY", service_name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function GLAUNCH(service_name, ...)
|
||||||
|
if GLOBAL then
|
||||||
|
return cmd.LAUNCH(service_name, ...)
|
||||||
|
else
|
||||||
|
return waitfor(service_name, skynet.call, "SERVICE", "lua", "LAUNCH", service_name, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function cmd.LAUNCH(global, service_name, ...)
|
||||||
|
if global == true then
|
||||||
|
return GLAUNCH(service_name, ...)
|
||||||
|
else
|
||||||
|
return waitfor(global, skynet.newservice, global, service_name, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function cmd.QUERY(global, service_name)
|
||||||
|
if global == true then
|
||||||
|
return GQUERY(service_name)
|
||||||
|
end
|
||||||
|
return waitfor(global)
|
||||||
|
end
|
||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
skynet.dispatch("lua", function(session, address, command, service_name , ...)
|
skynet.dispatch("lua", function(session, address, command, service_name , ...)
|
||||||
local f = cmd[command]
|
local f = cmd[command]
|
||||||
@@ -62,11 +100,12 @@ skynet.start(function()
|
|||||||
if ok then
|
if ok then
|
||||||
skynet.ret(skynet.pack(r))
|
skynet.ret(skynet.pack(r))
|
||||||
else
|
else
|
||||||
skynet.ret(skynet.pack(nil))
|
skynet.ret(skynet.pack(nil, r))
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
skynet.register(".service")
|
skynet.register(".service")
|
||||||
if skynet.getenv "standalone" then
|
if skynet.getenv "standalone" then
|
||||||
|
GLOBAL = true
|
||||||
skynet.register("SERVICE")
|
skynet.register("SERVICE")
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|||||||
Reference in New Issue
Block a user