Skip to content

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

Merged
doujiang24 merged 5 commits into
masterfrom
proxy_recreate_request
Jul 30, 2020
Merged

doujiang24 merged 5 commits into
masterfrom
proxy_recreate_request

Conversation

@dndx

@dndx dndx commented Jun 23, 2020 •

Copy link
Copy Markdown
Member

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
Copy Markdown
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

dndx commented Jun 30, 2020

Copy link
Copy Markdown
Member Author

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

@dndx
dndx force-pushed the proxy_recreate_request branch from 20204f1 to 5a27cf2 Compare June 30, 2020 11:22
@dndx

dndx commented Jul 16, 2020

Copy link
Copy Markdown
Member Author

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

Comment thread lib/ngx/balancer.lua Outdated
long read_timeout, char **err);

int
ngx_http_lua_ffi_balancer_recreate_request(ngx_http_request_t *r,

Copy link
Copy Markdown
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

Comment thread lib/ngx/balancer.md Outdated
[Back to TOC](#table-of-contents)

recreate_request
------------

Copy link
Copy Markdown
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

Comment thread lib/ngx/balancer.md Outdated

**context:** *balancer_by_lua**

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

Copy link
Copy Markdown
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.

Comment thread lib/ngx/balancer.md Outdated

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
Copy Markdown
Member

Choose a reason for hiding this comment

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

s/sets/set/

Comment thread lib/ngx/balancer.md Outdated
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
Copy Markdown
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..."

Comment thread lib/ngx/balancer.md Outdated
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
Copy Markdown
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...

Comment thread lib/ngx/balancer.md Resolved
Datong Sun 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

dndx commented Jul 17, 2020

Copy link
Copy Markdown
Member Author

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

Comment thread lib/ngx/balancer.md Outdated
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
Copy Markdown
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.

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

if ngx.ctx.balancer_ran then

Copy link
Copy Markdown
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