Skip to content

Commit

Permalink
timeouts in src/resty/http/proxy.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed Feb 6, 2024
1 parent a7dec6d commit b705c0f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 28 deletions.
17 changes: 15 additions & 2 deletions gateway/src/resty/http/proxy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 1 addition & 15 deletions gateway/src/resty/resolver/http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
37 changes: 29 additions & 8 deletions spec/resty/http/proxy_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion t/apicast-policy-camel.t
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
2 changes: 0 additions & 2 deletions t/apicast-policy-upstream-connection.t
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b705c0f

Please sign in to comment.