Skip to content

Lua supports hosts #2385

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

Closed
ZJfans opened this issue Dec 19, 2024 · 3 comments
Closed

Lua supports hosts #2385

ZJfans opened this issue Dec 19, 2024 · 3 comments

Comments

@ZJfans
Copy link
Contributor

ZJfans commented Dec 19, 2024

When using connect, static resolution is not supported, only resolver is supported.It's forbidden here,https://github.com/ZJfans/lua-nginx-module/blob/master/src/ngx_http_lua_socket_tcp.c#L668 .

lua_pushcfunction(L, ngx_http_lua_socket_tcp_connect);
    lua_setfield(L, -2, "connect");

I studied the code and official documentation, https://www.f5.com/company/blog/nginx/dns-service-discovery-nginx-plus ,All modules set no_resolve to 1, which prohibits domain name resolution. I guess it is to prevent repeated resolution. It is only processed in https://github.com/nginx/nginx/blob/master/src/http/ngx_http_upstream_round_robin.c#L366 .

This is enough for nginx, but when I use openresty and use domain names from lua, I have to configure the resolver because hosts (which actually also includes the system's dns) are not supported. Currently I have introduced the dns module of kong, https://github.com/Kong/kong/tree/master/kong/resty/dns ,for hosts.

I'm not sure if I understand correctly, should we support hosts. it would only require a small change.
@zhuizhuhaomeng I'd appreciate it if you have time to look at this.

@oowl
Copy link
Contributor

oowl commented Dec 19, 2024

Because ngx_parse_url uses a blocking method in the internal processing logic of nginx, we need no_resolve = 1 here to avoid blocking the epoll loop call of nginx during operation. This is the only way we can do it in this asynchronous model. So nginx has its own asynchronous DNS resolution mechanism http://nginx.org/en/docs/http/ngx_http_core_module.html#resolver, which performs DNS request assembly and response parsing in the C module. Openresty has tried to support it before #1478

So back to your question, connect supports host is not as simple as it seems, and in fact, the current solution recommended by openresty does not seem to have any big problems, that is, using lua-resty-dns https://github.com/openresty/lua-resty-dns

@oowl
Copy link
Contributor

oowl commented Dec 19, 2024

Actually, it is not as easy as you think to support connect hostname in async runtime. Most runtimes use a new thread to call the blocking function gethostbyname() and then wait for the result to return to the main thread so that the main asynchronous runtime thread will not be blocked. Of course, nginx and lua themselves cannot support multi-threaded programming very well, so this solution is not easy to implement in openresty.

@ZJfans
Copy link
Contributor Author

ZJfans commented Dec 19, 2024

Actually, it is not as easy as you think to support connect hostname in async runtime. Most runtimes use a new thread to call the blocking function gethostbyname() and then wait for the result to return to the main thread so that the main asynchronous runtime thread will not be blocked. Of course, nginx and lua themselves cannot support multi-threaded programming very well, so this solution is not easy to implement in openresty.

Thank you for your reply, it is very helpful for me. I will take some time to understand this.

@ZJfans ZJfans closed this as completed Dec 19, 2024
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