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") print("Server start")
local service = skynet.newservice("service_mgr") local service = skynet.newservice("service_mgr")
skynet.monitor "simplemonitor" skynet.monitor "simplemonitor"
local lualog = skynet.newservice("lualog")
local console = skynet.newservice("console") local console = skynet.newservice("console")
-- skynet.newservice("debug_console",8000) -- skynet.newservice("debug_console",8000)
skynet.newservice("simpledb") skynet.newservice("simpledb")

View File

@@ -103,7 +103,7 @@ function mongo.client( conf )
auth = mongo_auth(conf), auth = mongo_auth(conf),
} }
setmetatable(obj, client_meta) setmetatable(obj, client_meta)
obj.__sock:connect() obj.__sock:connect(true) -- try connect only once
return obj return obj
end end

View File

@@ -86,8 +86,8 @@ function redis.connect(db_conf)
port = db_conf.port or 6379, port = db_conf.port or 6379,
auth = redis_login(db_conf.auth, db_conf.db), auth = redis_login(db_conf.auth, db_conf.db),
} }
-- try connect first -- try connect first only once
channel:connect() channel:connect(true)
return setmetatable( { channel }, meta ) return setmetatable( { channel }, meta )
end end
@@ -178,8 +178,8 @@ function redis.watch(db_conf)
} }
obj.__sock = channel obj.__sock = channel
-- try connect first -- try connect first only once
channel:connect() channel:connect(true)
return setmetatable( obj, watchmeta ) return setmetatable( obj, watchmeta )
end end

View File

@@ -146,11 +146,13 @@ local function connect_once(self)
return true return true
end end
local function try_connect(self) local function try_connect(self , once)
local t = 100 local t = 100
while not self.__closed do while not self.__closed do
if connect_once(self) then if connect_once(self) then
return return
elseif once then
error(string.format("Connect to %s:%d failed", self.__host, self.__port))
end end
if t > 1000 then if t > 1000 then
print("socket: try to reconnect", self.__host, self.__port) print("socket: try to reconnect", self.__host, self.__port)
@@ -163,7 +165,7 @@ local function try_connect(self)
end end
end end
local function block_connect(self) local function block_connect(self, once)
if self.__sock then if self.__sock then
local authco = self.__authcoroutine local authco = self.__authcoroutine
if not authco then if not authco then
@@ -183,11 +185,11 @@ local function block_connect(self)
table.insert(self.__connecting, co) table.insert(self.__connecting, co)
skynet.wait() skynet.wait()
-- check connection again -- check connection again
return block_connect(self) return block_connect(self, once)
end end
self.__connecting[1] = true self.__connecting[1] = true
try_connect(self) try_connect(self, once)
self.__connecting[1] = nil self.__connecting[1] = nil
for i=2, #self.__connecting do for i=2, #self.__connecting do
local co = self.__connecting[i] local co = self.__connecting[i]
@@ -196,15 +198,15 @@ local function block_connect(self)
end end
-- check again -- check again
return block_connect(self) return block_connect(self, once)
end end
function channel:connect() function channel:connect(once)
if self.__closed then if self.__closed then
self.__closed = false self.__closed = false
end end
return block_connect(self) return block_connect(self, once)
end end
function channel:request(request, response) function channel:request(request, response)