mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 12:43:09 +00:00
introduce socket.channel, and rewrite redis/mongo driver
This commit is contained in:
@@ -521,24 +521,6 @@ reply_length(lua_State *L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
|
||||||
copy_result(lua_State *L) {
|
|
||||||
if (lua_type(L,2) == LUA_TNIL)
|
|
||||||
return 0;
|
|
||||||
int n1 = lua_rawlen(L,1);
|
|
||||||
int n2 = lua_rawlen(L,2);
|
|
||||||
int i;
|
|
||||||
for (i=0;i<n1;i++) {
|
|
||||||
lua_rawgeti(L,1,i+1);
|
|
||||||
lua_rawseti(L,2,i+1);
|
|
||||||
}
|
|
||||||
for (i=n1;i<n2;i++) {
|
|
||||||
lua_pushnil(L);
|
|
||||||
lua_rawseti(L,2,i+1);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
int
|
||||||
luaopen_mongo_driver(lua_State *L) {
|
luaopen_mongo_driver(lua_State *L) {
|
||||||
luaL_checkversion(L);
|
luaL_checkversion(L);
|
||||||
@@ -551,7 +533,6 @@ luaopen_mongo_driver(lua_State *L) {
|
|||||||
{ "update", op_update },
|
{ "update", op_update },
|
||||||
{ "insert", op_insert },
|
{ "insert", op_insert },
|
||||||
{ "length", reply_length },
|
{ "length", reply_length },
|
||||||
{ "copy_result", copy_result },
|
|
||||||
{ NULL, NULL },
|
{ NULL, NULL },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
116
lualib/mongo.lua
116
lualib/mongo.lua
@@ -63,74 +63,28 @@ local collection_meta = {
|
|||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
local function try_connect(host, port)
|
local function dispatch_reply(so)
|
||||||
-- try 10 times
|
local len_reply = so:read(4)
|
||||||
for i = 1, 10 do
|
local reply = so:read(driver.length(len_reply))
|
||||||
local sock = socket.open(host, port)
|
local result = {}
|
||||||
if not sock then
|
local succ, reply_id, document, cursor_id, startfrom = driver.reply(reply, result)
|
||||||
-- todo: write log
|
|
||||||
print("Try to connect " .. host .. " failed")
|
|
||||||
skynet.sleep(100*i)
|
|
||||||
else
|
|
||||||
return sock
|
|
||||||
end
|
|
||||||
end
|
|
||||||
error("Connect to mongo " .. host .. " failed")
|
|
||||||
end
|
|
||||||
|
|
||||||
local function reconnect(obj)
|
|
||||||
for _,v in pairs(obj.__request) do
|
|
||||||
v.succ = nil
|
|
||||||
-- wakeup request coroutine and send a failure.
|
|
||||||
skynet.wakeup(v.co)
|
|
||||||
end
|
|
||||||
-- todo: reconnect is too simple now.
|
|
||||||
local sock = try_connect(obj.host, obj.port)
|
|
||||||
obj.__sock = sock
|
|
||||||
return sock
|
|
||||||
end
|
|
||||||
|
|
||||||
local function reply_queue(obj)
|
|
||||||
local sock = obj.__sock
|
|
||||||
local tmp = {}
|
|
||||||
local request_set = obj.__request
|
|
||||||
while true do
|
|
||||||
local len_reply = socket.read(sock, 4)
|
|
||||||
if not len_reply then
|
|
||||||
if not obj.__sock then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
sock = reconnect(obj)
|
|
||||||
else
|
|
||||||
local length = driver.length(len_reply)
|
|
||||||
local reply = socket.read(sock, length)
|
|
||||||
if not reply then
|
|
||||||
if not obj.__sock then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
sock = reconnect(obj)
|
|
||||||
else
|
|
||||||
local succ, reply_id, document, cursor_id, startfrom = driver.reply(reply, tmp)
|
|
||||||
local result = assert(request_set[reply_id])
|
|
||||||
driver.copy_result(tmp, result.result)
|
|
||||||
result.succ = succ
|
|
||||||
result.document = document
|
result.document = document
|
||||||
result.cursor_id = cursor_id
|
result.cursor_id = cursor_id
|
||||||
result.startfrom = startfrom
|
result.startfrom = startfrom
|
||||||
result.data = reply
|
result.data = reply
|
||||||
skynet.wakeup(result.co)
|
return reply_id, succ, result
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function mongo.client( obj )
|
function mongo.client( obj )
|
||||||
obj.port = obj.port or 27017
|
obj.port = obj.port or 27017
|
||||||
obj.__id = 0
|
obj.__id = 0
|
||||||
obj.__sock = try_connect(obj.host, obj.port)
|
obj.__sock = socket.channel {
|
||||||
obj.__request = {}
|
host = obj.host,
|
||||||
|
port = obj.port,
|
||||||
|
response = dispatch_reply,
|
||||||
|
}
|
||||||
setmetatable(obj, client_meta)
|
setmetatable(obj, client_meta)
|
||||||
skynet.fork(reply_queue, obj)
|
obj.__sock:connect()
|
||||||
return obj
|
return obj
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -168,14 +122,6 @@ function mongo_client:runCommand(...)
|
|||||||
return self.admin:runCommand(...)
|
return self.admin:runCommand(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_reply(conn, request_id, result)
|
|
||||||
local r = { result = result , co = coroutine.running() }
|
|
||||||
conn.__request[request_id] = r
|
|
||||||
skynet.wait()
|
|
||||||
conn.__request[request_id] = nil
|
|
||||||
return r.data, r.succ, r.document, r.cursor_id, r.startfrom
|
|
||||||
end
|
|
||||||
|
|
||||||
function mongo_db:runCommand(cmd,cmd_v,...)
|
function mongo_db:runCommand(cmd,cmd_v,...)
|
||||||
local conn = self.connection
|
local conn = self.connection
|
||||||
local request_id = conn:genId()
|
local request_id = conn:genId()
|
||||||
@@ -187,11 +133,7 @@ function mongo_db:runCommand(cmd,cmd_v,...)
|
|||||||
bson_cmd = bson_encode_order(cmd,cmd_v,...)
|
bson_cmd = bson_encode_order(cmd,cmd_v,...)
|
||||||
end
|
end
|
||||||
local pack = driver.query(request_id, 0, self.__cmd, 0, 1, bson_cmd)
|
local pack = driver.query(request_id, 0, self.__cmd, 0, 1, bson_cmd)
|
||||||
-- todo: check send
|
local doc = sock:request(pack, request_id).document
|
||||||
assert(socket.write(sock, pack), "write fail")
|
|
||||||
local _, succ, doc = get_reply(conn,request_id)
|
|
||||||
-- todo: check succ and doc
|
|
||||||
assert(succ, "runCommand error")
|
|
||||||
return bson_decode(doc)
|
return bson_decode(doc)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -214,9 +156,8 @@ function mongo_collection:insert(doc)
|
|||||||
end
|
end
|
||||||
local sock = self.connection.__sock
|
local sock = self.connection.__sock
|
||||||
local pack = driver.insert(0, self.full_name, bson_encode(doc))
|
local pack = driver.insert(0, self.full_name, bson_encode(doc))
|
||||||
-- todo: check send
|
|
||||||
-- flags support 1: ContinueOnError
|
-- flags support 1: ContinueOnError
|
||||||
assert(socket.write(sock, pack), "write fail")
|
sock:request(pack)
|
||||||
end
|
end
|
||||||
|
|
||||||
function mongo_collection:batch_insert(docs)
|
function mongo_collection:batch_insert(docs)
|
||||||
@@ -228,23 +169,20 @@ function mongo_collection:batch_insert(docs)
|
|||||||
end
|
end
|
||||||
local sock = self.connection.__sock
|
local sock = self.connection.__sock
|
||||||
local pack = driver.insert(0, self.full_name, docs)
|
local pack = driver.insert(0, self.full_name, docs)
|
||||||
-- todo: check send
|
sock:request(pack)
|
||||||
assert(socket.write(sock, pack), "write fail")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function mongo_collection:update(selector,update,upsert,multi)
|
function mongo_collection:update(selector,update,upsert,multi)
|
||||||
local flags = (upsert and 1 or 0) + (multi and 2 or 0)
|
local flags = (upsert and 1 or 0) + (multi and 2 or 0)
|
||||||
local sock = self.connection.__sock
|
local sock = self.connection.__sock
|
||||||
local pack = driver.update(self.full_name, flags, bson_encode(selector), bson_encode(update))
|
local pack = driver.update(self.full_name, flags, bson_encode(selector), bson_encode(update))
|
||||||
-- todo: check send
|
sock:request(pack)
|
||||||
assert(socket.write(sock, pack),"write fail")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function mongo_collection:delete(selector, single)
|
function mongo_collection:delete(selector, single)
|
||||||
local sock = self.connection.__sock
|
local sock = self.connection.__sock
|
||||||
local pack = driver.delete(self.full_name, single, bson_encode(selector))
|
local pack = driver.delete(self.full_name, single, bson_encode(selector))
|
||||||
-- todo: check send
|
sock:request(pack)
|
||||||
assert(socket.write(sock, pack), "write fail")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function mongo_collection:findOne(query, selector)
|
function mongo_collection:findOne(query, selector)
|
||||||
@@ -252,12 +190,7 @@ function mongo_collection:findOne(query, selector)
|
|||||||
local request_id = conn:genId()
|
local request_id = conn:genId()
|
||||||
local sock = conn.__sock
|
local sock = conn.__sock
|
||||||
local pack = driver.query(request_id, 0, self.full_name, 0, 1, query and bson_encode(query) or empty_bson, selector and bson_encode(selector))
|
local pack = driver.query(request_id, 0, self.full_name, 0, 1, query and bson_encode(query) or empty_bson, selector and bson_encode(selector))
|
||||||
|
local doc = sock:request(pack, request_id).document
|
||||||
-- todo: check send
|
|
||||||
assert(socket.write(sock, pack),"write fail")
|
|
||||||
|
|
||||||
local _, succ, doc = get_reply(conn, request_id)
|
|
||||||
-- todo: check succ
|
|
||||||
return bson_decode(doc)
|
return bson_decode(doc)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -296,12 +229,14 @@ function mongo_cursor:hasNext()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--todo: check send
|
local ok, result = pcall(sock.request,sock,pack, request_id)
|
||||||
assert(socket.write(sock, pack),"write fail")
|
|
||||||
|
|
||||||
local data, succ, doc, cursor = get_reply(conn, request_id, self.__document)
|
local doc = result.document
|
||||||
if succ then
|
local cursor = result.cursor_id
|
||||||
|
|
||||||
|
if ok then
|
||||||
if doc then
|
if doc then
|
||||||
|
self.__document = doc
|
||||||
self.__data = data
|
self.__data = data
|
||||||
self.__ptr = 1
|
self.__ptr = 1
|
||||||
self.__cursor = cursor
|
self.__cursor = cursor
|
||||||
@@ -346,8 +281,7 @@ function mongo_cursor:close()
|
|||||||
if self.__cursor then
|
if self.__cursor then
|
||||||
local sock = self.__collection.connection.__sock
|
local sock = self.__collection.connection.__sock
|
||||||
local pack = driver.kill(self.__cursor)
|
local pack = driver.kill(self.__cursor)
|
||||||
-- todo: check send
|
sock:request(pack)
|
||||||
assert(socket.write(sock, pack),"write fail")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
264
lualib/redis.lua
264
lualib/redis.lua
@@ -4,8 +4,6 @@ local config = require "config"
|
|||||||
local redis_conf = skynet.getenv "redis"
|
local redis_conf = skynet.getenv "redis"
|
||||||
local name = config (redis_conf)
|
local name = config (redis_conf)
|
||||||
|
|
||||||
local readline = socket.readline
|
|
||||||
local readbytes = socket.read
|
|
||||||
local table = table
|
local table = table
|
||||||
local string = string
|
local string = string
|
||||||
|
|
||||||
@@ -14,26 +12,87 @@ local command = {}
|
|||||||
local meta = {
|
local meta = {
|
||||||
__index = command,
|
__index = command,
|
||||||
__gc = function(self)
|
__gc = function(self)
|
||||||
socket.close(self.__handle)
|
self[1]:close()
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
function redis.connect(dbname)
|
---------- redis response
|
||||||
local db_conf = name[dbname]
|
local redcmd = {}
|
||||||
local fd = assert(socket.open(db_conf.host, db_conf.port or 6379))
|
|
||||||
local r = setmetatable( { __handle = fd, __mode = false }, meta )
|
redcmd[42] = function(fd, data) -- '*'
|
||||||
if db_conf.auth ~= nil then
|
local n = tonumber(data)
|
||||||
r:auth(db_conf.auth)
|
if n < 0 then
|
||||||
|
return true, nil
|
||||||
end
|
end
|
||||||
if db_conf.db ~= nil then
|
local bulk = {}
|
||||||
r:select(db_conf.db)
|
for i = 1,n do
|
||||||
|
local line = fd:readline "\r\n"
|
||||||
|
local bytes = tonumber(string.sub(line,2))
|
||||||
|
if bytes >= 0 then
|
||||||
|
local data = fd:read(bytes + 2)
|
||||||
|
-- bulk[i] = nil when bytes < 0
|
||||||
|
bulk[i] = string.sub(data,1,-3)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return true, bulk
|
||||||
end
|
end
|
||||||
|
|
||||||
return r
|
redcmd[36] = function(fd, data) -- '$'
|
||||||
|
local bytes = tonumber(data)
|
||||||
|
if bytes < 0 then
|
||||||
|
return true,nil
|
||||||
|
end
|
||||||
|
local firstline = fd:read(bytes+2)
|
||||||
|
return true,string.sub(firstline,1,-3)
|
||||||
|
end
|
||||||
|
|
||||||
|
redcmd[43] = function(fd, data) -- '+'
|
||||||
|
return true,data
|
||||||
|
end
|
||||||
|
|
||||||
|
redcmd[45] = function(fd, data) -- '-'
|
||||||
|
return false,data
|
||||||
|
end
|
||||||
|
|
||||||
|
redcmd[58] = function(fd, data) -- ':'
|
||||||
|
-- todo: return string later
|
||||||
|
return true, tonumber(data)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function read_response(fd)
|
||||||
|
local result = fd:readline "\r\n"
|
||||||
|
local firstchar = string.byte(result)
|
||||||
|
local data = string.sub(result,2)
|
||||||
|
return redcmd[firstchar](fd,data)
|
||||||
|
end
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
local function redis_login(auth, db)
|
||||||
|
if auth == nil and db == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
return function(so)
|
||||||
|
if auth then
|
||||||
|
so:request("AUTH "..auth.."\r\n", read_response)
|
||||||
|
end
|
||||||
|
if db then
|
||||||
|
so:request("SELECT "..db.."\r\n", read_response)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function redis.connect(dbname)
|
||||||
|
local db_conf = name[dbname]
|
||||||
|
local channel = socket.channel {
|
||||||
|
host = db_conf.host,
|
||||||
|
port = db_conf.port or 6379,
|
||||||
|
auth = redis_login(db_conf.auth, db_conf.db),
|
||||||
|
}
|
||||||
|
return setmetatable( { channel }, meta )
|
||||||
end
|
end
|
||||||
|
|
||||||
function command:disconnect()
|
function command:disconnect()
|
||||||
socket.close(self.__handle)
|
self[1]:close()
|
||||||
setmetatable(self, nil)
|
setmetatable(self, nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -58,140 +117,32 @@ local function compose_message(msg)
|
|||||||
return cmd
|
return cmd
|
||||||
end
|
end
|
||||||
|
|
||||||
local redcmd = {}
|
|
||||||
|
|
||||||
redcmd[42] = function(fd, data) -- '*'
|
|
||||||
local n = tonumber(data)
|
|
||||||
if n < 0 then
|
|
||||||
return true, nil
|
|
||||||
end
|
|
||||||
local bulk = {}
|
|
||||||
for i = 1,n do
|
|
||||||
local line = readline(fd,"\r\n")
|
|
||||||
local bytes = tonumber(string.sub(line,2))
|
|
||||||
if bytes >= 0 then
|
|
||||||
local data = readbytes(fd, bytes + 2)
|
|
||||||
-- bulk[i] = nil when bytes < 0
|
|
||||||
bulk[i] = string.sub(data,1,-3)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return true, bulk
|
|
||||||
end
|
|
||||||
|
|
||||||
redcmd[36] = function(fd, data) -- '$'
|
|
||||||
local bytes = tonumber(data)
|
|
||||||
if bytes < 0 then
|
|
||||||
return true,nil
|
|
||||||
end
|
|
||||||
local firstline = readbytes(fd, bytes+2)
|
|
||||||
return true,string.sub(firstline,1,-3)
|
|
||||||
end
|
|
||||||
|
|
||||||
redcmd[43] = function(fd, data) -- '+'
|
|
||||||
return true,data
|
|
||||||
end
|
|
||||||
|
|
||||||
redcmd[45] = function(fd, data) -- '-'
|
|
||||||
return false,data
|
|
||||||
end
|
|
||||||
|
|
||||||
redcmd[58] = function(fd, data) -- ':'
|
|
||||||
-- todo: return string later
|
|
||||||
return true, tonumber(data)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function read_response(fd)
|
|
||||||
local result = readline(fd, "\r\n")
|
|
||||||
local firstchar = string.byte(result)
|
|
||||||
local data = string.sub(result,2)
|
|
||||||
return redcmd[firstchar](fd,data)
|
|
||||||
end
|
|
||||||
|
|
||||||
setmetatable(command, { __index = function(t,k)
|
setmetatable(command, { __index = function(t,k)
|
||||||
local cmd = string.upper(k)
|
local cmd = string.upper(k)
|
||||||
local f = function (self, ...)
|
local f = function (self, ...)
|
||||||
local fd = self.__handle
|
return self[1]:request(compose_message { cmd, ... }, read_response)
|
||||||
if self.__mode then
|
|
||||||
socket.write(fd, compose_message { cmd, ... })
|
|
||||||
self.__batch = self.__batch + 1
|
|
||||||
else
|
|
||||||
socket.lock(fd)
|
|
||||||
socket.write(fd, compose_message { cmd, ... })
|
|
||||||
local ok, ret = read_response(fd)
|
|
||||||
socket.unlock(fd)
|
|
||||||
assert(ok, ret)
|
|
||||||
return ret
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
t[k] = f
|
t[k] = f
|
||||||
return f
|
return f
|
||||||
end})
|
end})
|
||||||
|
|
||||||
|
local function read_boolean(so)
|
||||||
|
local ok, result = read_response(so)
|
||||||
|
return ok, result ~= 0
|
||||||
|
end
|
||||||
|
|
||||||
function command:exists(key)
|
function command:exists(key)
|
||||||
assert(not self.__mode, "exists can't used in batch mode")
|
local fd = self[1]
|
||||||
local fd = self.__handle
|
return fd:request(compose_message { "EXISTS", key }, read_boolean)
|
||||||
socket.lock(fd)
|
|
||||||
socket.write(fd, compose_message { "EXISTS", key })
|
|
||||||
local ok, exists = read_response(fd)
|
|
||||||
socket.unlock(fd)
|
|
||||||
assert(ok, exists)
|
|
||||||
return exists ~= 0
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function command:sismember(key, value)
|
function command:sismember(key, value)
|
||||||
assert(not self.__mode, "sismember can't used in batch mode")
|
local fd = self[1]
|
||||||
local fd = self.__handle
|
return fd:request(compose_message { "SISMEMBER", key, value }, read_boolean)
|
||||||
socket.lock(fd)
|
|
||||||
socket.write(fd, compose_message { "SISMEMBER", key, value })
|
|
||||||
local ok, ismember = read_response(fd)
|
|
||||||
socket.unlock(fd)
|
|
||||||
assert(ok, ismember)
|
|
||||||
return ismember ~= 0
|
|
||||||
end
|
|
||||||
|
|
||||||
function command:batch(mode)
|
|
||||||
if mode == "end" then
|
|
||||||
local fd = self.__handle
|
|
||||||
if self.__mode == "read" then
|
|
||||||
local allok = true
|
|
||||||
local allret = {}
|
|
||||||
for i = 1, self.__batch do
|
|
||||||
local ok, ret = read_response(fd)
|
|
||||||
allok = allok and ok
|
|
||||||
allret[i] = ret
|
|
||||||
end
|
|
||||||
self.__mode = false
|
|
||||||
socket.unlock(self.__handle)
|
|
||||||
assert(allok, "batch read failed")
|
|
||||||
return allret
|
|
||||||
else
|
|
||||||
local allok = true
|
|
||||||
for i = 1, self.__batch do
|
|
||||||
local ok = read_response(fd)
|
|
||||||
allok = allok and ok
|
|
||||||
end
|
|
||||||
self.__mode = false
|
|
||||||
socket.unlock(self.__handle)
|
|
||||||
return allok
|
|
||||||
end
|
|
||||||
else
|
|
||||||
assert(mode == "read" or mode == "write")
|
|
||||||
socket.lock(self.__handle)
|
|
||||||
self.__mode = mode
|
|
||||||
self.__batch = 0
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function command:multi()
|
|
||||||
local fd = self.__handle
|
|
||||||
socket.lock(fd)
|
|
||||||
self.__mode = "multi"
|
|
||||||
self.__batch = 0
|
|
||||||
socket.write(fd, "MULTI\r\n")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function read_exec(fd)
|
local function read_exec(fd)
|
||||||
local result = readline(fd, "\r\n")
|
local result = fd:readline "\r\n"
|
||||||
local firstchar = string.byte(result)
|
local firstchar = string.byte(result)
|
||||||
local data = string.sub(result,2)
|
local data = string.sub(result,2)
|
||||||
if firstchar ~= 42 then
|
if firstchar ~= 42 then
|
||||||
@@ -203,57 +154,24 @@ local function read_exec(fd)
|
|||||||
local err = nil
|
local err = nil
|
||||||
for i = 1,n do
|
for i = 1,n do
|
||||||
local ok, r = read_response(fd)
|
local ok, r = read_response(fd)
|
||||||
|
if ok then
|
||||||
result[i] = r
|
result[i] = r
|
||||||
if err then
|
|
||||||
err[i] = ok
|
|
||||||
else
|
else
|
||||||
if ok == false then
|
if not err then
|
||||||
err = {}
|
err = {}
|
||||||
for j = 1, i-1 do
|
|
||||||
err[j] = true
|
|
||||||
end
|
end
|
||||||
err[i] = false
|
table.insert(err, i .. ":" .. r)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
if err then
|
||||||
|
return false, table.concat(err,",")
|
||||||
|
else
|
||||||
|
return true, result
|
||||||
end
|
end
|
||||||
return result, err
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function command:exec()
|
function command:exec()
|
||||||
if self.__mode ~= "multi" then
|
return self[1]:request( "EXEC\r\n" , read_exec)
|
||||||
error "call multi first"
|
|
||||||
end
|
|
||||||
local fd = self.__handle
|
|
||||||
socket.write(fd, "EXEC\r\n")
|
|
||||||
local allok = true
|
|
||||||
for i = 0, self.__batch do
|
|
||||||
local ok, queue = read_response(fd)
|
|
||||||
allok = allok and ok
|
|
||||||
end
|
|
||||||
if not allok then
|
|
||||||
self.__mode = false
|
|
||||||
socket.unlock(fd)
|
|
||||||
error "Queue command error"
|
|
||||||
end
|
|
||||||
|
|
||||||
local result, err = read_exec(fd)
|
|
||||||
|
|
||||||
self.__mode = false
|
|
||||||
socket.unlock(fd)
|
|
||||||
|
|
||||||
if not result then
|
|
||||||
error(err)
|
|
||||||
elseif err then
|
|
||||||
local errmsg = ""
|
|
||||||
for k,v in ipairs(err) do
|
|
||||||
if v == false then
|
|
||||||
errmsg = errmsg .. k .. ":" .. result[k]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
error(errmsg)
|
|
||||||
end
|
|
||||||
|
|
||||||
return result
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return redis
|
return redis
|
||||||
|
|||||||
@@ -93,11 +93,14 @@ end
|
|||||||
|
|
||||||
-- SKYNET_SOCKET_TYPE_ERROR = 5
|
-- SKYNET_SOCKET_TYPE_ERROR = 5
|
||||||
socket_message[5] = function(id)
|
socket_message[5] = function(id)
|
||||||
print("error on ", id)
|
|
||||||
local s = socket_pool[id]
|
local s = socket_pool[id]
|
||||||
if s == nil then
|
if s == nil then
|
||||||
|
print("socket: error on unknown", id)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
if s.connected then
|
||||||
|
print("socket: error on", id)
|
||||||
|
end
|
||||||
s.connected = false
|
s.connected = false
|
||||||
wakeup(s)
|
wakeup(s)
|
||||||
end
|
end
|
||||||
@@ -146,6 +149,18 @@ function socket.start(id, func)
|
|||||||
return connect(id, func)
|
return connect(id, func)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function clear_socket(id)
|
||||||
|
local s = socket_pool[id]
|
||||||
|
if s then
|
||||||
|
if s.buffer then
|
||||||
|
driver.clear(s.buffer,buffer_pool)
|
||||||
|
end
|
||||||
|
if s.connected then
|
||||||
|
driver.close(id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function socket.close(id)
|
function socket.close(id)
|
||||||
local s = socket_pool[id]
|
local s = socket_pool[id]
|
||||||
if s == nil then
|
if s == nil then
|
||||||
@@ -163,10 +178,9 @@ function socket.close(id)
|
|||||||
else
|
else
|
||||||
suspend(s)
|
suspend(s)
|
||||||
end
|
end
|
||||||
|
s.connected = false
|
||||||
end
|
end
|
||||||
if s.buffer then
|
clear_socket(id)
|
||||||
driver.clear(s.buffer,buffer_pool)
|
|
||||||
end
|
|
||||||
assert(s.lock_set == nil or next(s.lock_set) == nil)
|
assert(s.lock_set == nil or next(s.lock_set) == nil)
|
||||||
socket_pool[id] = nil
|
socket_pool[id] = nil
|
||||||
end
|
end
|
||||||
@@ -235,6 +249,17 @@ function socket.readline(id, sep)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function socket.block(id)
|
||||||
|
local s = socket_pool[id]
|
||||||
|
if not s or not s.connected then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
assert(not s.read_required)
|
||||||
|
s.read_required = 0
|
||||||
|
suspend(s)
|
||||||
|
return s.connected
|
||||||
|
end
|
||||||
|
|
||||||
socket.write = assert(driver.send)
|
socket.write = assert(driver.send)
|
||||||
|
|
||||||
function socket.invalid(id)
|
function socket.invalid(id)
|
||||||
@@ -251,10 +276,10 @@ function socket.lock(id)
|
|||||||
lock_set = {}
|
lock_set = {}
|
||||||
s.lock = lock_set
|
s.lock = lock_set
|
||||||
end
|
end
|
||||||
local co = coroutine.running()
|
|
||||||
if #lock_set == 0 then
|
if #lock_set == 0 then
|
||||||
lock_set[1] = co
|
lock_set[1] = true
|
||||||
else
|
else
|
||||||
|
local co = coroutine.running()
|
||||||
table.insert(lock_set, co)
|
table.insert(lock_set, co)
|
||||||
skynet.wait()
|
skynet.wait()
|
||||||
end
|
end
|
||||||
@@ -263,12 +288,9 @@ end
|
|||||||
function socket.unlock(id)
|
function socket.unlock(id)
|
||||||
local s = socket_pool[id]
|
local s = socket_pool[id]
|
||||||
assert(s)
|
assert(s)
|
||||||
local lock_set = s.lock
|
local lock_set = assert(s.lock)
|
||||||
assert(lock_set)
|
|
||||||
local co = coroutine.running()
|
|
||||||
assert(lock_set[1] == co)
|
|
||||||
table.remove(lock_set,1)
|
table.remove(lock_set,1)
|
||||||
co = lock_set[1]
|
local co = lock_set[1]
|
||||||
if co then
|
if co then
|
||||||
skynet.wakeup(co)
|
skynet.wakeup(co)
|
||||||
end
|
end
|
||||||
@@ -284,4 +306,251 @@ function socket.abandon(id)
|
|||||||
socket_pool[id] = nil
|
socket_pool[id] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- channel support auto reconnect , and capture socket error in request/response transaction
|
||||||
|
-- { host = "", port = , auth = function(so) , response = function(so) session, data }
|
||||||
|
|
||||||
|
local channel = {}
|
||||||
|
local channel_socket = {}
|
||||||
|
local channel_meta = { __index = channel }
|
||||||
|
local channel_socket_meta = {
|
||||||
|
__index = channel_socket,
|
||||||
|
__gc = function(cs)
|
||||||
|
local fd = cs[1]
|
||||||
|
cs[1] = false
|
||||||
|
if fd then
|
||||||
|
clear_socket(fd)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}
|
||||||
|
local socket_error = channel_socket -- alias for error object
|
||||||
|
|
||||||
|
function socket.channel(desc)
|
||||||
|
local c = {
|
||||||
|
__host = assert(desc.host),
|
||||||
|
__port = assert(desc.port),
|
||||||
|
__auth = desc.auth,
|
||||||
|
__response = desc.response,
|
||||||
|
__request = {}, -- request seq { response func or session }
|
||||||
|
__thread = {}, -- coroutine seq or session->coroutine map
|
||||||
|
__result = {}, -- response result { coroutine -> result }
|
||||||
|
__result_data = {},
|
||||||
|
__connecting = {},
|
||||||
|
__sock = false,
|
||||||
|
__closed = false,
|
||||||
|
}
|
||||||
|
|
||||||
|
return setmetatable(c, channel_meta)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function close_channel_socket(self)
|
||||||
|
if self.__sock then
|
||||||
|
local so = self.__sock
|
||||||
|
self.__sock = false
|
||||||
|
socket.close(so[1])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function wakeup_all(self, errmsg)
|
||||||
|
for i = 1, #self.__thread do
|
||||||
|
local co = self.__thread[i]
|
||||||
|
self.__thread[i] = nil
|
||||||
|
self.__result[co] = socket_error
|
||||||
|
self.__result_data[co] = errmsg
|
||||||
|
skynet.wakeup(co)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function dispatch_response(self)
|
||||||
|
local response = self.__response
|
||||||
|
if response then
|
||||||
|
-- response() return session
|
||||||
|
while self.__sock do
|
||||||
|
local ok , session, result_ok, result_data = pcall(response, self.__sock)
|
||||||
|
if ok then
|
||||||
|
local co = self.__thread[session]
|
||||||
|
self.__thread[session] = nil
|
||||||
|
if co then
|
||||||
|
self.__result[co] = result_ok
|
||||||
|
self.__result_data[co] = result_data
|
||||||
|
skynet.wakeup(co)
|
||||||
|
else
|
||||||
|
print("socket: unknown session :", session)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
close_channel_socket(self)
|
||||||
|
local errormsg
|
||||||
|
if session ~= socket_error then
|
||||||
|
errormsg = session
|
||||||
|
end
|
||||||
|
for k,co in pairs(self.__thread) do
|
||||||
|
-- throw error (errormsg)
|
||||||
|
self.__thread[k] = nil
|
||||||
|
self.__result[co] = socket_error
|
||||||
|
self.__result_data[co] = errormsg
|
||||||
|
skynet.wakeup(co)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
-- pop response function from __request
|
||||||
|
while self.__sock do
|
||||||
|
local func = table.remove(self.__request, 1)
|
||||||
|
if func == nil then
|
||||||
|
if not socket.block(self.__sock[1]) then
|
||||||
|
close_channel_socket(self)
|
||||||
|
wakeup_all(self)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
local ok, result_ok, result_data = pcall(func, self.__sock)
|
||||||
|
if ok then
|
||||||
|
local co = table.remove(self.__thread, 1)
|
||||||
|
self.__result[co] = result_ok
|
||||||
|
self.__result_data[co] = result_data
|
||||||
|
skynet.wakeup(co)
|
||||||
|
else
|
||||||
|
close_channel_socket(self)
|
||||||
|
local errmsg
|
||||||
|
if result ~= socket_error then
|
||||||
|
errmsg = result_ok
|
||||||
|
end
|
||||||
|
wakeup_all(self, errmsg)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function try_connect(self)
|
||||||
|
assert(not self.__sock)
|
||||||
|
local fd = socket.open(self.__host, self.__port)
|
||||||
|
if not fd then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
self.__sock = setmetatable( {fd} , channel_socket_meta )
|
||||||
|
skynet.fork(dispatch_response, 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
|
||||||
|
error(message)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function channel:connect()
|
||||||
|
if self.__sock then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
if self.__closed then
|
||||||
|
self.__closed = false
|
||||||
|
end
|
||||||
|
if #self.__connecting > 0 then
|
||||||
|
-- connecting in other coroutine
|
||||||
|
local co = coroutine.running()
|
||||||
|
table.insert(self.__connecting, co)
|
||||||
|
skynet.wait()
|
||||||
|
else
|
||||||
|
self.__connecting[1] = true
|
||||||
|
try_connect(self)
|
||||||
|
self.__connecting[1] = nil
|
||||||
|
for i=2, #self.__connecting do
|
||||||
|
local co = self.__connecting[i]
|
||||||
|
self.__connecting[i] = nil
|
||||||
|
skynet.wakeup(co)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if self.__sock then
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function reconnect_channel(self)
|
||||||
|
local t = 100
|
||||||
|
while not self.__closed do
|
||||||
|
if self:connect() then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if t > 1000 then
|
||||||
|
print("socket: try to reconnect", self.__host, self.__port)
|
||||||
|
skynet.sleep(t)
|
||||||
|
t = 0
|
||||||
|
else
|
||||||
|
skynet.sleep(t)
|
||||||
|
end
|
||||||
|
t = t + 100
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function channel:request(request, response)
|
||||||
|
if not self.__sock then
|
||||||
|
assert(not self.__closed)
|
||||||
|
reconnect_channel(self)
|
||||||
|
end
|
||||||
|
|
||||||
|
if not socket.write(self.__sock[1], request) then
|
||||||
|
return self:request(request, response)
|
||||||
|
end
|
||||||
|
|
||||||
|
if response == nil then
|
||||||
|
-- no response
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local co = coroutine.running()
|
||||||
|
|
||||||
|
if self.__response then
|
||||||
|
-- response is session
|
||||||
|
self.__thread[response] = co
|
||||||
|
else
|
||||||
|
-- response is a function, push it to __request
|
||||||
|
table.insert(self.__request, response)
|
||||||
|
table.insert(self.__thread, co)
|
||||||
|
end
|
||||||
|
skynet.wait()
|
||||||
|
|
||||||
|
local result = self.__result[co]
|
||||||
|
self.__result[co] = nil
|
||||||
|
local result_data = self.__result_data[co]
|
||||||
|
self.__result_data[co] = nil
|
||||||
|
|
||||||
|
if result == socket_error then
|
||||||
|
if result_data then
|
||||||
|
print("socket: dispatch", request, result_data)
|
||||||
|
end
|
||||||
|
return self:request(request, response)
|
||||||
|
else
|
||||||
|
assert(result, result_data)
|
||||||
|
return result_data
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function channel:close()
|
||||||
|
if not self.__closed then
|
||||||
|
self.__closed = true
|
||||||
|
close_channel_socket(self)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
channel_meta.__gc = channel.close
|
||||||
|
|
||||||
|
local function wrapper_socket_function(f)
|
||||||
|
return function(self, ...)
|
||||||
|
local result = f(self[1], ...)
|
||||||
|
if not result then
|
||||||
|
error(socket_error)
|
||||||
|
else
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
channel_socket.read = wrapper_socket_function(socket.read)
|
||||||
|
channel_socket.readline = wrapper_socket_function(socket.readline)
|
||||||
|
|
||||||
return socket
|
return socket
|
||||||
|
|||||||
@@ -3,34 +3,32 @@ local redis = require "redis"
|
|||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
local db = redis.connect "main"
|
local db = redis.connect "main"
|
||||||
print(db:select(0))
|
|
||||||
db:batch "write" -- ignore results
|
|
||||||
db:del "C"
|
db:del "C"
|
||||||
db:set("A", "hello")
|
db:set("A", "hello")
|
||||||
db:set("B", "world")
|
db:set("B", "world")
|
||||||
db:sadd("C", "one")
|
db:sadd("C", "one")
|
||||||
print(db:batch "end")
|
|
||||||
|
|
||||||
db:batch "read"
|
print(db:get("A"))
|
||||||
db:get("A")
|
print(db:get("B"))
|
||||||
db:get("B")
|
|
||||||
local r = db:batch "end" -- return all results in a table
|
|
||||||
for k,v in pairs(r) do
|
|
||||||
print(k,v)
|
|
||||||
end
|
|
||||||
|
|
||||||
db:batch "write"
|
|
||||||
db:del "D"
|
db:del "D"
|
||||||
for i=1,1000 do
|
for i=1,1000 do
|
||||||
db:hset("D",i,i)
|
db:hset("D",i,i)
|
||||||
end
|
end
|
||||||
db:batch "end"
|
|
||||||
local r = db:hvals "D"
|
local r = db:hvals "D"
|
||||||
for k,v in pairs(r) do
|
for k,v in pairs(r) do
|
||||||
print(k,v)
|
print(k,v)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--[[
|
||||||
|
db:multi()
|
||||||
|
db:get "A"
|
||||||
|
db:get "B"
|
||||||
|
local t = db:exec()
|
||||||
|
for k,v in ipairs(t) do
|
||||||
|
print("Exec", v)
|
||||||
|
end
|
||||||
|
]]
|
||||||
print(db:exists "A")
|
print(db:exists "A")
|
||||||
print(db:get "A")
|
print(db:get "A")
|
||||||
print(db:set("A","hello world"))
|
print(db:set("A","hello world"))
|
||||||
|
|||||||
Reference in New Issue
Block a user