Skip to content

Commit af7418b

Browse files
committed
perf(proxy/upstream_connection): setup configuration in init phase
Previously, the configuration for these two policies was set up in the export() phase. This phase only runs once, when the PolicyChain is built at configuration load time. In PR #1485, we moved those parts to the rewrite phase and caused a small allocation per request. This PR eliminates that by moving the configuration construction to the init phase.
1 parent 1b0032e commit af7418b

5 files changed

Lines changed: 43 additions & 15 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1313
- Routing Policy: eliminate unnecessary TemplateString allocation per request [PR #1585](https://github.com/3scale/APIcast/pull/1585)
1414
- Headers Policy: don't render template string when delete header [PR #1586](https://github.com/3scale/APIcast/pull/1586)
1515
- 3scale Batcher Policy: replace regex with string operations [PR #1583](https://github.com/3scale/APIcast/pull/1583)
16+
- Proxy/Upstream Connection: setup configuration in init phase - [PR #1602](https://github.com/3scale/APIcast/pull/1602)
1617

1718
### Fixed
1819
- 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)

gateway/src/apicast/policy/http_proxy/proxy.lua

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ local new = _M.new
88

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

11+
local function find_proxy(self, scheme)
12+
return self.proxies[scheme]
13+
end
14+
1115
function _M.new(config)
1216
local self = new(config)
1317
self.proxies = {}
@@ -27,22 +31,21 @@ function _M.new(config)
2731
end
2832
self.proxies[proto] = val or self.all_proxy
2933
end
30-
return self
31-
end
32-
33-
local function find_proxy(self, scheme)
34-
return self.proxies[scheme]
35-
end
3634

37-
function _M:rewrite(context)
38-
-- APIcast reads this flag in the access phase, that's why we need to set it
39-
-- in rewrite phase.
40-
context.get_http_proxy = function(uri)
35+
self.get_http_proxy = function(uri)
4136
if not uri.scheme then
4237
return nil
4338
end
4439
return find_proxy(self, uri.scheme)
4540
end
41+
42+
return self
43+
end
44+
45+
function _M:rewrite(context)
46+
-- APIcast reads this flag in the access phase, that's why we need to set it
47+
-- in rewrite phase.
48+
context.get_http_proxy = self.get_http_proxy
4649
end
4750

4851
return _M

gateway/src/apicast/policy/upstream_connection/upstream_connection.lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ function _M.new(config)
1515
self.send_timeout = tonumber(config.send_timeout)
1616
self.read_timeout = tonumber(config.read_timeout)
1717

18-
return self
19-
end
20-
21-
function _M:rewrite(context)
22-
context.upstream_connection_opts = {
18+
self.upstream_connection_opts = {
2319
connect_timeout = self.connect_timeout,
2420
send_timeout = self.send_timeout,
2521
read_timeout = self.read_timeout
2622
}
23+
24+
return self
25+
end
26+
27+
function _M:rewrite(context)
28+
context.upstream_connection_opts = self.upstream_connection_opts
2729
end
2830

2931
return _M

spec/policy/http_proxy/http_proxy_spec.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ describe('HTTP proxy policy', function()
4646
assert.is_nil(context.get_http_proxy(http_uri))
4747
end)
4848

49+
it("reuses the same get_http_proxy function across requests instead of allocating a new one", function()
50+
local proxy = proxy_policy.new({ all_proxy = all_proxy_val })
51+
52+
local context_a = {}
53+
local context_b = {}
54+
proxy:rewrite(context_a)
55+
proxy:rewrite(context_b)
56+
57+
assert.equal(context_a.get_http_proxy, context_b.get_http_proxy)
58+
end)
59+
4960
describe("get_http_proxy callback", function()
5061
local proxy = proxy_policy.new({
5162
all_proxy = all_proxy_val,

spec/policy/upstream_connection/upstream_connection_spec.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,16 @@ describe('Upstream connection policy', function()
2323

2424
assert.same(config_timeouts, context.upstream_connection_opts)
2525
end)
26+
27+
it('reuses the same table across requests instead of allocating a new one', function()
28+
local policy = UpstreamConnectionPolicy.new({ connect_timeout = 1, send_timeout = 2, read_timeout = 3 })
29+
30+
local context_a = {}
31+
local context_b = {}
32+
policy:rewrite(context_a)
33+
policy:rewrite(context_b)
34+
35+
assert.equal(context_a.upstream_connection_opts, context_b.upstream_connection_opts)
36+
end)
2637
end)
2738
end)

0 commit comments

Comments
 (0)