Merge branch 'hongling0-master'

This commit is contained in:
Cloud Wu
2018-02-05 11:38:22 +08:00

View File

@@ -70,7 +70,7 @@ 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 function releasehandle(handle) local function releasehandle(source, handle)
local h = handles[handle] local h = handles[handle]
h.ref = h.ref - 1 h.ref = h.ref - 1
if h.ref == 0 and h.collect then if h.ref == 0 and h.collect then
@@ -78,62 +78,33 @@ local function releasehandle(handle)
h.collect = nil h.collect = nil
handles[handle] = nil handles[handle] = nil
end end
end local t=dataset[h.name]
t.monitor[source]=nil
local function register_collector(name)
local function collect()
local t = dataset[name]
local m = t.monitor
local i = 1
while m[i] do
if not m[i] "TEST" then
local n = #m
m[i] = m[n]
m[n] = nil
releasehandle(t.handle)
else
i = i + 1
end
end
skynet.timeout(10 * 60 * 100, collect) -- 10 mins
end
collect()
end end
-- from builder, create or update handle -- from builder, create or update handle
function datasheet.update(name, handle) function datasheet.update(source, name, handle)
local t = dataset[name] local t = dataset[name]
if not t then if not t then
-- new datasheet -- new datasheet
t = { handle = handle, monitor = {} } t = { handle = handle, monitor = {} }
dataset[name] = t dataset[name] = t
handles[handle] = { ref = 1, name = name } handles[handle] = { ref = 1, name = name }
register_collector(name)
else else
local old_handle = t.handle
t.handle = handle
-- report update to customers -- report update to customers
handles[handle] = { handles[handle] = { ref = handles[t.handle].ref, name = name }
ref = nil, t.handle = handle
name = name
}
local ref = 1
for k,v in ipairs(t.monitor) do for k,v in pairs(t.monitor) do
if v(true, handle) then v(true, handle)
ref = ref + 1
else
releasehandle(old_handle)
end
t.monitor[k] = nil t.monitor[k] = nil
end end
handles[handle].ref = ref
end end
skynet.ret() skynet.ret()
end end
-- from customers -- from customers
function datasheet.query(name) function datasheet.query(source, name)
local t = assert(dataset[name], "create data first") local t = assert(dataset[name], "create data first")
local handle = t.handle local handle = t.handle
local h = handles[handle] local h = handles[handle]
@@ -142,25 +113,25 @@ function datasheet.query(name)
end end
-- from customers, monitor handle change -- from customers, monitor handle change
function datasheet.monitor(handle) 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
skynet.ret(skynet.pack(t.handle)) skynet.ret(skynet.pack(t.handle))
else else
h.ref = h.ref + 1 assert(not t.monitor[source])
table.insert(t.monitor, skynet.response()) t.monitor[source]=skynet.response()
end end
end end
-- from customers, release handle , ref count - 1 -- from customers, release handle , ref count - 1
function datasheet.release(handle) function datasheet.release(source, handle)
-- send message, don't ret -- send message, don't ret
releasehandle(handle) releasehandle(source, handle)
end end
-- from builder, monitor handle release -- from builder, monitor handle release
function datasheet.collect(handle) function datasheet.collect(source, handle)
local h = assert(handles[handle], "Invalid data handle") local h = assert(handles[handle], "Invalid data handle")
if h.ref == 0 then if h.ref == 0 then
handles[handle] = nil handles[handle] = nil
@@ -171,8 +142,8 @@ function datasheet.collect(handle)
end end
end end
skynet.dispatch("lua", function(_,_,cmd,...) skynet.dispatch("lua", function(_,source,cmd,...)
datasheet[cmd](...) datasheet[cmd](source,...)
end) end)
skynet.info_func(function() skynet.info_func(function()
@@ -186,7 +157,7 @@ skynet.info_func(function()
tmp[v.handle] = nil tmp[v.handle] = nil
info[k] = { info[k] = {
handle = v.handle, handle = v.handle,
monitors = #v.monitor, monitors = h.ref,
} }
end end
for k,v in pairs(tmp) do for k,v in pairs(tmp) do