This commit is contained in:
Cloud Wu
2019-04-08 15:27:55 +08:00
committed by 云风
parent fbd932e846
commit cf18b59d90
2 changed files with 26 additions and 4 deletions

View File

@@ -69,6 +69,13 @@ local skynet = require "skynet"
local datasheet = {} local datasheet = {}
local handles = {} -- handle:{ ref:count , name:name , collect:resp } local handles = {} -- handle:{ ref:count , name:name , collect:resp }
local dataset = {} -- name:{ handle:handle, monitor:{monitors queue} } local dataset = {} -- name:{ handle:handle, monitor:{monitors queue} }
local customers = {} -- source: { handle:true }
setmetatable(customers, { __index = function(c, source)
local v = {}
c[source] = v
return v
end } )
local function releasehandle(source, handle) local function releasehandle(source, handle)
local h = handles[handle] local h = handles[handle]
@@ -109,6 +116,7 @@ function datasheet.query(source, name)
local handle = t.handle local handle = t.handle
local h = handles[handle] local h = handles[handle]
h.ref = h.ref + 1 h.ref = h.ref + 1
customers[source][handle] = true
skynet.ret(skynet.pack(handle)) skynet.ret(skynet.pack(handle))
end end
@@ -117,19 +125,35 @@ function datasheet.monitor(source, handle)
local h = assert(handles[handle], "Invalid data handle") local h = assert(handles[handle], "Invalid data handle")
local t = dataset[h.name] local t = dataset[h.name]
if t.handle ~= handle then -- already changes if t.handle ~= handle then -- already changes
customers[source][t.handle] = true
skynet.ret(skynet.pack(t.handle)) skynet.ret(skynet.pack(t.handle))
else else
assert(not t.monitor[source]) assert(not t.monitor[source])
t.monitor[source]=skynet.response() local resp = skynet.response()
t.monitor[source]= function(ok, handle)
if ok then
customers[source][handle] = true
end
resp(ok, handle)
end
end end
end end
-- from customers, release handle , ref count - 1 -- from customers, release handle , ref count - 1
function datasheet.release(source, handle) function datasheet.release(source, handle)
-- send message, don't ret -- send message, don't ret
customers[source][handle] = nil
releasehandle(source, handle) releasehandle(source, handle)
end end
-- customer closed, clear all handles it queried
function datasheet.close(source)
for handle in pairs(customers[source]) do
releasehandle(source, handle)
end
customers[source] = nil
end
-- from builder, monitor handle release -- from builder, monitor handle release
function datasheet.collect(source, handle) function datasheet.collect(source, handle)
local h = assert(handles[handle], "Invalid data handle") local h = assert(handles[handle], "Invalid data handle")

View File

@@ -11,9 +11,7 @@ end)
local datasheet = {} local datasheet = {}
local sheets = setmetatable({}, { local sheets = setmetatable({}, {
__gc = function(t) __gc = function(t)
for _,v in pairs(t) do skynet.send(datasheet_svr, "lua", "close")
skynet.send(datasheet_svr, "lua", "release", v.handle)
end
end, end,
}) })