Skip to content

Commit

Permalink
feat(db) add pg_timeout configuration parameter to postgres strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
bungle committed Sep 28, 2018
1 parent 3e6c7d0 commit 6dc0229
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions kong.conf.default
Expand Up @@ -332,6 +332,9 @@

#pg_host = 127.0.0.1 # The PostgreSQL host to connect to.
#pg_port = 5432 # The port to connect to.
#pg_timeout = 5000 # Defines the timeout (in ms), for connecting,
# reading and writing.

#pg_user = kong # The username to authenticate if required.
#pg_password = # The password to authenticate if required.
#pg_database = kong # The database name to connect to.
Expand Down
4 changes: 3 additions & 1 deletion kong/cmd/migrations.lua
Expand Up @@ -74,9 +74,11 @@ local function execute(args)
end

local conf = assert(conf_loader(args.conf))

conf.pg_timeout = args.db_timeout -- connect + send + read

conf.cassandra_timeout = args.db_timeout -- connect + send + read
conf.cassandra_schema_consensus_timeout = args.db_timeout
-- TODO: no support for custom pgmoon timeout

local db = DB.new(conf)
assert(db:init_connector())
Expand Down
3 changes: 2 additions & 1 deletion kong/cmd/start.lua
Expand Up @@ -15,9 +15,10 @@ local function execute(args)
prefix = args.prefix
}))

conf.pg_timeout = args.db_timeout -- connect + send + read

conf.cassandra_timeout = args.db_timeout -- connect + send + read
conf.cassandra_schema_consensus_timeout = args.db_timeout
-- TODO: no support for custom pgmoon timeout

assert(not kill.is_running(conf.nginx_pid),
"Kong is already running in " .. conf.prefix)
Expand Down
1 change: 1 addition & 0 deletions kong/conf_loader.lua
Expand Up @@ -104,6 +104,7 @@ local CONF_INFERENCES = {

database = { enum = { "postgres", "cassandra" } },
pg_port = { typ = "number" },
pg_timeout = { typ = "number" },
pg_password = { typ = "string" },
pg_ssl = { typ = "boolean" },
pg_ssl_verify = { typ = "boolean" },
Expand Down
10 changes: 10 additions & 0 deletions kong/dao/db/postgres.lua
Expand Up @@ -58,6 +58,7 @@ function _M.new(kong_config)
self.query_options = {
host = kong_config.pg_host,
port = kong_config.pg_port,
timeout = kong_config.pg_timeout,
user = kong_config.pg_user,
password = kong_config.pg_password,
database = kong_config.pg_database,
Expand Down Expand Up @@ -354,6 +355,11 @@ end
function _M:query(query, schema)
local conn_opts = query_opts(self)
local pg = pgmoon.new(conn_opts)

if conn_opts.timeout then
pg:settimeout(conn_opts.timeout)
end

local ok, err = pg:connect()
if not ok then
return nil, Errors.db(err)
Expand Down Expand Up @@ -622,6 +628,10 @@ function _M:reachable()
local conn_opts = query_opts(self)
local pg = pgmoon.new(conn_opts)

if conn_opts.timeout then
pg:settimeout(conn_opts.timeout)
end

local ok, err = pg:connect()
if not ok then
return nil, Errors.db(err)
Expand Down
5 changes: 5 additions & 0 deletions kong/db/strategies/postgres/connector.lua
Expand Up @@ -138,6 +138,10 @@ local function connect(config)
connection.convert_null = true
connection.NULL = null

if config.timeout then
connection:settimeout(config.timeout)
end

local ok, err = connection:connect()
if not ok then
return nil, err
Expand Down Expand Up @@ -761,6 +765,7 @@ function _M.new(kong_config)
local config = {
host = kong_config.pg_host,
port = kong_config.pg_port,
timeout = kong_config.pg_timeout,
user = kong_config.pg_user,
password = kong_config.pg_password,
database = kong_config.pg_database,
Expand Down
1 change: 1 addition & 0 deletions kong/templates/kong_defaults.lua
Expand Up @@ -39,6 +39,7 @@ database = postgres
pg_host = 127.0.0.1
pg_port = 5432
pg_database = kong
pg_timeout = 5000
pg_user = kong
pg_password = NONE
pg_ssl = off
Expand Down
1 change: 1 addition & 0 deletions spec-old-api/kong_tests.conf
Expand Up @@ -12,6 +12,7 @@ dns_resolver = 8.8.8.8
database = postgres
pg_host = 127.0.0.1
pg_port = 5432
pg_timeout = 10000
pg_database = kong_tests
cassandra_keyspace = kong_tests
cassandra_timeout = 10000
Expand Down
3 changes: 3 additions & 0 deletions spec/02-integration/03-dao/02-migrations_spec.lua
Expand Up @@ -137,16 +137,19 @@ helpers.for_each_dao(function(kong_config)
describe("errors", function()
it("returns errors prefixed by the DB type in __tostring()", function()
local pg_port = kong_config.pg_port
local pg_timeout = kong_config.pg_timeout
local cassandra_port = kong_config.cassandra_port
local cassandra_timeout = kong_config.cassandra_timeout
finally(function()
kong_config.pg_port = pg_port
kong_config.pg_timeout = pg_timeout
kong_config.cassandra_port = cassandra_port
kong_config.cassandra_timeout = cassandra_timeout
ngx.shared.kong_cassandra:flush_all()
ngx.shared.kong_cassandra:flush_expired()
end)
kong_config.pg_port = 3333
kong_config.pg_timeout = 1000
kong_config.cassandra_port = 3333
kong_config.cassandra_timeout = 1000

Expand Down
3 changes: 3 additions & 0 deletions spec/02-integration/03-dao/03-crud_spec.lua
Expand Up @@ -771,16 +771,19 @@ helpers.for_each_dao(function(kong_config)
describe("errors", function()
it("returns errors prefixed by the DB type in __tostring()", function()
local pg_port = kong_config.pg_port
local pg_timeout = kong_config.pg_timeout
local cassandra_port = kong_config.cassandra_port
local cassandra_timeout = kong_config.cassandra_timeout
finally(function()
kong_config.pg_port = pg_port
kong_config.pg_timeout = pg_timeout
kong_config.cassandra_port = cassandra_port
kong_config.cassandra_timeout = cassandra_timeout
ngx.shared.kong_cassandra:flush_all()
ngx.shared.kong_cassandra:flush_expired()
end)
kong_config.pg_port = 3333
kong_config.pg_timeout = 1000
kong_config.cassandra_port = 3333
kong_config.cassandra_timeout = 1000

Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/headers.conf
Expand Up @@ -12,6 +12,7 @@ dns_resolver = 8.8.8.8
database = postgres
pg_host = 127.0.0.1
pg_port = 5432
pg_timeout = 10000
pg_database = kong_tests
cassandra_keyspace = kong_tests
cassandra_timeout = 10000
Expand Down
1 change: 1 addition & 0 deletions spec/kong_tests.conf
Expand Up @@ -12,6 +12,7 @@ dns_resolver = 8.8.8.8
database = postgres
pg_host = 127.0.0.1
pg_port = 5432
pg_timeout = 10000
pg_database = kong_tests
cassandra_keyspace = kong_tests
cassandra_timeout = 10000
Expand Down

0 comments on commit 6dc0229

Please sign in to comment.