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

Synchronous DNS resolution affects the performance of this library #314

Open
hujiulong opened this issue Feb 25, 2025 · 2 comments · May be fixed by #315
Open

Synchronous DNS resolution affects the performance of this library #314

hujiulong opened this issue Feb 25, 2025 · 2 comments · May be fixed by #315

Comments

@hujiulong
Copy link

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(

@Apollon77
Copy link
Contributor

do you like to provide a PR?

@hujiulong
Copy link
Author

@Apollon77 Yes, I created a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants