Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Routing Policy: eliminate unnecessary TemplateString allocation per request [PR #1585](https://github.com/3scale/APIcast/pull/1585)
- Headers Policy: don't render template string when delete header [PR #1586](https://github.com/3scale/APIcast/pull/1586)
- 3scale Batcher Policy: replace regex with string operations [PR #1583](https://github.com/3scale/APIcast/pull/1583)
- Proxy/Upstream Connection: setup configuration in init phase - [PR #1602](https://github.com/3scale/APIcast/pull/1602)

### Fixed
- Correct FAPI header to `x-fapi-interaction-id` [PR #1557](https://github.com/3scale/APIcast/pull/1557) [THREESCALE-11957](https://issues.redhat.com/browse/THREESCALE-11957)
Expand Down
23 changes: 13 additions & 10 deletions gateway/src/apicast/policy/http_proxy/proxy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ local new = _M.new

local proxies = {"http", "https"}

local function find_proxy(self, scheme)
return self.proxies[scheme]
end

function _M.new(config)
local self = new(config)
self.proxies = {}
Expand All @@ -27,22 +31,21 @@ function _M.new(config)
end
self.proxies[proto] = val or self.all_proxy
end
return self
end

local function find_proxy(self, scheme)
return self.proxies[scheme]
end

function _M:rewrite(context)
-- APIcast reads this flag in the access phase, that's why we need to set it
-- in rewrite phase.
context.get_http_proxy = function(uri)
self.get_http_proxy = function(uri)
if not uri.scheme then
return nil
end
return find_proxy(self, uri.scheme)
end

return self
end

function _M:rewrite(context)
-- APIcast reads this flag in the access phase, that's why we need to set it
-- in rewrite phase.
context.get_http_proxy = self.get_http_proxy
end

return _M
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ function _M.new(config)
self.send_timeout = tonumber(config.send_timeout)
self.read_timeout = tonumber(config.read_timeout)

return self
end

function _M:rewrite(context)
context.upstream_connection_opts = {
self.upstream_connection_opts = {
connect_timeout = self.connect_timeout,
send_timeout = self.send_timeout,
read_timeout = self.read_timeout
}

return self
end

function _M:rewrite(context)
context.upstream_connection_opts = self.upstream_connection_opts
end

return _M
11 changes: 11 additions & 0 deletions spec/policy/http_proxy/http_proxy_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ describe('HTTP proxy policy', function()
assert.is_nil(context.get_http_proxy(http_uri))
end)

it("reuses the same get_http_proxy function across requests instead of allocating a new one", function()
local proxy = proxy_policy.new({ all_proxy = all_proxy_val })

local context_a = {}
local context_b = {}
proxy:rewrite(context_a)
proxy:rewrite(context_b)

assert.equal(context_a.get_http_proxy, context_b.get_http_proxy)
end)

describe("get_http_proxy callback", function()
local proxy = proxy_policy.new({
all_proxy = all_proxy_val,
Expand Down
11 changes: 11 additions & 0 deletions spec/policy/upstream_connection/upstream_connection_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,16 @@ describe('Upstream connection policy', function()

assert.same(config_timeouts, context.upstream_connection_opts)
end)

it('reuses the same table across requests instead of allocating a new one', function()
local policy = UpstreamConnectionPolicy.new({ connect_timeout = 1, send_timeout = 2, read_timeout = 3 })

local context_a = {}
local context_b = {}
policy:rewrite(context_a)
policy:rewrite(context_b)

assert.equal(context_a.upstream_connection_opts, context_b.upstream_connection_opts)
end)
end)
end)
Loading