mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
support @name, see #823
This commit is contained in:
@@ -2,10 +2,7 @@ local skynet = require "skynet"
|
|||||||
local cluster = require "skynet.cluster"
|
local cluster = require "skynet.cluster"
|
||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
-- query name "sdb" of cluster db.
|
local proxy = cluster.proxy "db@sdb" -- cluster.proxy("db", "@sdb")
|
||||||
local sdb = cluster.query("db", "sdb")
|
|
||||||
print("db.sbd=",sdb)
|
|
||||||
local proxy = cluster.proxy("db", sdb)
|
|
||||||
local largekey = string.rep("X", 128*1024)
|
local largekey = string.rep("X", 128*1024)
|
||||||
local largevalue = string.rep("R", 100 * 1024)
|
local largevalue = string.rep("R", 100 * 1024)
|
||||||
skynet.call(proxy, "lua", "SET", largekey, largevalue)
|
skynet.call(proxy, "lua", "SET", largekey, largevalue)
|
||||||
@@ -13,9 +10,9 @@ skynet.start(function()
|
|||||||
assert(largevalue == v)
|
assert(largevalue == v)
|
||||||
skynet.send(proxy, "lua", "PING", "proxy")
|
skynet.send(proxy, "lua", "PING", "proxy")
|
||||||
|
|
||||||
print(cluster.call("db", sdb, "GET", "a"))
|
print(cluster.call("db", "@sdb", "GET", "a"))
|
||||||
print(cluster.call("db2", sdb, "GET", "b"))
|
print(cluster.call("db2", "@sdb", "GET", "b"))
|
||||||
cluster.send("db2", sdb, "PING", "db2:longstring" .. largevalue)
|
cluster.send("db2", "@sdb", "PING", "db2:longstring" .. largevalue)
|
||||||
|
|
||||||
-- test snax service
|
-- test snax service
|
||||||
skynet.timeout(300,function()
|
skynet.timeout(300,function()
|
||||||
@@ -23,7 +20,7 @@ skynet.start(function()
|
|||||||
db = false, -- db is down
|
db = false, -- db is down
|
||||||
db3 = "127.0.0.1:2529"
|
db3 = "127.0.0.1:2529"
|
||||||
}
|
}
|
||||||
print(pcall(cluster.call, "db", sdb, "GET", "a")) -- db is down
|
print(pcall(cluster.call, "db", "@sdb", "GET", "a")) -- db is down
|
||||||
end)
|
end)
|
||||||
local pingserver = cluster.snax("db3", "pingserver")
|
local pingserver = cluster.snax("db3", "pingserver")
|
||||||
print(pingserver.req.ping "hello")
|
print(pingserver.req.ping "hello")
|
||||||
|
|||||||
@@ -553,6 +553,16 @@ lconcat(lua_State *L) {
|
|||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
lisname(lua_State *L) {
|
||||||
|
const char * name = lua_tostring(L, 1);
|
||||||
|
if (name && name[0] == '@') {
|
||||||
|
lua_pushboolean(L, 1);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
LUAMOD_API int
|
LUAMOD_API int
|
||||||
luaopen_skynet_cluster_core(lua_State *L) {
|
luaopen_skynet_cluster_core(lua_State *L) {
|
||||||
luaL_Reg l[] = {
|
luaL_Reg l[] = {
|
||||||
@@ -563,6 +573,7 @@ luaopen_skynet_cluster_core(lua_State *L) {
|
|||||||
{ "unpackresponse", lunpackresponse },
|
{ "unpackresponse", lunpackresponse },
|
||||||
{ "append", lappend },
|
{ "append", lappend },
|
||||||
{ "concat", lconcat },
|
{ "concat", lconcat },
|
||||||
|
{ "isname", lisname },
|
||||||
{ NULL, NULL },
|
{ NULL, NULL },
|
||||||
};
|
};
|
||||||
luaL_checkversion(L);
|
luaL_checkversion(L);
|
||||||
|
|||||||
@@ -11,6 +11,16 @@ fd = tonumber(fd)
|
|||||||
local large_request = {}
|
local large_request = {}
|
||||||
local register_name = {}
|
local register_name = {}
|
||||||
|
|
||||||
|
setmetatable(register_name, { __index =
|
||||||
|
function(self, name)
|
||||||
|
local addr = skynet.call(clusterd, "lua", "queryname", name:sub(2)) -- name must be '@xxxx'
|
||||||
|
if addr then
|
||||||
|
self[name] = addr
|
||||||
|
end
|
||||||
|
return addr
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
local function dispatch_request(_,_,addr, session, msg, sz, padding, is_push)
|
local function dispatch_request(_,_,addr, session, msg, sz, padding, is_push)
|
||||||
if padding then
|
if padding then
|
||||||
local req = large_request[session] or { addr = addr , is_push = is_push }
|
local req = large_request[session] or { addr = addr , is_push = is_push }
|
||||||
@@ -36,11 +46,7 @@ local function dispatch_request(_,_,addr, session, msg, sz, padding, is_push)
|
|||||||
if addr == 0 then
|
if addr == 0 then
|
||||||
local name = skynet.unpack(msg, sz)
|
local name = skynet.unpack(msg, sz)
|
||||||
skynet.trash(msg, sz)
|
skynet.trash(msg, sz)
|
||||||
local addr = register_name[name]
|
local addr = register_name["@" .. name]
|
||||||
if addr == nil then
|
|
||||||
addr = skynet.call(clusterd, "lua", "queryname", name)
|
|
||||||
register_name[name] = addr
|
|
||||||
end
|
|
||||||
if addr then
|
if addr then
|
||||||
ok = true
|
ok = true
|
||||||
msg, sz = skynet.pack(addr)
|
msg, sz = skynet.pack(addr)
|
||||||
@@ -48,11 +54,21 @@ local function dispatch_request(_,_,addr, session, msg, sz, padding, is_push)
|
|||||||
ok = false
|
ok = false
|
||||||
msg = "name not found"
|
msg = "name not found"
|
||||||
end
|
end
|
||||||
elseif is_push then
|
|
||||||
skynet.rawsend(addr, "lua", msg, sz)
|
|
||||||
return -- no response
|
|
||||||
else
|
else
|
||||||
ok , msg, sz = pcall(skynet.rawcall, addr, "lua", msg, sz)
|
if cluster.isname(addr) then
|
||||||
|
addr = register_name[addr]
|
||||||
|
end
|
||||||
|
if addr then
|
||||||
|
if is_push then
|
||||||
|
skynet.rawsend(addr, "lua", msg, sz)
|
||||||
|
return -- no response
|
||||||
|
else
|
||||||
|
ok , msg, sz = pcall(skynet.rawcall, addr, "lua", msg, sz)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
ok = false
|
||||||
|
msg = "Invalid name"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
if ok then
|
if ok then
|
||||||
response = cluster.packresponse(session, true, msg, sz)
|
response = cluster.packresponse(session, true, msg, sz)
|
||||||
|
|||||||
@@ -149,6 +149,12 @@ end
|
|||||||
local proxy = {}
|
local proxy = {}
|
||||||
|
|
||||||
function command.proxy(source, node, name)
|
function command.proxy(source, node, name)
|
||||||
|
if name == nil then
|
||||||
|
node, name = node:match "^([^@.]+)([@.].+)"
|
||||||
|
if name == nil then
|
||||||
|
error ("Invalid name " .. tostring(node))
|
||||||
|
end
|
||||||
|
end
|
||||||
local fullname = node .. "." .. name
|
local fullname = node .. "." .. name
|
||||||
if proxy[fullname] == nil then
|
if proxy[fullname] == nil then
|
||||||
proxy[fullname] = skynet.newservice("clusterproxy", node, name)
|
proxy[fullname] = skynet.newservice("clusterproxy", node, name)
|
||||||
|
|||||||
Reference in New Issue
Block a user