mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
add socketchannel backup host
This commit is contained in:
@@ -26,6 +26,7 @@ function socket_channel.channel(desc)
|
|||||||
local c = {
|
local c = {
|
||||||
__host = assert(desc.host),
|
__host = assert(desc.host),
|
||||||
__port = assert(desc.port),
|
__port = assert(desc.port),
|
||||||
|
__backup = desc.backup,
|
||||||
__auth = desc.auth,
|
__auth = desc.auth,
|
||||||
__response = desc.response, -- It's for session mode
|
__response = desc.response, -- It's for session mode
|
||||||
__request = {}, -- request seq { response func or session } -- It's for order mode
|
__request = {}, -- request seq { response func or session } -- It's for order mode
|
||||||
@@ -152,30 +153,51 @@ local function dispatch_function(self)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function connect_backup(self)
|
||||||
|
if self.__backup then
|
||||||
|
for _, host in ipairs(self.__backup) do
|
||||||
|
skynet.error("socket: connect to backup host", host, self.__port)
|
||||||
|
local fd = socket.open(host, self.__port)
|
||||||
|
if fd then
|
||||||
|
self.__host = host
|
||||||
|
return fd
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function connect_once(self)
|
local function connect_once(self)
|
||||||
assert(not self.__sock and not self.__authcoroutine)
|
assert(not self.__sock and not self.__authcoroutine)
|
||||||
local fd = socket.open(self.__host, self.__port)
|
local fd = socket.open(self.__host, self.__port)
|
||||||
if not fd then
|
if not fd then
|
||||||
return false
|
fd = connect_backup(self)
|
||||||
end
|
if not fd then
|
||||||
self.__authcoroutine = coroutine.running()
|
return false
|
||||||
self.__sock = setmetatable( {fd} , channel_socket_meta )
|
|
||||||
skynet.fork(dispatch_function(self), self)
|
|
||||||
|
|
||||||
if self.__auth then
|
|
||||||
local ok , message = pcall(self.__auth, self)
|
|
||||||
if not ok then
|
|
||||||
close_channel_socket(self)
|
|
||||||
if message ~= socket_error then
|
|
||||||
skynet.error("socket: auth failed", message)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
self.__authcoroutine = false
|
|
||||||
return ok
|
|
||||||
end
|
end
|
||||||
|
while not self.__closed do
|
||||||
|
self.__authcoroutine = coroutine.running()
|
||||||
|
self.__sock = setmetatable( {fd} , channel_socket_meta )
|
||||||
|
skynet.fork(dispatch_function(self), self)
|
||||||
|
|
||||||
self.__authcoroutine = false
|
if self.__auth then
|
||||||
return self.__sock ~= nil
|
local ok , message = pcall(self.__auth, self)
|
||||||
|
if not ok then
|
||||||
|
close_channel_socket(self)
|
||||||
|
if message ~= socket_error then
|
||||||
|
skynet.error("socket: auth failed", message)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
self.__authcoroutine = false
|
||||||
|
return ok
|
||||||
|
end
|
||||||
|
|
||||||
|
self.__authcoroutine = false
|
||||||
|
if self.__sock then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
-- auth may change host, so connect again
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function try_connect(self , once)
|
local function try_connect(self , once)
|
||||||
@@ -302,6 +324,10 @@ function channel:changehost(host, port)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function channel:changebackup(backup)
|
||||||
|
self.__backup = backup
|
||||||
|
end
|
||||||
|
|
||||||
channel_meta.__gc = channel.close
|
channel_meta.__gc = channel.close
|
||||||
|
|
||||||
local function wrapper_socket_function(f)
|
local function wrapper_socket_function(f)
|
||||||
|
|||||||
Reference in New Issue
Block a user