Skip to content

Commit

Permalink
Revert "Revert "Revert "fix(postgres): close socket actively when tim…
Browse files Browse the repository at this point in the history
…eout happens during query (#11480)"""

This reverts commit 5b6d932.
  • Loading branch information
AndyZhang0707 committed Jul 18, 2024
1 parent dc8e87b commit 8e4c60a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 75 deletions.
7 changes: 0 additions & 7 deletions CHANGELOG/unreleased/kong/11480.yaml

This file was deleted.

34 changes: 10 additions & 24 deletions kong/db/strategies/postgres/connector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,6 @@ function _mt:query(sql, operation)
operation = "write"
end

local conn, is_new_conn
local res, err, partial, num_queries

local ok
Expand All @@ -506,36 +505,23 @@ function _mt:query(sql, operation)
return nil, "error acquiring query semaphore: " .. err
end

conn = self:get_stored_connection(operation)
if not conn then
local conn = self:get_stored_connection(operation)
if conn then
res, err, partial, num_queries = conn:query(sql)

else
local connection
local config = operation == "write" and self.config or self.config_ro

conn, err = connect(config)
if not conn then
connection, err = connect(config)
if not connection then
self:release_query_semaphore_resource(operation)
return nil, err
end
is_new_conn = true
end

res, err, partial, num_queries = conn:query(sql)

-- if err is string then either it is a SQL error
-- or it is a socket error, here we abort connections
-- that encounter errors instead of reusing them, for
-- safety reason
if err and type(err) == "string" then
ngx.log(ngx.DEBUG, "SQL query throw error: ", err, ", close connection")
local _, err = conn:disconnect()
if err then
-- We're at the end of the query - just logging if
-- we cannot cleanup the connection
ngx.log(ngx.ERR, "failed to disconnect: ", err)
end
self.store_connection(nil, operation)
res, err, partial, num_queries = connection:query(sql)

elseif is_new_conn then
setkeepalive(conn)
setkeepalive(connection)
end

self:release_query_semaphore_resource(operation)
Expand Down
44 changes: 0 additions & 44 deletions spec/02-integration/03-db/01-db_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -447,50 +447,6 @@ for _, strategy in helpers.each_strategy() do
end)
end)

describe("#testme :query() [#" .. strategy .. "]", function()
lazy_setup(function()
helpers.get_db_utils(strategy, {})
end)

postgres_only("establish new connection when error occurred", function()
ngx.IS_CLI = false

local conf = utils.deep_copy(helpers.test_conf)
conf.pg_ro_host = conf.pg_host
conf.pg_ro_user = conf.pg_user

local db, err = DB.new(conf, strategy)

assert.is_nil(err)
assert.is_table(db)
assert(db:init_connector())
assert(db:connect())

local res, err = db.connector:query("SELECT now();")
assert.not_nil(res)
assert.is_nil(err)

local old_conn = db.connector:get_stored_connection("write")
assert.not_nil(old_conn)

local res, err = db.connector:query("SELECT * FROM not_exist_table;")
assert.is_nil(res)
assert.not_nil(err)

local new_conn = db.connector:get_stored_connection("write")
assert.is_nil(new_conn)

local res, err = db.connector:query("SELECT now();")
assert.not_nil(res)
assert.is_nil(err)

local res, err = db.connector:query("SELECT now();")
assert.not_nil(res)
assert.is_nil(err)

assert(db:close())
end)
end)

describe(":setkeepalive() [#" .. strategy .. "]", function()
lazy_setup(function()
Expand Down

0 comments on commit 8e4c60a

Please sign in to comment.