Skip to content

Commit 4e80133

Browse files
committed
feat(patches) add the upstream request recreate feature patches by @locao
These will be removed once openresty/lua-nginx-module#1734 and openresty/lua-resty-core#302 are merged
1 parent 5c0ca06 commit 4e80133

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
From daf26ee06cb4e276c96ea7e34a192f04d5fa7a22 Mon Sep 17 00:00:00 2001
2+
From: Datong Sun <[email protected]>
3+
Date: Tue, 23 Jun 2020 17:40:20 -0700
4+
Subject: [PATCH 1/2] feature: add the `balancer.recreate_request` function,
5+
which allows user to recreate request buffer in balancer phase.
6+
7+
This allows certain request parameters, such as headers (including
8+
`Host` header) to be modified between balancer retries.
9+
---
10+
lua-resty-core-0.1.17/lib/ngx/balancer.lua | 25 ++++++++++++++++++++++
11+
12+
diff --git a/lua-resty-core-0.1.17/lib/ngx/balancer.lua b/lua-resty-core-0.1.17/lib/ngx/balancer.lua
13+
index d584639..2bc16e1 100644
14+
--- a/lua-resty-core-0.1.17/lib/ngx/balancer.lua
15+
+++ b/lua-resty-core-0.1.17/lib/ngx/balancer.lua
16+
@@ -38,6 +38,10 @@ if subsystem == 'http' then
17+
int ngx_http_lua_ffi_balancer_set_timeouts(ngx_http_request_t *r,
18+
long connect_timeout, long send_timeout,
19+
long read_timeout, char **err);
20+
+
21+
+ int
22+
+ ngx_http_lua_ffi_balancer_recreate_request(ngx_http_request_t *r,
23+
+ char **err);
24+
]]
25+
26+
ngx_lua_ffi_balancer_set_current_peer =
27+
@@ -207,4 +211,25 @@ function _M.set_timeouts(connect_timeout, send_timeout, read_timeout)
28+
end
29+
30+
31+
+if subsystem == 'http' then
32+
+ function _M.recreate_request()
33+
+ local r = get_request()
34+
+ if not r then
35+
+ error("no request found")
36+
+ end
37+
+
38+
+ local rc = C.ngx_http_lua_ffi_balancer_recreate_request(r, errmsg)
39+
+ if rc == FFI_OK then
40+
+ return true
41+
+ end
42+
+
43+
+ if errmsg[0] ~= nil then
44+
+ return nil, ffi_str(errmsg[0])
45+
+ end
46+
+
47+
+ return nil, "failed to recreate the upstream request"
48+
+ end
49+
+end
50+
+
51+
+
52+
return _M
53+
--
54+
2.20.1
55+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
From 200cf83dbd180bc015e3e1b5b4344abc665d096a Mon Sep 17 00:00:00 2001
2+
From: Datong Sun <[email protected]>
3+
Date: Tue, 23 Jun 2020 17:33:09 -0700
4+
Subject: [PATCH] feature: added the
5+
`ngx_http_lua_ffi_balancer_recreate_request` FFI function to allow recreation
6+
of request buffer in balancer phase.
7+
8+
---
9+
ngx_lua-0.10.15/src/ngx_http_lua_balancer.c | 39 +++++++++++++++++++++++++++++++++++++
10+
1 file changed, 39 insertions(+)
11+
12+
diff --git a/ngx_lua-0.10.15/src/ngx_http_lua_balancer.c b/ngx_lua-0.10.15/src/ngx_http_lua_balancer.c
13+
index f71a3e00..eff9dc13 100644
14+
--- a/ngx_lua-0.10.15/src/ngx_http_lua_balancer.c
15+
+++ b/ngx_lua-0.10.15/src/ngx_http_lua_balancer.c
16+
@@ -753,4 +753,43 @@ ngx_http_lua_ffi_balancer_get_last_failure(ngx_http_request_t *r,
17+
}
18+
19+
20+
+int
21+
+ngx_http_lua_ffi_balancer_recreate_request(ngx_http_request_t *r,
22+
+ char **err)
23+
+{
24+
+ ngx_http_lua_ctx_t *ctx;
25+
+ ngx_http_upstream_t *u;
26+
+
27+
+ if (r == NULL) {
28+
+ *err = "no request found";
29+
+ return NGX_ERROR;
30+
+ }
31+
+
32+
+ u = r->upstream;
33+
+
34+
+ if (u == NULL) {
35+
+ *err = "no upstream found";
36+
+ return NGX_ERROR;
37+
+ }
38+
+
39+
+ ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
40+
+ if (ctx == NULL) {
41+
+ *err = "no ctx found";
42+
+ return NGX_ERROR;
43+
+ }
44+
+
45+
+ if ((ctx->context & NGX_HTTP_LUA_CONTEXT_BALANCER) == 0) {
46+
+ *err = "API disabled in the current context";
47+
+ return NGX_ERROR;
48+
+ }
49+
+
50+
+ /* u->create_request can not be NULL since we are in balancer phase */
51+
+ ngx_http_lua_assert(u->create_request != NULL);
52+
+
53+
+ *err = NULL;
54+
+
55+
+ return u->create_request(r);
56+
+}
57+
+
58+
+
59+
#endif /* NGX_LUA_NO_FFI_API */
60+
--
61+
2.20.1
62+

0 commit comments

Comments
 (0)