mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
add cluster.proxy and cluster.ncall
This commit is contained in:
@@ -7,6 +7,8 @@ Dev version
|
|||||||
* Don't check imported function in snax.hotfix
|
* Don't check imported function in snax.hotfix
|
||||||
* snax service add change SERVICE_PATH and add it to package.path
|
* snax service add change SERVICE_PATH and add it to package.path
|
||||||
* skynet.redirect support string address
|
* skynet.redirect support string address
|
||||||
|
* add skynet.harbor.queryname to query globalname
|
||||||
|
* add cluster.proxy and cluster.ncall
|
||||||
|
|
||||||
v0.5.2 (2014-8-11)
|
v0.5.2 (2014-8-11)
|
||||||
-----------
|
-----------
|
||||||
|
|||||||
@@ -2,5 +2,8 @@ local skynet = require "skynet"
|
|||||||
local cluster = require "cluster"
|
local cluster = require "cluster"
|
||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
print(cluster.call("db", "SIMPLEDB", "GET", "a"))
|
local proxy = cluster.proxy("db", ".simpledb")
|
||||||
|
print(skynet.call(proxy, "lua", "GET", "a"))
|
||||||
|
print(cluster.ncall("db.simpledb","GET", "a"))
|
||||||
|
print(cluster.call("db", ".simpledb", "GET", "a"))
|
||||||
end)
|
end)
|
||||||
|
|||||||
@@ -4,5 +4,6 @@ skynet.start(function()
|
|||||||
skynet.dispatch("lua", function(session, address, ...)
|
skynet.dispatch("lua", function(session, address, ...)
|
||||||
print("[GLOBALLOG]", skynet.address(address), ...)
|
print("[GLOBALLOG]", skynet.address(address), ...)
|
||||||
end)
|
end)
|
||||||
|
skynet.register ".log"
|
||||||
skynet.register "LOG"
|
skynet.register "LOG"
|
||||||
end)
|
end)
|
||||||
|
|||||||
@@ -22,5 +22,6 @@ skynet.start(function()
|
|||||||
error(string.format("Unknown command %s", tostring(cmd)))
|
error(string.format("Unknown command %s", tostring(cmd)))
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
skynet.register ".simpledb"
|
||||||
skynet.register "SIMPLEDB"
|
skynet.register "SIMPLEDB"
|
||||||
end)
|
end)
|
||||||
|
|||||||
@@ -73,10 +73,17 @@ _cb(struct skynet_context * context, void * ud, int type, int session, uint32_t
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
forward_cb(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) {
|
||||||
|
_cb(context, ud, type, session, source, msg, sz);
|
||||||
|
// don't delete msg in forward mode.
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_callback(lua_State *L) {
|
_callback(lua_State *L) {
|
||||||
struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
|
struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
|
||||||
|
int forward = lua_toboolean(L, 2);
|
||||||
luaL_checktype(L,1,LUA_TFUNCTION);
|
luaL_checktype(L,1,LUA_TFUNCTION);
|
||||||
lua_settop(L,1);
|
lua_settop(L,1);
|
||||||
lua_rawsetp(L, LUA_REGISTRYINDEX, _cb);
|
lua_rawsetp(L, LUA_REGISTRYINDEX, _cb);
|
||||||
@@ -84,7 +91,11 @@ _callback(lua_State *L) {
|
|||||||
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD);
|
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD);
|
||||||
lua_State *gL = lua_tothread(L,-1);
|
lua_State *gL = lua_tothread(L,-1);
|
||||||
|
|
||||||
|
if (forward) {
|
||||||
|
skynet_callback(context, gL, forward_cb);
|
||||||
|
} else {
|
||||||
skynet_callback(context, gL, _cb);
|
skynet_callback(context, gL, _cb);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ local clusterd
|
|||||||
local cluster = {}
|
local cluster = {}
|
||||||
|
|
||||||
function cluster.call(node, address, ...)
|
function cluster.call(node, address, ...)
|
||||||
-- skynet.pack(...) will free by cluster.c.packrequest
|
-- skynet.pack(...) will free by cluster.core.packrequest
|
||||||
return skynet.call(clusterd, "lua", "req", node, address, skynet.pack(...))
|
return skynet.call(clusterd, "lua", "req", node, address, skynet.pack(...))
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -20,6 +20,23 @@ function cluster.reload()
|
|||||||
skynet.call(clusterd, "lua", "reload")
|
skynet.call(clusterd, "lua", "reload")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function cluster.proxy(node, name)
|
||||||
|
return skynet.call(clusterd, "lua", "proxy", node, name)
|
||||||
|
end
|
||||||
|
|
||||||
|
local namecache = {}
|
||||||
|
|
||||||
|
function cluster.ncall(name, ...)
|
||||||
|
local s = namecache[name]
|
||||||
|
if not s then
|
||||||
|
local node , lname = name:match "(.-)(%..+)"
|
||||||
|
assert(node and lname)
|
||||||
|
s = cluster.proxy(node, lname)
|
||||||
|
namecache[name] = assert(s)
|
||||||
|
end
|
||||||
|
return skynet.call(s, "lua", ...)
|
||||||
|
end
|
||||||
|
|
||||||
skynet.init(function()
|
skynet.init(function()
|
||||||
clusterd = skynet.uniqueservice("clusterd")
|
clusterd = skynet.uniqueservice("clusterd")
|
||||||
end)
|
end)
|
||||||
|
|||||||
@@ -99,6 +99,7 @@ local coroutine_yield = coroutine.yield
|
|||||||
local function co_create(f)
|
local function co_create(f)
|
||||||
local co = table.remove(coroutine_pool)
|
local co = table.remove(coroutine_pool)
|
||||||
if co == nil then
|
if co == nil then
|
||||||
|
local print = print
|
||||||
co = coroutine.create(function(...)
|
co = coroutine.create(function(...)
|
||||||
f(...)
|
f(...)
|
||||||
while true do
|
while true do
|
||||||
@@ -548,7 +549,7 @@ end
|
|||||||
|
|
||||||
function skynet.address(addr)
|
function skynet.address(addr)
|
||||||
if type(addr) == "number" then
|
if type(addr) == "number" then
|
||||||
return string.format(":%x",addr)
|
return string.format(":%08x",addr)
|
||||||
else
|
else
|
||||||
return tostring(addr)
|
return tostring(addr)
|
||||||
end
|
end
|
||||||
@@ -648,6 +649,21 @@ function skynet.filter(f ,start_func)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function skynet.forward_type(map, start_func)
|
||||||
|
c.callback(function(ptype, msg, sz, ...)
|
||||||
|
local prototype = map[ptype]
|
||||||
|
if prototype then
|
||||||
|
dispatch_message(prototype, msg, sz, ...)
|
||||||
|
else
|
||||||
|
dispatch_message(ptype, msg, sz, ...)
|
||||||
|
c.trash(msg, sz)
|
||||||
|
end
|
||||||
|
end, true)
|
||||||
|
skynet.timeout(0, function()
|
||||||
|
init_service(start_func)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
function skynet.endless()
|
function skynet.endless()
|
||||||
return c.command("ENDLESS")~=nil
|
return c.command("ENDLESS")~=nil
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -36,7 +36,11 @@ function harbor.REGISTER(name, handle)
|
|||||||
skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name)
|
skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name)
|
||||||
end
|
end
|
||||||
|
|
||||||
function harbor.QUERYNAME(fd, name)
|
function harbor.QUERYNAME(name)
|
||||||
|
if name:byte() == 46 then -- "." , local name
|
||||||
|
skynet.ret(skynet.pack(skynet.localname(name)))
|
||||||
|
return
|
||||||
|
end
|
||||||
local result = globalname[name]
|
local result = globalname[name]
|
||||||
if result then
|
if result then
|
||||||
skynet.ret(skynet.pack(result))
|
skynet.ret(skynet.pack(result))
|
||||||
|
|||||||
@@ -60,6 +60,16 @@ function command.req(source, node, addr, msg, sz)
|
|||||||
skynet.ret(c:request(request, session))
|
skynet.ret(c:request(request, session))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local proxy = {}
|
||||||
|
|
||||||
|
function command.proxy(source, node, name)
|
||||||
|
local fullname = node .. "." .. name
|
||||||
|
if proxy[fullname] == nil then
|
||||||
|
proxy[fullname] = skynet.newservice("clusterproxy", node, name)
|
||||||
|
end
|
||||||
|
skynet.ret(skynet.pack(proxy[fullname]))
|
||||||
|
end
|
||||||
|
|
||||||
local request_fd = {}
|
local request_fd = {}
|
||||||
|
|
||||||
function command.socket(source, subcmd, fd, msg)
|
function command.socket(source, subcmd, fd, msg)
|
||||||
|
|||||||
@@ -93,6 +93,7 @@ local function monitor_master(master_fd)
|
|||||||
local fd = slaves[id_name]
|
local fd = slaves[id_name]
|
||||||
slaves[id_name] = false
|
slaves[id_name] = false
|
||||||
if fd then
|
if fd then
|
||||||
|
monitor_clear(id_name)
|
||||||
socket.close(fd)
|
socket.close(fd)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -143,14 +144,14 @@ local function monitor_harbor(master_fd)
|
|||||||
return function(session, source, command)
|
return function(session, source, command)
|
||||||
local t = string.sub(command, 1, 1)
|
local t = string.sub(command, 1, 1)
|
||||||
local arg = string.sub(command, 3)
|
local arg = string.sub(command, 3)
|
||||||
if t == "Q" then
|
if t == 'Q' then
|
||||||
-- query name
|
-- query name
|
||||||
if globalname[arg] then
|
if globalname[arg] then
|
||||||
skynet.redirect(harbor_service, globalname[arg], "harbor", 0, "N " .. arg)
|
skynet.redirect(harbor_service, globalname[arg], "harbor", 0, "N " .. arg)
|
||||||
else
|
else
|
||||||
socket.write(master_fd, pack_package("Q", arg))
|
socket.write(master_fd, pack_package("Q", arg))
|
||||||
end
|
end
|
||||||
elseif t == "D" then
|
elseif t == 'D' then
|
||||||
-- harbor down
|
-- harbor down
|
||||||
local id = tonumber(arg)
|
local id = tonumber(arg)
|
||||||
if slaves[id] then
|
if slaves[id] then
|
||||||
@@ -194,6 +195,10 @@ function harbor.CONNECT(fd, id)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function harbor.QUERYNAME(fd, name)
|
function harbor.QUERYNAME(fd, name)
|
||||||
|
if name:byte() == 46 then -- "." , local name
|
||||||
|
skynet.ret(skynet.pack(skynet.localname(name)))
|
||||||
|
return
|
||||||
|
end
|
||||||
local result = globalname[name]
|
local result = globalname[name]
|
||||||
if result then
|
if result then
|
||||||
skynet.ret(skynet.pack(result))
|
skynet.ret(skynet.pack(result))
|
||||||
|
|||||||
Reference in New Issue
Block a user