try connect only once at first time

This commit is contained in:
Cloud Wu
2014-04-22 16:18:49 +08:00
parent 08c511160f
commit b6c208cf51
4 changed files with 14 additions and 13 deletions

View File

@@ -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")

View File

@@ -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

View File

@@ -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

View File

@@ -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)