Skip to content

feature: add the balancer.recreate_request function, which allows user to recreate request buffer in balancer phase. #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 30, 2020

Conversation

dndx
Copy link
Member

@dndx dndx commented Jun 23, 2020

I hereby granted the copyright of the changes in this pull request
to the authors of this lua-resty-core project.

Feature was originally developed by @locao inside Kong/lua-kong-nginx-module#13. With slight modifications from @dndx.

Related PR: openresty/lua-nginx-module#1734.

@doujiang24
Copy link
Member

@dndx just a reminder, the CI failed.

@dndx dndx force-pushed the proxy_recreate_request branch from ef551a4 to 20204f1 Compare June 29, 2020 16:06
@dndx
Copy link
Member Author

dndx commented Jun 30, 2020

@doujiang24 Yes the test plan was wrong. I have fixed the number.

@dndx
Copy link
Member Author

dndx commented Jul 16, 2020

Ping @doujiang24. Could you give another look at this? Thanks!

@@ -38,6 +38,10 @@ if subsystem == 'http' then
int ngx_http_lua_ffi_balancer_set_timeouts(ngx_http_request_t *r,
long connect_timeout, long send_timeout,
long read_timeout, char **err);

int
ngx_http_lua_ffi_balancer_recreate_request(ngx_http_request_t *r,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: FFI prototype declarations should have the return type on the same line, like in the C module

@@ -237,6 +237,29 @@ This function was first added in the `0.1.7` version of this library.

[Back to TOC](#table-of-contents)

recreate_request
------------
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: this line should underline the whole method name


**context:** *balancer_by_lua**

Regenerates the request buffer for sending to the upstream server. This is useful, for example
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe better to stay consistent with wording? i.e. use "recreate" instead of "regenerate" which is the terminology used everywhere else when referring to this API.


Normally this does not work because the request buffer is created once during upstream module
initialization and won't be regenerated for subsequent retries. However you can use
`proxy_set_header My-Header $my_header` and sets the `ngx.var.my_header` variable inside the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/sets/set/

Normally this does not work because the request buffer is created once during upstream module
initialization and won't be regenerated for subsequent retries. However you can use
`proxy_set_header My-Header $my_header` and sets the `ngx.var.my_header` variable inside the
balancer phase. Calling this function after that will cause the request buffer to be re-generated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Calling this function after that" can be confusing for the reader. "Calling balancer.recreate_request() after updating a header field will cause..."

and the `My-Header` header will thus contain the new value.

**Warning:** because the request buffer has to be recreated and such allocation occurs on the
request memory pool, the old buffer has to be thrown away and only be freed after the request
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and will only be freed when...

dndx added 3 commits July 16, 2020 22:01
to recreate request buffer in balancer phase.

This allows certain request parameters, such as headers (including
`Host` header) to be modified between balancer retries.
@dndx dndx force-pushed the proxy_recreate_request branch from 5a27cf2 to 1e749c8 Compare July 17, 2020 05:04
@dndx
Copy link
Member Author

dndx commented Jul 17, 2020

Thanks for the reviews @thibaultcha, I fixed the styling and ran the document through with markdown-toc.pl.

to this function should be made **only** if you know the request buffer must be regenerated,
instead of unconditionally in each balancer retries.

This function was first added in the `0.1.19` version of this library.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be changed to 0.1.20.

t/balancer.t Outdated
print("here")
local b = require "ngx.balancer"

if ngx.ctx.balancer_ran then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think balancer_run is more grammatically correct.

@doujiang24 doujiang24 merged commit 7560c8c into master Jul 30, 2020
doujiang24 pushed a commit that referenced this pull request Jul 30, 2020
…ser to recreate request buffer in balancer phase. (#302)

This allows certain request parameters, such as headers (including
`Host` header) to be modified between balancer retries.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants