diff --git a/gateway/src/resty/http/proxy.lua b/gateway/src/resty/http/proxy.lua index 3b0b1d0f3..eccdcd4c4 100644 --- a/gateway/src/resty/http/proxy.lua +++ b/gateway/src/resty/http/proxy.lua @@ -134,8 +134,21 @@ end local function connect(request) request = request or { } - local opts = { timeouts = request.upstream_connection_opts } - local httpc = http.new(opts) + local httpc = http.new() + + if request.upstream_connection_opts then + local con_opts = request.upstream_connection_opts + ngx.log(ngx.DEBUG, 'setting timeouts (secs), connect_timeout: ', con_opts.connect_timeout, + ' send_timeout: ', con_opts.send_timeout, ' read_timeout: ', con_opts.read_timeout) + -- lua-resty-http uses nginx API for lua sockets + -- in milliseconds + -- https://github.com/openresty/lua-nginx-module?tab=readme-ov-file#tcpsocksettimeouts + local connect_timeout = con_opts.connect_timeout and con_opts.connect_timeout * 1000 + local send_timeout = con_opts.send_timeout and con_opts.send_timeout * 1000 + local read_timeout = con_opts.read_timeout and con_opts.read_timeout * 1000 + httpc:set_timeouts(connect_timeout, send_timeout, read_timeout) + end + local proxy_uri = find_proxy_url(request) request.ssl_verify = request.options and request.options.ssl and request.options.ssl.verify diff --git a/gateway/src/resty/resolver/http.lua b/gateway/src/resty/resolver/http.lua index b58c62d45..4af497855 100644 --- a/gateway/src/resty/resolver/http.lua +++ b/gateway/src/resty/resolver/http.lua @@ -8,23 +8,9 @@ local _M = setmetatable({}, { __index = resty_http }) local mt = { __index = _M } -function _M.new(opts) - opts = opts or { } +function _M.new() local http = resty_http:new() - local timeouts = opts.timeouts - if timeouts then - ngx.log(ngx.DEBUG, 'setting timeouts (secs), connect_timeout: ', timeouts.connect_timeout, - ' send_timeout: ', timeouts.send_timeout, ' read_timeout: ', timeouts.read_timeout) - -- lua-resty-http uses nginx API for lua sockets - -- in milliseconds - -- https://github.com/openresty/lua-nginx-module?tab=readme-ov-file#tcpsocksettimeouts - local connect_timeout = timeouts.connect_timeout and timeouts.connect_timeout * 1000 - local send_timeout = timeouts.send_timeout and timeouts.send_timeout * 1000 - local read_timeout = timeouts.read_timeout and timeouts.read_timeout * 1000 - http:set_timeouts(connect_timeout, send_timeout, read_timeout) - end - http.resolver = resty_resolver:instance() http.balancer = round_robin.new() diff --git a/spec/resty/http/proxy_spec.lua b/spec/resty/http/proxy_spec.lua index fef47fdf0..0967ac9bf 100644 --- a/spec/resty/http/proxy_spec.lua +++ b/spec/resty/http/proxy_spec.lua @@ -47,16 +47,37 @@ describe('resty.http.proxy', function() end) context('.new', function() - it('connects to the #http_proxy', function() - _M:reset({ http_proxy = 'http://127.0.0.1:1984' }) + before_each(function() + _M:reset({ http_proxy = 'http://127.0.0.1:1984' }) + end) - local request = { url = 'http://upstream:8091/request', method = 'GET' } - local proxy = assert(_M.new(request)) + it('connects to the #http_proxy', function() + local request = { url = 'http://upstream:8091/request', method = 'GET' } + local proxy = assert(_M.new(request)) - local res = assert(proxy:request(request)) + local res = assert(proxy:request(request)) - assert.same(200, res.status) - assert.match('GET http://upstream:8091/request HTTP/1.1', res:read_body()) - end) + assert.same(200, res.status) + assert.match('GET http://upstream:8091/request HTTP/1.1', res:read_body()) + end) + + it('connects to the #http_proxy with timeouts', function() + local request = { + url = 'http://upstream:8091/request', + method = 'GET', + upstream_connection_opts = { + connect_timeout = 1, + send_timeout = 1, + read_timeout = 1 + } + } + + local proxy = assert(_M.new(request)) + + local res = assert(proxy:request(request)) + + assert.same(200, res.status) + assert.match('GET http://upstream:8091/request HTTP/1.1', res:read_body()) + end) end) end) diff --git a/t/apicast-policy-camel.t b/t/apicast-policy-camel.t index 6577e6391..37c41834c 100644 --- a/t/apicast-policy-camel.t +++ b/t/apicast-policy-camel.t @@ -19,7 +19,7 @@ run_tests(); __DATA__ -=== TEST 1: API backend connection uses http proxy +=== TEST 1: API backend connection uses http proxy --- configuration { "services": [ diff --git a/t/apicast-policy-upstream-connection.t b/t/apicast-policy-upstream-connection.t index 767e32ce2..701f7ba6f 100644 --- a/t/apicast-policy-upstream-connection.t +++ b/t/apicast-policy-upstream-connection.t @@ -60,7 +60,6 @@ GET / --- ignore_response --- error_log upstream timed out ---- error_code: === TEST 2: Set timeouts using HTTPS proxy for backend In this test we set some timeouts to 1s. To force a read timeout, the upstream @@ -140,7 +139,6 @@ GET /test?user_key=test3 --- more_headers User-Agent: Test::APIcast::Blackbox ETag: foobar ---- error_code: --- error_log env proxy request: CONNECT test-upstream.lvh.me:$TEST_NGINX_RANDOM_PORT HTTP/1.1 using proxy: $TEST_NGINX_HTTPS_PROXY