Skip to content

Commit

Permalink
Merge pull request Kong#1019 from Mashape/chore/serf
Browse files Browse the repository at this point in the history
Improving the performance of Serf
  • Loading branch information
subnetmarco committed Feb 25, 2016
2 parents 59f2791 + 57e26eb commit abb9dc5
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
7 changes: 3 additions & 4 deletions kong/api/routes/cluster.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
11 changes: 7 additions & 4 deletions kong/cli/services/base_service.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion kong/cli/services/serf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 0 additions & 2 deletions kong/core/cluster.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))
Expand Down
8 changes: 1 addition & 7 deletions kong/core/hooks.lua
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions kong/kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit abb9dc5

Please sign in to comment.