add socketchannel.overload

This commit is contained in:
Cloud Wu
2018-10-17 14:54:44 +08:00
parent e0f16b8df5
commit f110b51290
4 changed files with 33 additions and 11 deletions

View File

@@ -173,6 +173,7 @@ function mongo.client( conf )
auth = mongo_auth(obj),
backup = backup,
nodelay = true,
overload = conf.overload,
}
setmetatable(obj, client_meta)
obj.__sock:connect(true) -- try connect only once

View File

@@ -21,26 +21,17 @@ local sha1= crypt.sha1
local setmetatable = setmetatable
local error = error
local tonumber = tonumber
local new_tab = function (narr, nrec) return {} end
local new_tab = function (narr, nrec) return {} end
local _M = { _VERSION = '0.13' }
local _M = { _VERSION = '0.14' }
-- constants
local STATE_CONNECTED = 1
local STATE_COMMAND_SENT = 2
local COM_QUERY = 0x03
local SERVER_MORE_RESULTS_EXISTS = 8
-- 16MB - 1, the default max allowed packet size used by libmysqlclient
local FULL_PACKET_SIZE = 16777215
local mt = { __index = _M }
-- mysql field value type converters
local converters = new_tab(0, 8)
@@ -640,6 +631,7 @@ function _M.connect(opts)
host = opts.host,
port = opts.port or 3306,
auth = _mysql_login(self,user,password,database,opts.on_connect),
overload = opts.overload,
}
self.sockchannel = channel
-- try connect first only once

View File

@@ -143,6 +143,7 @@ function redis.connect(db_conf)
port = db_conf.port or 6379,
auth = redis_login(db_conf.auth, db_conf.db),
nodelay = true,
overload = db_conf.overload,
}
-- try connect first only once
channel:connect(true)

View File

@@ -39,6 +39,8 @@ function socket_channel.channel(desc)
__closed = false,
__authcoroutine = false,
__nodelay = desc.nodelay,
__overload_notify = desc.overload,
__overload = false,
}
return setmetatable(c, channel_meta)
@@ -246,6 +248,32 @@ local function connect_once(self)
socketdriver.nodelay(fd)
end
-- register overload warning
local overload = self.__overload_notify
if overload then
local function overload_trigger(id, size)
if id == self.__sock[1] then
if size == 0 then
if self.__overload then
self.__overload = false
overload(false)
end
else
if not self.__overload then
self.__overload = true
overload(true)
else
skynet.error(string.format("WARNING: %d K bytes need to send out (fd = %d %s:%s)", size, id, self.__host, self.__port))
end
end
end
end
skynet.fork(overload_trigger, fd, 0)
socket.warning(fd, overload_trigger)
end
while self.__dispatch_thread do
-- wait for dispatch thread exit
skynet.yield()