From 57e26eb207362f940e927d2dc0a580a926c8f9dd Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 24 Feb 2016 15:59:24 -0800 Subject: [PATCH] Improving the performance of Serf --- .luacheckrc | 2 +- kong/api/routes/cluster.lua | 7 +++---- kong/cli/services/base_service.lua | 11 +++++++---- kong/cli/services/serf.lua | 2 +- kong/core/cluster.lua | 2 -- kong/core/hooks.lua | 8 +------- kong/kong.lua | 2 ++ 7 files changed, 15 insertions(+), 19 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index 9381fe661136..ca4bedfbc337 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -1,6 +1,6 @@ redefined = false unused_args = false -globals = {"ngx", "dao", "app", "configuration", "events"} +globals = {"ngx", "dao", "app", "configuration", "events", "serf"} files["kong/"] = { std = "luajit" diff --git a/kong/api/routes/cluster.lua b/kong/api/routes/cluster.lua index abeb9adbbeb2..b2cb79d46c50 100644 --- a/kong/api/routes/cluster.lua +++ b/kong/api/routes/cluster.lua @@ -1,5 +1,4 @@ local responses = require "kong.tools.responses" -local Serf = require "kong.cli.services.serf" local pairs = pairs local table_insert = table.insert @@ -8,7 +7,7 @@ local string_upper = string.upper return { ["/cluster/"] = { GET = function(self, dao_factory, helpers) - local members, err = Serf(configuration):_members() + local members, err = serf:_members() if err then return responses.send_HTTP_INTERNAL_SERVER_ERROR(err) end @@ -33,7 +32,7 @@ return { return responses.send_HTTP_BAD_REQUEST("Missing node \"name\"") end - local _, err = Serf(configuration):invoke_signal("force-leave", {self.params.name}) + local _, err = serf:invoke_signal("force-leave", {self.params.name}) if err then return responses.send_HTTP_BAD_REQUEST(err) end @@ -46,7 +45,7 @@ return { return responses.send_HTTP_BAD_REQUEST("Missing node \"address\"") end - local _, err = Serf(configuration):invoke_signal("join", {self.params.address}) + local _, err = serf:invoke_signal("join", {self.params.address}) if err then return responses.send_HTTP_BAD_REQUEST(err) end diff --git a/kong/cli/services/base_service.lua b/kong/cli/services/base_service.lua index 171a68666fb2..a8b77b8b8f51 100644 --- a/kong/cli/services/base_service.lua +++ b/kong/cli/services/base_service.lua @@ -68,11 +68,14 @@ function BaseService:is_running() end function BaseService:_get_cmd(additional_paths, check_path_func) - local cmd = BaseService.find_cmd(self._name, additional_paths, check_path_func) - if not cmd then - return nil, "Can't find "..self._name + if not self._cmd then -- Cache the command after the first time + local cmd = BaseService.find_cmd(self._name, additional_paths, check_path_func) + if not cmd then + return nil, "Can't find "..self._name + end + self._cmd = cmd end - return cmd + return self._cmd end function BaseService:start() diff --git a/kong/cli/services/serf.lua b/kong/cli/services/serf.lua index 5fbf93934aec..a5dfcb3ae89a 100644 --- a/kong/cli/services/serf.lua +++ b/kong/cli/services/serf.lua @@ -257,7 +257,7 @@ function Serf:event(t_payload) return false, "Encoded payload is "..string.len(encoded_payload).." and it exceeds the limit of 512 bytes!" end - return self:invoke_signal("event "..tostring(args).." kong", {"'"..encoded_payload.."'"}, true) + return self:invoke_signal("event "..tostring(args).." kong", {"'"..encoded_payload.."'", "&"}, true) end function Serf:stop() diff --git a/kong/core/cluster.lua b/kong/core/cluster.lua index 978d30a559ca..bb69b1565418 100644 --- a/kong/core/cluster.lua +++ b/kong/core/cluster.lua @@ -1,5 +1,4 @@ local cluster_utils = require "kong.tools.cluster" -local Serf = require "kong.cli.services.serf" local cache = require "kong.tools.database_cache" local resty_lock @@ -38,7 +37,6 @@ local function async_autojoin(premature) if err then ngx.log(ngx.ERR, tostring(err)) elseif count > 1 then - local serf = Serf(configuration) local members, err = serf:_members() if err then ngx.log(ngx.ERR, tostring(err)) diff --git a/kong/core/hooks.lua b/kong/core/hooks.lua index 4cc84f9c6b36..e2afc4864146 100644 --- a/kong/core/hooks.lua +++ b/kong/core/hooks.lua @@ -1,7 +1,6 @@ local events = require "kong.core.events" local cache = require "kong.tools.database_cache" local stringy = require "stringy" -local Serf = require "kong.cli.services.serf" local function invalidate_plugin(entity) cache.delete(cache.plugin_key(entity.name, entity.api_id, entity.consumer_id)) @@ -22,7 +21,6 @@ local function invalidate(message_t) end local function get_cluster_members() - local serf = require("kong.cli.services.serf")(configuration) local members, err = serf:_members() if err then ngx.log(ngx.ERR, err) @@ -128,11 +126,7 @@ return { invalidate(message_t) end, [events.TYPES.CLUSTER_PROPAGATE] = function(message_t) - local serf = Serf(configuration) - local ok, err = serf:event(message_t) - if not ok then - ngx.log(ngx.ERR, err) - end + serf:event(message_t) end, [events.TYPES["MEMBER-JOIN"]] = function(message_t) member_join(message_t) diff --git a/kong/kong.lua b/kong/kong.lua index a915aa8d23a6..4157cab4d3df 100644 --- a/kong/kong.lua +++ b/kong/kong.lua @@ -30,6 +30,7 @@ local dao_loader = require "kong.tools.dao_loader" local config_loader = require "kong.tools.config_loader" local plugins_iterator = require "kong.core.plugins_iterator" local Events = require "kong.core.events" +local Serf = require "kong.cli.services.serf" local ipairs = ipairs local table_insert = table.insert @@ -127,6 +128,7 @@ local Kong = {} function Kong.init() local status, err = pcall(function() configuration = config_loader.load(os.getenv("KONG_CONF")) + serf = Serf(configuration) events = Events() dao = dao_loader.load(configuration, true, events) loaded_plugins = load_node_plugins(configuration)