Skip to content

Lua supports hosts #2385

Closed
Closed
@ZJfans

Description

@ZJfans

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.

Activity

oowl

oowl commented on Dec 19, 2024

@oowl
Contributor

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

oowl commented on Dec 19, 2024

@oowl
Contributor

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

ZJfans commented on Dec 19, 2024

@ZJfans
ContributorAuthor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @oowl@ZJfans

        Issue actions

          Lua supports hosts · Issue #2385 · openresty/lua-nginx-module