From 5b6bbd7304c327b1888078e615db2decc566b1ef Mon Sep 17 00:00:00 2001 From: Aapo Talvensaari Date: Wed, 9 Jan 2019 14:03:47 +0200 Subject: [PATCH] perf(db) improve kong startup time by reusing luasocket on kong.init 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. --- kong/init.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/kong/init.lua b/kong/init.lua index 45f09fc52d25..b0acb7dc2f84 100644 --- a/kong/init.lua +++ b/kong/init.lua @@ -289,6 +289,7 @@ function Kong.init() end --]] + assert(db:connect()) assert(db.plugins:check_db_against_config(config.loaded_plugins)) -- LEGACY @@ -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