Skip to content

Commit

Permalink
perf(runloop) prewarm service dns on creation / update
Browse files Browse the repository at this point in the history
This change prewarms the DNS cache when a new Service is added or
updated.
  • Loading branch information
kikito committed May 30, 2019
1 parent f0a96ac commit 4a23ba3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
16 changes: 16 additions & 0 deletions kong/runloop/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ local function get_now()
end


local function warmup_hostname_dns_timer(premature, host)
if premature then
return
end

kong.dns.toip(host)
end


local function register_events()
-- initialize local local_events hooks
local db = kong.db
Expand Down Expand Up @@ -206,6 +215,13 @@ local function register_events()
log(DEBUG, "[events] Service updated, invalidating router")
cache:invalidate("router:version")
end

if data.operation == "create" or
data.operation == "update" then
if utils.hostname_type(data.entity.host) == "name" then
timer_at(0, warmup_hostname_dns_timer, data.entity.host)
end
end
end, "crud", "services")


Expand Down
37 changes: 37 additions & 0 deletions spec/02-integration/05-proxy/05-dns_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,43 @@ end

for _, strategy in helpers.each_strategy() do
describe("DNS [#" .. strategy .. "]", function()
describe("#db Services are warmed up on creation", function()
local mock_dns, admin_client
lazy_setup(function()
mock_dns = helpers.udp_server(10101, 2)
assert(helpers.start_kong{
database = strategy,
dns_resolver = "127.0.0.1:10101"
})
admin_client = helpers.admin_client()
end)

lazy_teardown(function()
if admin_client then
admin_client:close()
end

helpers.stop_kong()
end)

it("warms up the cache when a new service is created", function()
admin_client:post("/services", {
headers = { ["Content-Type"] = "application/json" },
body = {
name = "serv",
host = "example.net",
path = "/",
}
})

ngx.sleep(2)

local ok, res = mock_dns:join()
assert.truthy(ok)
assert.matches("example.net", table.concat(res))
end)
end)

describe("retries", function()
local retries = 3
local proxy_client
Expand Down

0 comments on commit 4a23ba3

Please sign in to comment.