support @name, see #823

This commit is contained in:
Cloud Wu
2018-04-19 14:54:25 +08:00
committed by 云风
parent f4d5bc7aa4
commit 182d7c7dc7
4 changed files with 47 additions and 17 deletions

View File

@@ -2,10 +2,7 @@ local skynet = require "skynet"
local cluster = require "skynet.cluster"
skynet.start(function()
-- query name "sdb" of cluster db.
local sdb = cluster.query("db", "sdb")
print("db.sbd=",sdb)
local proxy = cluster.proxy("db", sdb)
local proxy = cluster.proxy "db@sdb" -- cluster.proxy("db", "@sdb")
local largekey = string.rep("X", 128*1024)
local largevalue = string.rep("R", 100 * 1024)
skynet.call(proxy, "lua", "SET", largekey, largevalue)
@@ -13,9 +10,9 @@ skynet.start(function()
assert(largevalue == v)
skynet.send(proxy, "lua", "PING", "proxy")
print(cluster.call("db", sdb, "GET", "a"))
print(cluster.call("db2", sdb, "GET", "b"))
cluster.send("db2", sdb, "PING", "db2:longstring" .. largevalue)
print(cluster.call("db", "@sdb", "GET", "a"))
print(cluster.call("db2", "@sdb", "GET", "b"))
cluster.send("db2", "@sdb", "PING", "db2:longstring" .. largevalue)
-- test snax service
skynet.timeout(300,function()
@@ -23,7 +20,7 @@ skynet.start(function()
db = false, -- db is down
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)
local pingserver = cluster.snax("db3", "pingserver")
print(pingserver.req.ping "hello")

View File

@@ -553,6 +553,16 @@ lconcat(lua_State *L) {
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
luaopen_skynet_cluster_core(lua_State *L) {
luaL_Reg l[] = {
@@ -563,6 +573,7 @@ luaopen_skynet_cluster_core(lua_State *L) {
{ "unpackresponse", lunpackresponse },
{ "append", lappend },
{ "concat", lconcat },
{ "isname", lisname },
{ NULL, NULL },
};
luaL_checkversion(L);

View File

@@ -11,6 +11,16 @@ fd = tonumber(fd)
local large_request = {}
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)
if padding then
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
local name = skynet.unpack(msg, sz)
skynet.trash(msg, sz)
local addr = register_name[name]
if addr == nil then
addr = skynet.call(clusterd, "lua", "queryname", name)
register_name[name] = addr
end
local addr = register_name["@" .. name]
if addr then
ok = true
msg, sz = skynet.pack(addr)
@@ -48,11 +54,21 @@ local function dispatch_request(_,_,addr, session, msg, sz, padding, is_push)
ok = false
msg = "name not found"
end
elseif is_push then
skynet.rawsend(addr, "lua", msg, sz)
return -- no response
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
if ok then
response = cluster.packresponse(session, true, msg, sz)

View File

@@ -149,6 +149,12 @@ end
local proxy = {}
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
if proxy[fullname] == nil then
proxy[fullname] = skynet.newservice("clusterproxy", node, name)