From cf18b59d90b7a420800dc3d728f3b64ede9a6441 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 8 Apr 2019 15:27:55 +0800 Subject: [PATCH] fix #981 --- lualib/skynet/datasheet/builder.lua | 26 +++++++++++++++++++++++++- lualib/skynet/datasheet/init.lua | 4 +--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lualib/skynet/datasheet/builder.lua b/lualib/skynet/datasheet/builder.lua index 1683a105..7e43c0e5 100644 --- a/lualib/skynet/datasheet/builder.lua +++ b/lualib/skynet/datasheet/builder.lua @@ -69,6 +69,13 @@ local skynet = require "skynet" local datasheet = {} local handles = {} -- handle:{ ref:count , name:name , collect:resp } 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 h = handles[handle] @@ -109,6 +116,7 @@ function datasheet.query(source, name) local handle = t.handle local h = handles[handle] h.ref = h.ref + 1 + customers[source][handle] = true skynet.ret(skynet.pack(handle)) end @@ -117,19 +125,35 @@ function datasheet.monitor(source, handle) local h = assert(handles[handle], "Invalid data handle") local t = dataset[h.name] if t.handle ~= handle then -- already changes + customers[source][t.handle] = true skynet.ret(skynet.pack(t.handle)) else 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 -- from customers, release handle , ref count - 1 function datasheet.release(source, handle) -- send message, don't ret + customers[source][handle] = nil releasehandle(source, handle) 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 function datasheet.collect(source, handle) local h = assert(handles[handle], "Invalid data handle") diff --git a/lualib/skynet/datasheet/init.lua b/lualib/skynet/datasheet/init.lua index c902b982..69c9c6d6 100644 --- a/lualib/skynet/datasheet/init.lua +++ b/lualib/skynet/datasheet/init.lua @@ -11,9 +11,7 @@ end) local datasheet = {} local sheets = setmetatable({}, { __gc = function(t) - for _,v in pairs(t) do - skynet.send(datasheet_svr, "lua", "release", v.handle) - end + skynet.send(datasheet_svr, "lua", "close") end, })