Skip to content

Commit 193d4ea

Browse files
committed
Add aggregation policy action proxyOptions property
Allows setting global proxyTimeout.
1 parent 4681d53 commit 193d4ea

File tree

6 files changed

+57
-1
lines changed

6 files changed

+57
-1
lines changed

README.org

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,20 @@ brew:
452452
If keepResponseHeaders is not specified all headers will be
453453
returned.
454454

455+
*** ~proxyOptions~
456+
457+
Allow setting global ~proxyOptions~ (can be overwritten per backend).
458+
459+
#+begin_src yaml
460+
- aggregation:
461+
- action:
462+
proxyOptions:
463+
proxyTimeout: 10000
464+
#+end_src
465+
466+
The above will set a global timeout for requests going to backends of
467+
10 seconds.
468+
455469
* Building deployable images
456470

457471
The repository includes a [[./Dockerfile][Dockerfile]] that can be used to build a

policies/aggregation/aggregation.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,11 @@ module.exports = (config, { gatewayConfig: { serviceEndpoints } }) => {
198198
proxy.on('error', () => res.sendStatus(httpcode.BadGateway))
199199
}
200200

201+
const proxyOptions = config.proxyOptions || {}
201202
const opts = await proxyOptionsForEndpoint({ db, endpoint })
202203
opts.headers.traceParent = outgoingTraceParent.toString()
203204
proxy.web(req, res, {
205+
...proxyOptions,
204206
...opts,
205207
target: endpoint.url,
206208
changeOrigin: true,

policies/aggregation/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ module.exports = {
3535
metricsPrefix: {
3636
type: 'string',
3737
description: 'Prefix for metrics for outgoing HTTP requests'
38+
},
39+
proxyOptions: {
40+
type: 'object',
41+
description: 'Global proxy options'
3842
}
3943

4044
},

test/config/gateway.config.v4.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ serviceEndpoints:
104104
# proxyTimeout: 500
105105
proxyOptionsEncoded: >-
106106
1ad6802a2431e8d09a03f7d9e7dbaf28:VyJb/QWzTZ/2jIU41q0zQGuJdRoZrr0yJ6W/pUE9jq8=
107+
'OtherSlow.Backend':
108+
url: '${OOAPI_SLOW_BACKEND_URL:-http://localhost:8086/}'
107109

108110
policies:
109111
- basic-auth
@@ -211,6 +213,8 @@ pipelines:
211213
paths: ['/']
212214
- endpoint: 'Slow.Backend'
213215
paths: ['/']
216+
- endpoint: 'OtherSlow.Backend'
217+
paths: ['/']
214218

215219
- request-transformer:
216220
- action:
@@ -252,6 +256,8 @@ pipelines:
252256
- 'traceparent'
253257
- 'content-type'
254258
- 'content-length'
259+
proxyOptions:
260+
proxyTimeout: 2000
255261
not_found:
256262
apiEndpoints:
257263
- not_found

test/config/gateway.config.v5.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ serviceEndpoints:
111111
url: '${OOAPI_SLOW_BACKEND_URL:-http://localhost:8086/}'
112112
proxyOptions:
113113
proxyTimeout: 500
114+
'OtherSlow.Backend':
115+
url: '${OOAPI_SLOW_BACKEND_URL:-http://localhost:8086/}'
114116

115117
policies:
116118
- basic-auth
@@ -233,6 +235,8 @@ pipelines:
233235
paths: ['/']
234236
- endpoint: 'Slow.Backend'
235237
paths: ['/']
238+
- endpoint: 'OtherSlow.Backend'
239+
paths: ['/']
236240

237241
- request-transformer:
238242
- action:
@@ -274,6 +278,8 @@ pipelines:
274278
- 'content-type'
275279
- 'content-length'
276280
- 'traceparent'
281+
proxyOptions:
282+
proxyTimeout: 2000
277283
not_found:
278284
apiEndpoints:
279285
- not_found

test/timeout.integration.test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const {
2828
integrationContext('endpoint timeouts', function () {
2929
describe('slow backend', () => {
3030
it('backend response status code is 0 when sleeping longer than configured 500ms', async () => {
31-
const res = await httpGet(gatewayUrl('bubbles', `/?sleep=${1000 * 60 * 60 * 24}`), {
31+
const res = await httpGet(gatewayUrl('bubbles', `/?sleep=${60 * 60 * 24}`), {
3232
headers: { 'X-Route': 'endpoint=Slow.Backend' }
3333
})
3434
const { gateway: { endpoints } } = JSON.parse(res.body)
@@ -49,4 +49,28 @@ integrationContext('endpoint timeouts', function () {
4949
)
5050
})
5151
})
52+
53+
describe('other slow backend', () => {
54+
it('backend response status code is 0 when sleeping longer than globally configured 2s', async () => {
55+
const res = await httpGet(gatewayUrl('bubbles', `/?sleep=${60 * 60 * 24}`), {
56+
headers: { 'X-Route': 'endpoint=OtherSlow.Backend' }
57+
})
58+
const { gateway: { endpoints } } = JSON.parse(res.body)
59+
assert.equal(
60+
endpoints['OtherSlow.Backend'].responseCode,
61+
0
62+
)
63+
})
64+
65+
it('backend response status code is OK when fast enough', async () => {
66+
const res = await httpGet(gatewayUrl('bubbles', '/?sleep=1'), {
67+
headers: { 'X-Route': 'endpoint=OtherSlow.Backend' }
68+
})
69+
const { gateway: { endpoints } } = JSON.parse(res.body)
70+
assert.equal(
71+
endpoints['OtherSlow.Backend'].responseCode,
72+
httpcode.OK
73+
)
74+
})
75+
})
5276
})

0 commit comments

Comments
 (0)