gluon-announce(d): preload functions, collectgarbage
By calling collectgarbage() at various places I was able to reduce heap usage by ~270kb.
This commit is contained in:
parent
435ded0c60
commit
9dffb1284f
@ -19,7 +19,9 @@ function collect_dir(dir)
|
|||||||
|
|
||||||
for entry in fs.dir(dir) do
|
for entry in fs.dir(dir) do
|
||||||
if entry:sub(1, 1) ~= '.' then
|
if entry:sub(1, 1) ~= '.' then
|
||||||
|
collectgarbage()
|
||||||
local fn, err = collect_entry(dir .. '/' .. entry)
|
local fn, err = collect_entry(dir .. '/' .. entry)
|
||||||
|
|
||||||
if fn then
|
if fn then
|
||||||
fns[entry] = fn
|
fns[entry] = fn
|
||||||
else
|
else
|
||||||
@ -32,7 +34,9 @@ function collect_dir(dir)
|
|||||||
local ret = { [{}] = true }
|
local ret = { [{}] = true }
|
||||||
|
|
||||||
for k, v in pairs(fns) do
|
for k, v in pairs(fns) do
|
||||||
|
collectgarbage()
|
||||||
local ok, val = pcall(setfenv(v, _M))
|
local ok, val = pcall(setfenv(v, _M))
|
||||||
|
|
||||||
if ok then
|
if ok then
|
||||||
ret[k] = val
|
ret[k] = val
|
||||||
else
|
else
|
||||||
@ -40,6 +44,8 @@ function collect_dir(dir)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
collectgarbage()
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,26 +1,29 @@
|
|||||||
local announce = require 'gluon.announce'
|
local announce = require 'gluon.announce'
|
||||||
local deflate = require 'deflate'
|
local deflate = require 'deflate'
|
||||||
local json = require 'luci.jsonc'
|
local json = require 'luci.jsonc'
|
||||||
|
local nixio = require 'nixio'
|
||||||
|
local fs = require 'nixio.fs'
|
||||||
|
|
||||||
local memoize = {}
|
local memoize = {}
|
||||||
|
|
||||||
local function collect(type)
|
nixio.chdir('/lib/gluon/announce/')
|
||||||
if not memoize[type] then
|
|
||||||
memoize[type] = announce.collect_dir('/lib/gluon/announce/' .. type .. '.d')
|
|
||||||
end
|
|
||||||
|
|
||||||
return memoize[type]()
|
for dir in fs.glob('*.d') do
|
||||||
|
local name = dir:sub(1, -3)
|
||||||
|
memoize[name] = announce.collect_dir(dir)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function collect(type)
|
||||||
|
return memoize[type] and memoize[type]()
|
||||||
|
end
|
||||||
|
|
||||||
module('gluon.announced', package.seeall)
|
module('gluon.announced', package.seeall)
|
||||||
|
|
||||||
function handle_request(query)
|
function handle_request(query)
|
||||||
if query:match('^nodeinfo$') then
|
collectgarbage()
|
||||||
return json.stringify(collect('nodeinfo'))
|
|
||||||
end
|
|
||||||
|
|
||||||
local m = query:match('^GET ([a-z ]+)$')
|
local m = query:match('^GET ([a-z ]+)$')
|
||||||
|
local ret
|
||||||
if m then
|
if m then
|
||||||
local data = {}
|
local data = {}
|
||||||
|
|
||||||
@ -32,7 +35,16 @@ function handle_request(query)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if next(data) then
|
if next(data) then
|
||||||
return deflate.compress(json.stringify(data))
|
ret = deflate.compress(json.stringify(data))
|
||||||
|
end
|
||||||
|
elseif query:match('^[a-z]+$') then
|
||||||
|
local ok, data = pcall(collect, query)
|
||||||
|
if ok then
|
||||||
|
ret = json.stringify(data)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
collectgarbage()
|
||||||
|
|
||||||
|
return ret
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user