Skip to content

Synchronous DNS resolution affects the performance of this library #314

Open
@hujiulong

Description

@hujiulong

Hello everyone, I am using this library in my application to modify the response of requests in a webpage. However, I noticed a strange phenomenon: if a request on the webpage is in a pending state, subsequent requests will also be pending, as if all requests are queued.

I checked both my application code and the library's code and found no logic for synchronous blocking between requests; everything is processed asynchronously.

After a thorough investigation, I discovered the culprit: Node.js internally uses dns.lookup() by default for DNS resolution. This method appears to be asynchronous, but its implementation is actually synchronous!

This is a quotation from the Node.js official website:

Though the call to dns.lookup() will be asynchronous from JavaScript's perspective, it is implemented as a synchronous call to getaddrinfo(3) that runs on libuv's threadpool. This can have surprising negative performance implications for some applications.

So, once there is a DNS resolution timeout (which is more common in China), subsequent DNS requests will wait for the preceding request to complete.

Solution:
Methods like net.connect() and http.request() provide a lookup parameter, allowing you to implement DNS resolution yourself. The community offers many solutions to this problem, such as dns-lookup-cache. You need to pass a custom lookup method to all places in the library where DNS resolution is involved.

Places to Modify:

ctx.proxyToServerRequest = proto.request(

const conn = net.connect(

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions