mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 12:20:41 +00:00
mongo driver support request_id
This commit is contained in:
@@ -521,6 +521,24 @@ 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);
|
||||||
@@ -533,6 +551,7 @@ 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 },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
local bson = require "bson"
|
local bson = require "bson"
|
||||||
local socket = require "socket"
|
local socket = require "socket"
|
||||||
|
local skynet = require "skynet"
|
||||||
local driver = require "mongo.driver"
|
local driver = require "mongo.driver"
|
||||||
local rawget = rawget
|
local rawget = rawget
|
||||||
local assert = assert
|
local assert = assert
|
||||||
@@ -61,11 +62,38 @@ local collection_meta = {
|
|||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
|
local function reply_queue(obj)
|
||||||
|
local sock = obj.__sock
|
||||||
|
local tmp = {}
|
||||||
|
local request_set = obj.__request
|
||||||
|
while true do
|
||||||
|
-- todo: reconnect
|
||||||
|
local length = driver.length(socket.read(sock, 4))
|
||||||
|
local reply = socket.read(sock, length)
|
||||||
|
if reply == nil then
|
||||||
|
--todo : reconnect
|
||||||
|
break
|
||||||
|
end
|
||||||
|
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
|
||||||
|
|
||||||
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 = assert(socket.open(obj.host, obj.port),"Connect failed")
|
obj.__sock = assert(socket.open(obj.host, obj.port),"Connect failed")
|
||||||
return setmetatable(obj, client_meta)
|
obj.__request = {}
|
||||||
|
setmetatable(obj, client_meta)
|
||||||
|
skynet.fork(reply_queue, obj)
|
||||||
|
return obj
|
||||||
end
|
end
|
||||||
|
|
||||||
function mongo_client:getDB(dbname)
|
function mongo_client:getDB(dbname)
|
||||||
@@ -101,24 +129,23 @@ function mongo_client:runCommand(cmd)
|
|||||||
return self.admin:runCommand(cmd)
|
return self.admin:runCommand(cmd)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_reply(sock, result)
|
local function get_reply(conn, request_id, result)
|
||||||
local length = driver.length(socket.read(sock, 4))
|
local r = { result = result , co = coroutine.running() }
|
||||||
local reply = socket.read(sock, length)
|
conn.__request[request_id] = r
|
||||||
return reply, driver.reply(reply, result)
|
skynet.wait()
|
||||||
|
conn.__request[request_id] = nil
|
||||||
|
return r.data, r.succ, r.document, r.cursor_id, r.startfrom
|
||||||
end
|
end
|
||||||
|
|
||||||
function mongo_db:runCommand(cmd)
|
function mongo_db:runCommand(cmd)
|
||||||
local request_id = self.connection:genId()
|
local conn = self.connection
|
||||||
local sock = self.connection.__sock
|
local request_id = conn:genId()
|
||||||
socket.lock(sock)
|
local sock = conn.__sock
|
||||||
local pack = driver.query(request_id, 0, self.__cmd, 0, 1, bson_encode(cmd))
|
local pack = driver.query(request_id, 0, self.__cmd, 0, 1, bson_encode(cmd))
|
||||||
-- todo: check send
|
-- todo: check send
|
||||||
socket.write(sock, pack)
|
socket.write(sock, pack)
|
||||||
|
|
||||||
local _, succ, reply_id, doc = get_reply(sock)
|
local _, succ, doc = get_reply(conn,request_id)
|
||||||
socket.unlock(sock)
|
|
||||||
assert(request_id == reply_id, "Reply from mongod error")
|
|
||||||
-- todo: check succ
|
|
||||||
return bson_decode(doc)
|
return bson_decode(doc)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -175,17 +202,15 @@ function mongo_collection:delete(selector, single)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function mongo_collection:findOne(query, selector)
|
function mongo_collection:findOne(query, selector)
|
||||||
local request_id = self.connection:genId()
|
local conn = self.connection
|
||||||
local sock = self.connection.__sock
|
local request_id = conn:genId()
|
||||||
socket.lock(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))
|
||||||
|
|
||||||
-- todo: check send
|
-- todo: check send
|
||||||
socket.write(sock, pack)
|
socket.write(sock, pack)
|
||||||
|
|
||||||
local _, succ, reply_id, doc = get_reply(sock)
|
local _, succ, doc = get_reply(conn, request_id)
|
||||||
socket.unlock(sock)
|
|
||||||
assert(request_id == reply_id, "Reply from mongod error")
|
|
||||||
-- todo: check succ
|
-- todo: check succ
|
||||||
return bson_decode(doc)
|
return bson_decode(doc)
|
||||||
end
|
end
|
||||||
@@ -225,13 +250,10 @@ function mongo_cursor:hasNext()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
socket.lock(sock)
|
|
||||||
--todo: check send
|
--todo: check send
|
||||||
socket.write(sock, pack)
|
socket.write(sock, pack)
|
||||||
|
|
||||||
local data, succ, reply_id, doc, cursor = get_reply(sock, self.__document)
|
local data, succ, doc, cursor = get_reply(conn, request_id, self.__document)
|
||||||
socket.unlock(sock)
|
|
||||||
assert(request_id == reply_id, "Reply from mongod error")
|
|
||||||
if succ then
|
if succ then
|
||||||
if doc then
|
if doc then
|
||||||
self.__data = data
|
self.__data = data
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ function skynet.sleep(ti)
|
|||||||
local session = c.command("TIMEOUT",tostring(ti))
|
local session = c.command("TIMEOUT",tostring(ti))
|
||||||
assert(session)
|
assert(session)
|
||||||
session = tonumber(session)
|
session = tonumber(session)
|
||||||
local ret = coroutine.yield("SLEEP", session)
|
local ret = coroutine_yield("SLEEP", session)
|
||||||
sleep_session[coroutine.running()] = nil
|
sleep_session[coroutine.running()] = nil
|
||||||
if ret == true then
|
if ret == true then
|
||||||
c.trace_switch(trace_handle, session)
|
c.trace_switch(trace_handle, session)
|
||||||
@@ -133,10 +133,18 @@ end
|
|||||||
function skynet.yield()
|
function skynet.yield()
|
||||||
local session = c.command("TIMEOUT","0")
|
local session = c.command("TIMEOUT","0")
|
||||||
assert(session)
|
assert(session)
|
||||||
coroutine.yield("SLEEP", tonumber(session))
|
coroutine_yield("SLEEP", tonumber(session))
|
||||||
sleep_session[coroutine.running()] = nil
|
sleep_session[coroutine.running()] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function skynet.wait()
|
||||||
|
local session = c.genid()
|
||||||
|
coroutine_yield("SLEEP", session)
|
||||||
|
local co = coroutine.running()
|
||||||
|
sleep_session[co] = nil
|
||||||
|
session_id_coroutine[session] = nil
|
||||||
|
end
|
||||||
|
|
||||||
function skynet.register(name)
|
function skynet.register(name)
|
||||||
c.command("REG", name)
|
c.command("REG", name)
|
||||||
end
|
end
|
||||||
@@ -224,25 +232,25 @@ skynet.tostring = assert(c.tostring)
|
|||||||
function skynet.call(addr, typename, ...)
|
function skynet.call(addr, typename, ...)
|
||||||
local p = proto[typename]
|
local p = proto[typename]
|
||||||
local session = c.send(addr, p.id , nil , p.pack(...))
|
local session = c.send(addr, p.id , nil , p.pack(...))
|
||||||
return p.unpack(coroutine.yield("CALL", session))
|
return p.unpack(coroutine_yield("CALL", session))
|
||||||
end
|
end
|
||||||
|
|
||||||
function skynet.blockcall(addr, typename , ...)
|
function skynet.blockcall(addr, typename , ...)
|
||||||
local p = proto[typename]
|
local p = proto[typename]
|
||||||
c.command("LOCK")
|
c.command("LOCK")
|
||||||
local session = c.send(addr, p.id , nil , p.pack(...))
|
local session = c.send(addr, p.id , nil , p.pack(...))
|
||||||
return p.unpack(coroutine.yield("CALL", session))
|
return p.unpack(coroutine_yield("CALL", session))
|
||||||
end
|
end
|
||||||
|
|
||||||
function skynet.rawcall(addr, typename, msg, sz)
|
function skynet.rawcall(addr, typename, msg, sz)
|
||||||
local p = proto[typename]
|
local p = proto[typename]
|
||||||
local session = c.send(addr, p.id , nil , msg, sz)
|
local session = c.send(addr, p.id , nil , msg, sz)
|
||||||
return coroutine.yield("CALL", session)
|
return coroutine_yield("CALL", session)
|
||||||
end
|
end
|
||||||
|
|
||||||
function skynet.ret(msg, sz)
|
function skynet.ret(msg, sz)
|
||||||
msg = msg or ""
|
msg = msg or ""
|
||||||
coroutine.yield("RETURN", msg, sz)
|
coroutine_yield("RETURN", msg, sz)
|
||||||
end
|
end
|
||||||
|
|
||||||
function skynet.wakeup(co)
|
function skynet.wakeup(co)
|
||||||
|
|||||||
Reference in New Issue
Block a user