From f32f613ac02eedccc035127828289175aadebd68 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 25 Sep 2014 18:16:24 +0800 Subject: [PATCH] add skynet.harbor.linkmaster --- examples/main_log.lua | 9 ++++++++- lualib/skynet/harbor.lua | 4 ++++ service/cslave.lua | 8 ++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/examples/main_log.lua b/examples/main_log.lua index f74b563f..2a3fbd30 100644 --- a/examples/main_log.lua +++ b/examples/main_log.lua @@ -1,9 +1,16 @@ local skynet = require "skynet" +local harbor = require "skynet.harbor" + +local function monitor_master() + harbor.linkmaster() + print("master is down") + skynet.exit() +end skynet.start(function() print("Log server start") skynet.monitor "simplemonitor" local log = skynet.newservice("globallog") - skynet.exit() + skynet.fork(monitor_master) end) diff --git a/lualib/skynet/harbor.lua b/lualib/skynet/harbor.lua index b5863921..a8873f1d 100644 --- a/lualib/skynet/harbor.lua +++ b/lualib/skynet/harbor.lua @@ -19,4 +19,8 @@ function harbor.connect(id) skynet.call(".cslave", "lua", "CONNECT", id) end +function harbor.linkmaster() + skynet.call(".cslave", "lua", "LINKMASTER") +end + return harbor diff --git a/service/cslave.lua b/service/cslave.lua index c380f371..f06f1452 100644 --- a/service/cslave.lua +++ b/service/cslave.lua @@ -9,6 +9,7 @@ local queryname = {} local harbor = {} local harbor_service local monitor = {} +local monitor_master_set = {} local function read_package(fd) local sz = socket.read(fd, 1) @@ -99,6 +100,9 @@ local function monitor_master(master_fd) end else skynet.error("Master disconnect") + for _, v in ipairs(monitor_master_set) do + v(true) + end socket.close(master_fd) break end @@ -183,6 +187,10 @@ function harbor.LINK(fd, id) end end +function harbor.LINKMASTER() + table.insert(monitor_master_set, skynet.response()) +end + function harbor.CONNECT(fd, id) if not slaves[id] then if monitor[id] == nil then