introduce socket.channel, and rewrite redis/mongo driver

This commit is contained in:
Cloud Wu
2014-03-23 16:05:36 +08:00
parent fd6e9a8bd1
commit 1eb775a326
5 changed files with 419 additions and 319 deletions

View File

@@ -63,74 +63,28 @@ local collection_meta = {
end
}
local function try_connect(host, port)
-- try 10 times
for i = 1, 10 do
local sock = socket.open(host, port)
if not sock then
-- 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.cursor_id = cursor_id
result.startfrom = startfrom
result.data = reply
skynet.wakeup(result.co)
end
end
end
local function dispatch_reply(so)
local len_reply = so:read(4)
local reply = so:read(driver.length(len_reply))
local result = {}
local succ, reply_id, document, cursor_id, startfrom = driver.reply(reply, result)
result.document = document
result.cursor_id = cursor_id
result.startfrom = startfrom
result.data = reply
return reply_id, succ, result
end
function mongo.client( obj )
obj.port = obj.port or 27017
obj.__id = 0
obj.__sock = try_connect(obj.host, obj.port)
obj.__request = {}
obj.__sock = socket.channel {
host = obj.host,
port = obj.port,
response = dispatch_reply,
}
setmetatable(obj, client_meta)
skynet.fork(reply_queue, obj)
obj.__sock:connect()
return obj
end
@@ -168,14 +122,6 @@ function mongo_client:runCommand(...)
return self.admin:runCommand(...)
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,...)
local conn = self.connection
local request_id = conn:genId()
@@ -187,11 +133,7 @@ function mongo_db:runCommand(cmd,cmd_v,...)
bson_cmd = bson_encode_order(cmd,cmd_v,...)
end
local pack = driver.query(request_id, 0, self.__cmd, 0, 1, bson_cmd)
-- todo: check send
assert(socket.write(sock, pack), "write fail")
local _, succ, doc = get_reply(conn,request_id)
-- todo: check succ and doc
assert(succ, "runCommand error")
local doc = sock:request(pack, request_id).document
return bson_decode(doc)
end
@@ -214,9 +156,8 @@ function mongo_collection:insert(doc)
end
local sock = self.connection.__sock
local pack = driver.insert(0, self.full_name, bson_encode(doc))
-- todo: check send
-- flags support 1: ContinueOnError
assert(socket.write(sock, pack), "write fail")
sock:request(pack)
end
function mongo_collection:batch_insert(docs)
@@ -228,23 +169,20 @@ function mongo_collection:batch_insert(docs)
end
local sock = self.connection.__sock
local pack = driver.insert(0, self.full_name, docs)
-- todo: check send
assert(socket.write(sock, pack), "write fail")
sock:request(pack)
end
function mongo_collection:update(selector,update,upsert,multi)
local flags = (upsert and 1 or 0) + (multi and 2 or 0)
local sock = self.connection.__sock
local pack = driver.update(self.full_name, flags, bson_encode(selector), bson_encode(update))
-- todo: check send
assert(socket.write(sock, pack),"write fail")
sock:request(pack)
end
function mongo_collection:delete(selector, single)
local sock = self.connection.__sock
local pack = driver.delete(self.full_name, single, bson_encode(selector))
-- todo: check send
assert(socket.write(sock, pack), "write fail")
sock:request(pack)
end
function mongo_collection:findOne(query, selector)
@@ -252,12 +190,7 @@ function mongo_collection:findOne(query, selector)
local request_id = conn:genId()
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))
-- todo: check send
assert(socket.write(sock, pack),"write fail")
local _, succ, doc = get_reply(conn, request_id)
-- todo: check succ
local doc = sock:request(pack, request_id).document
return bson_decode(doc)
end
@@ -296,12 +229,14 @@ function mongo_cursor:hasNext()
end
end
--todo: check send
assert(socket.write(sock, pack),"write fail")
local ok, result = pcall(sock.request,sock,pack, request_id)
local data, succ, doc, cursor = get_reply(conn, request_id, self.__document)
if succ then
local doc = result.document
local cursor = result.cursor_id
if ok then
if doc then
self.__document = doc
self.__data = data
self.__ptr = 1
self.__cursor = cursor
@@ -346,8 +281,7 @@ function mongo_cursor:close()
if self.__cursor then
local sock = self.__collection.connection.__sock
local pack = driver.kill(self.__cursor)
-- todo: check send
assert(socket.write(sock, pack),"write fail")
sock:request(pack)
end
end