mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-23 19:43:09 +00:00
try connect only once at first time
This commit is contained in:
@@ -6,7 +6,6 @@ skynet.start(function()
|
||||
print("Server start")
|
||||
local service = skynet.newservice("service_mgr")
|
||||
skynet.monitor "simplemonitor"
|
||||
local lualog = skynet.newservice("lualog")
|
||||
local console = skynet.newservice("console")
|
||||
-- skynet.newservice("debug_console",8000)
|
||||
skynet.newservice("simpledb")
|
||||
|
||||
@@ -103,7 +103,7 @@ function mongo.client( conf )
|
||||
auth = mongo_auth(conf),
|
||||
}
|
||||
setmetatable(obj, client_meta)
|
||||
obj.__sock:connect()
|
||||
obj.__sock:connect(true) -- try connect only once
|
||||
return obj
|
||||
end
|
||||
|
||||
|
||||
@@ -86,8 +86,8 @@ function redis.connect(db_conf)
|
||||
port = db_conf.port or 6379,
|
||||
auth = redis_login(db_conf.auth, db_conf.db),
|
||||
}
|
||||
-- try connect first
|
||||
channel:connect()
|
||||
-- try connect first only once
|
||||
channel:connect(true)
|
||||
return setmetatable( { channel }, meta )
|
||||
end
|
||||
|
||||
@@ -178,8 +178,8 @@ function redis.watch(db_conf)
|
||||
}
|
||||
obj.__sock = channel
|
||||
|
||||
-- try connect first
|
||||
channel:connect()
|
||||
-- try connect first only once
|
||||
channel:connect(true)
|
||||
return setmetatable( obj, watchmeta )
|
||||
end
|
||||
|
||||
|
||||
@@ -146,11 +146,13 @@ local function connect_once(self)
|
||||
return true
|
||||
end
|
||||
|
||||
local function try_connect(self)
|
||||
local function try_connect(self , once)
|
||||
local t = 100
|
||||
while not self.__closed do
|
||||
if connect_once(self) then
|
||||
return
|
||||
elseif once then
|
||||
error(string.format("Connect to %s:%d failed", self.__host, self.__port))
|
||||
end
|
||||
if t > 1000 then
|
||||
print("socket: try to reconnect", self.__host, self.__port)
|
||||
@@ -163,7 +165,7 @@ local function try_connect(self)
|
||||
end
|
||||
end
|
||||
|
||||
local function block_connect(self)
|
||||
local function block_connect(self, once)
|
||||
if self.__sock then
|
||||
local authco = self.__authcoroutine
|
||||
if not authco then
|
||||
@@ -183,11 +185,11 @@ local function block_connect(self)
|
||||
table.insert(self.__connecting, co)
|
||||
skynet.wait()
|
||||
-- check connection again
|
||||
return block_connect(self)
|
||||
return block_connect(self, once)
|
||||
end
|
||||
|
||||
self.__connecting[1] = true
|
||||
try_connect(self)
|
||||
try_connect(self, once)
|
||||
self.__connecting[1] = nil
|
||||
for i=2, #self.__connecting do
|
||||
local co = self.__connecting[i]
|
||||
@@ -196,15 +198,15 @@ local function block_connect(self)
|
||||
end
|
||||
|
||||
-- check again
|
||||
return block_connect(self)
|
||||
return block_connect(self, once)
|
||||
end
|
||||
|
||||
function channel:connect()
|
||||
function channel:connect(once)
|
||||
if self.__closed then
|
||||
self.__closed = false
|
||||
end
|
||||
|
||||
return block_connect(self)
|
||||
return block_connect(self, once)
|
||||
end
|
||||
|
||||
function channel:request(request, response)
|
||||
|
||||
Reference in New Issue
Block a user