Skip to content

Commit

Permalink
chore(db) connector:close and connector:setkeepalive cleanup
Browse files Browse the repository at this point in the history
Make both Postgres and Cassandra database connectors behave
similarly on `connector:close` and `connetor:setkeepalive`.

Also adds tests for the mentioned methods + :connect,
including tests with ssl connection.
  • Loading branch information
bungle committed Jan 18, 2019
1 parent 80703aa commit c81d2c9
Show file tree
Hide file tree
Showing 3 changed files with 590 additions and 48 deletions.
4 changes: 2 additions & 2 deletions kong/db/strategies/cassandra/connector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ function CassandraConnector:setkeepalive()
return true
end

local ok, err = conn:setkeepalive()
local _, err = conn:setkeepalive()

self:store_connection(nil)

if not ok then
if err then
return nil, err
end

Expand Down
54 changes: 9 additions & 45 deletions kong/db/strategies/postgres/connector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,56 +171,20 @@ local function connect(config)
end


local function close(connection)
if not connection or not connection.sock then
return nil, "no active connection"
end

local ok, err = connection:disconnect()
if not ok then
if err then
log(WARN, "unable to close postgres connection (", err, ")")

else
log(WARN, "unable to close postgres connection")
end

return nil, err
end

return true
end


setkeepalive = function(connection)
if not connection or not connection.sock then
return nil, "no active connection"
return true
end

local ok, err
if connection.sock_type == "luasocket" then
ok, err = connection:disconnect()
if not ok then
if err then
log(WARN, "unable to close postgres connection (", err, ")")

else
log(WARN, "unable to close postgres connection")
end

local _, err = connection:disconnect()
if err then
return nil, err
end

else
ok, err = connection:keepalive()
if not ok then
if err then
log(WARN, "unable to set keepalive for postgres connection (", err, ")")

else
log(WARN, "unable to set keepalive for postgres connection")
end

local _, err = connection:keepalive()
if err then
return nil, err
end
end
Expand Down Expand Up @@ -373,11 +337,11 @@ function _mt:close()
return true
end

local ok, err = close(conn)
local _, err = conn:disconnect()

self:store_connection(nil)

if not ok then
if err then
return nil, err
end

Expand All @@ -391,11 +355,11 @@ function _mt:setkeepalive()
return true
end

local ok, err = setkeepalive(conn)
local _, err = setkeepalive(conn)

self:store_connection(nil)

if not ok then
if err then
return nil, err
end

Expand Down
Loading

0 comments on commit c81d2c9

Please sign in to comment.