Avoid too many forks by caching hasService result

This commit is contained in:
emanuele-f 2018-11-23 19:18:50 +01:00
parent d6e78848ed
commit 2414538a2a

View file

@ -75,11 +75,19 @@ end
--! @brief Check if a service is available into the system.
--! @return true if service is available, false otherwise.
function os_utils.hasService(service_name, ...)
if not ntop.exists(NTOPNG_CONFIG_TOOL) then
return false
local cmd = ntopctl_cmd(service_name, "has-service", ...)
local key = "ntopng.cache.hasService." .. cmd:gsub(" ", "_")
local rv = ntop.getCache(key)
if rv == nil then
if not ntop.exists(NTOPNG_CONFIG_TOOL) then
return false
end
local rv = os_utils.execWithOutput(cmd)
ntop.setCache(key, rv, 60)
end
local rv = os_utils.execWithOutput(ntopctl_cmd(service_name, "has-service", ...))
return(rv == "yes\n")
end