Skip to content
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

ngx.var vs ngx.req.get_headers performance #2227

Closed
rltas opened this issue Sep 12, 2023 · 2 comments
Closed

ngx.var vs ngx.req.get_headers performance #2227

rltas opened this issue Sep 12, 2023 · 2 comments

Comments

@rltas
Copy link

rltas commented Sep 12, 2023

This API requires a relatively expensive metamethod call and it is recommended to avoid using it on hot code paths.

https://github.com/openresty/lua-nginx-module#ngxvarvariable

vs

Note that the ngx.var.HEADER API call, which uses core $http_HEADER variables, may be more preferable for reading individual request headers.

https://github.com/openresty/lua-nginx-module#ngxreqget_headers

This might need a bit more explanation on when to use what, or why ngx.req.get_headers is more preferable considering ngx.var is to be avoided.

@oowl
Copy link
Contributor

oowl commented Sep 14, 2023

ngx.var.VAR needs to do a metatable index function call, and then do an FFI c function call to get the HTTP header, This function can get one particular HTTP header, but it needs another lua index metastable, which may be some performance regression if we do this very frequency.

ngx.req.get_headers can get all HTTP headers, It's a pure FFI function, so there is no need to do other metastable index function calls. But it can only return all HTTP headers, So it may need many memory or CPU time if the caller does not want to get all HTTP headers.

So if you want to get all HTTP Header in the current request, you may need to use ngx.req.get_headers, it's better to use ngx.var.VAR if you just want to get a particular HTTP header.

@rltas
Copy link
Author

rltas commented Sep 14, 2023

Thank you!

@rltas rltas closed this as completed Sep 14, 2023
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

No branches or pull requests

2 participants