Skip to content

Commit 05d19e4

Browse files
committed
Setting socket timeout seems finicky, so defer to statement_timeout
1 parent dba5a6c commit 05d19e4

File tree

1 file changed

+4
-16
lines changed

1 file changed

+4
-16
lines changed

src/api-umbrella/utils/pg_utils.lua

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,10 @@ function _M.setup_type_casting(pg)
144144
pg:set_type_deserializer(1041, "array_string")
145145
end
146146

147-
function _M.setup_socket_timeouts(pg, statement_timeout)
148-
if not statement_timeout then
149-
statement_timeout = config["postgresql"]["statement_timeout"]
150-
end
151-
152-
-- Set socket timeouts based on the postgresql statement timeout, but defer
153-
-- to the statement timeout for better cleanup.
154-
local send_read_timeout = statement_timeout + 5000
147+
function _M.setup_socket_timeouts(pg)
148+
-- We'll assume statement_timeout should take precedence, so set a much
149+
-- longer socket timeout to work with any customized statement timeout.
150+
local send_read_timeout = 10 * 60 * 1000 -- 10 minutes
155151

156152
pg.sock:settimeouts(config["postgresql"]["connect_timeout"], send_read_timeout, send_read_timeout)
157153
end
@@ -224,13 +220,7 @@ function _M.query(query, values, options)
224220
end
225221

226222
-- Allow custom timeouts to be specified for individual queries.
227-
--
228-
-- Note: Because the socket is actually shared, only longer timeout should
229-
-- probably be passed in here, since otherwise, this timeout may still apply
230-
-- on this same socket from other async queries happening.
231223
if options and options["statement_timeout"] then
232-
_M.setup_socket_timeouts(pg, options["statement_timeout"])
233-
234224
local timeout_result, timeout_err = pg:query("SET SESSION statement_timeout = " .. _M.escape_literal(options["statement_timeout"] .. "ms"))
235225
if not timeout_result then
236226
ngx.log(ngx.ERR, "postgresql query error: ", timeout_err)
@@ -277,8 +267,6 @@ function _M.query(query, values, options)
277267

278268
-- If custom timeouts were specified, revert back to default timeouts.
279269
if options and options["statement_timeout"] then
280-
_M.setup_socket_timeouts(pg)
281-
282270
local timeout_result, timeout_err = pg:query("SET SESSION statement_timeout = " .. _M.escape_literal(config["postgresql"]["statement_timeout"] .. "ms"))
283271
if not timeout_result then
284272
ngx.log(ngx.ERR, "postgresql query error: ", timeout_err)

0 commit comments

Comments
 (0)