diff --git a/examples/cluster2.lua b/examples/cluster2.lua index 69f67438..3b7a2c02 100644 --- a/examples/cluster2.lua +++ b/examples/cluster2.lua @@ -20,8 +20,10 @@ skynet.start(function() -- test snax service skynet.timeout(300,function() cluster.reload { + db = false, -- db is down db3 = "127.0.0.1:2529" } + print(pcall(cluster.call, "db", sdb, "GET", "a")) -- db is down end) local pingserver = cluster.snax("db3", "pingserver") print(pingserver.req.ping "hello") diff --git a/service/clusterd.lua b/service/clusterd.lua index 8d971e9e..3196820a 100644 --- a/service/clusterd.lua +++ b/service/clusterd.lua @@ -33,19 +33,25 @@ local function open_channel(t, key) ct.namequery = co skynet.error("Wating for cluster node [".. key.."]") skynet.wait(co) - address = assert(node_address[key]) + address = node_address[key] + assert(address ~= nil) end - local host, port = string.match(address, "([^:]+):(.*)$") - local c = sc.channel { - host = host, - port = tonumber(port), - response = read_response, - nodelay = true, - } - local succ, err = pcall(c.connect, c, true) - if succ then - t[key] = c - ct.channel = c + local succ, err, c + if address then + local host, port = string.match(address, "([^:]+):(.*)$") + c = sc.channel { + host = host, + port = tonumber(port), + response = read_response, + nodelay = true, + } + succ, err = pcall(c.connect, c, true) + if succ then + t[key] = c + ct.channel = c + end + else + err = "cluster node [" .. key .. "] is down." end connecting[key] = nil for _, co in ipairs(ct) do @@ -68,7 +74,7 @@ local function loadconfig(tmp) end end for name,address in pairs(tmp) do - assert(type(address) == "string") + assert(address == false or type(address) == "string") if node_address[name] ~= address then -- address changed if rawget(node_channel, name) then @@ -92,7 +98,8 @@ end function command.listen(source, addr, port) local gate = skynet.newservice("gate") if port == nil then - addr, port = string.match(node_address[addr], "([^:]+):(.*)$") + local address = assert(node_address[addr], addr .. " is down") + addr, port = string.match(address, "([^:]+):(.*)$") end skynet.call(gate, "lua", "open", { address = addr, port = port }) skynet.ret(skynet.pack(nil))