mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-23 19:43:09 +00:00
add sharedata.flush() to collect old version immediately
This commit is contained in:
@@ -8,6 +8,7 @@ skynet.init(function()
|
||||
end)
|
||||
|
||||
local sharedata = {}
|
||||
local cache = setmetatable({}, { __mode = "kv" })
|
||||
|
||||
local function monitor(name, obj, cobj)
|
||||
local newobj = cobj
|
||||
@@ -18,13 +19,20 @@ local function monitor(name, obj, cobj)
|
||||
end
|
||||
sd.update(obj, newobj)
|
||||
end
|
||||
if cache[name] == obj then
|
||||
cache[name] = nil
|
||||
end
|
||||
end
|
||||
|
||||
function sharedata.query(name)
|
||||
if cache[name] then
|
||||
return cache[name]
|
||||
end
|
||||
local obj = skynet.call(service, "lua", "query", name)
|
||||
local r = sd.box(obj)
|
||||
skynet.send(service, "lua", "confirm" , obj)
|
||||
skynet.fork(monitor,name, r, obj)
|
||||
cache[name] = r
|
||||
return r
|
||||
end
|
||||
|
||||
@@ -40,4 +48,11 @@ function sharedata.delete(name)
|
||||
skynet.call(service, "lua", "delete", name)
|
||||
end
|
||||
|
||||
function sharedata.flush()
|
||||
for name, obj in pairs(cache) do
|
||||
sd.flush(obj)
|
||||
end
|
||||
collectgarbage()
|
||||
end
|
||||
|
||||
return sharedata
|
||||
|
||||
@@ -128,4 +128,8 @@ function conf.update(self, pointer)
|
||||
core.update(self.__gcobj, pointer, { __gcobj = core.box(pointer) })
|
||||
end
|
||||
|
||||
return conf
|
||||
function conf.flush(obj)
|
||||
getcobj(obj)
|
||||
end
|
||||
|
||||
return conf
|
||||
|
||||
@@ -8,6 +8,7 @@ local NORET = {}
|
||||
local pool = {}
|
||||
local pool_count = {}
|
||||
local objmap = {}
|
||||
local collect_tick = 600
|
||||
|
||||
local function newobj(name, tbl)
|
||||
assert(pool[name] == nil)
|
||||
@@ -19,17 +20,28 @@ local function newobj(name, tbl)
|
||||
pool_count[name] = { n = 0, threshold = 16 }
|
||||
end
|
||||
|
||||
local function collect10sec()
|
||||
if collect_tick > 10 then
|
||||
collect_tick = 10
|
||||
end
|
||||
end
|
||||
|
||||
local function collectobj()
|
||||
while true do
|
||||
skynet.sleep(600 * 100) -- sleep 10 min
|
||||
collectgarbage()
|
||||
for obj, v in pairs(objmap) do
|
||||
if v == true then
|
||||
if sharedata.host.getref(obj) <= 0 then
|
||||
objmap[obj] = nil
|
||||
sharedata.host.delete(obj)
|
||||
skynet.sleep(100) -- sleep 1s
|
||||
if collect_tick <= 0 then
|
||||
collect_tick = 600 -- reset tick count to 600 sec
|
||||
collectgarbage()
|
||||
for obj, v in pairs(objmap) do
|
||||
if v == true then
|
||||
if sharedata.host.getref(obj) <= 0 then
|
||||
objmap[obj] = nil
|
||||
sharedata.host.delete(obj)
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
collect_tick = collect_tick - 1
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -109,6 +121,7 @@ function CMD.update(name, t, ...)
|
||||
response(true, newobj)
|
||||
end
|
||||
end
|
||||
collect10sec() -- collect in 10 sec
|
||||
end
|
||||
|
||||
local function check_watch(queue)
|
||||
|
||||
Reference in New Issue
Block a user