Skip to content

Commit

Permalink
perf(db) improve kong startup time by reusing luasocket on kong.init
Browse files Browse the repository at this point in the history
Without this patch the Postgres strategy creates 7 non-pooled
luasocket connections to database on Kong.init(). This patch
reduces that to 3.

Without this patch the Cassandra strategy creates 9 non-pooled
luasocket connections to database on Kong.init(). This patch
reduces that to 1.

The difference is because Postgres needs to run initialization
SQL to setup the connection timezone and database schema on new
connections. And for Postgres `connect_migrations` is the same
as `connect` while with Cassandra it is not. It is still a tiny
improvement for Postgres as well.
  • Loading branch information
bungle committed Jan 9, 2019
1 parent 250757c commit 5b6bbd7
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions kong/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ function Kong.init()
end
--]]

assert(db:connect())
assert(db.plugins:check_db_against_config(config.loaded_plugins))

-- LEGACY
Expand Down Expand Up @@ -344,19 +345,19 @@ function Kong.init()
sort_plugins_for_execution(config, db, loaded_plugins)

local err
plugins_map_semaphore, err = semaphore.new()
plugins_map_semaphore, err = semaphore.new(1) -- 1 = treat this as a mutex
if not plugins_map_semaphore then
error("failed to create plugins map semaphore: " .. err)
end

plugins_map_semaphore:post(1) -- one resource, treat this as a mutex

local _, err = build_plugins_map(db, "init")
if err then
error("error building initial plugins map: ", err)
end

assert(runloop.build_router(db, "init"))

assert(db:close())
end


Expand Down

0 comments on commit 5b6bbd7

Please sign in to comment.