Dns.BeginGetHost ...阻断方法 [英] Dns.BeginGetHost... methods blocking

查看:500
本文介绍了Dns.BeginGetHost ...阻断方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我希望做一个的很多的DNS查询的

So I want to make a lot of DNS queries.

我创建从开始/ EndGetHostEntry 异步任务对(千):

I create (thousands) of Tasks from the Begin/EndGetHostEntry async pair:

var lookupTask = Task.Factory.FromAsync
   ( Dns.BeginGetHostEntry,
     (Func<IAsyncResult, IPHostEntry>) Dns.EndGetHostEntry,
     "google.com", 
     null
   )

然后 Task.WaitAll 的一切来完成。我看到线程响应我的要求大幅增加线程池的数量。如果我强迫线程池 minThreads 500,工作量相当快消耗。所有这一切都指向在 DNS 异步实现阻塞。

then Task.WaitAll for everything to complete. I'm seeing the number of ThreadPool threads increase drastically in response to my requests. If I force the ThreadPool minThreads to 500, the workload is consumed considerably faster. All of this points to blocking in the Dns asynchronous implementation.

如果我更换 DNS 管理DNS客户端,我可以只用1或2中的线程消耗同样的工作量线程池与CPU几乎空转。

If I replace Dns with a managed Dns client, I can consume the same workload with only 1 or 2 threads in the ThreadPool with cpu virtually idling.

问题是,在 DNS 的实施是绝对核心,许多网络API(的HttpWebRequest Web客户端的HttpClient ),他们似乎都受到这个问题的影响。如果我解析DNS与第三方库,并使用IP地址作为URI主机发出HTTP请求,然后修改主机头修复的要求,我得到比较卓越性能涉及 System.Net.Dns

The thing is, the Dns implementation is absolutely core to many networking APIs (HttpWebRequest, WebClient, HttpClient), and they all seem to be affected by this issue. If I resolve DNS with a 3rd party library, and make HTTP requests using the IP address as the host in the uri, then alter the Host header to fix the request, I get blistering performance in comparison to anything involving System.Net.Dns.

这是怎么回事?有我错过了什么或者是 System.Net.Dns 的实施真的如此糟糕?

What's going on here? Have I missed something or is the System.Net.Dns implementation really that bad?

推荐答案

System.Net.Dns使用Windows 的gethostbyname 功能DNS查询并没有真正具有异步功能都没有。该BeginGetHostEntry功能基本上只是线程池同步GetHostEntry调用的包装。

System.Net.Dns uses the windows gethostbyname function for DNS queries and doesn't really have asynchronous functions at all. The BeginGetHostEntry function is basically just a wrapper for a synchronous GetHostEntry invocation on the thread pool.

上次我有同样的问题与慢速/同步DNS查找我最终只用一个大的线程池来获得,因为没有哪一个内置的Windows或.net域名相关的功能支持正确的(水货)异步执行完成工作。

Last time I had this same problem with slow/synchronous DNS lookups I eventually just used a large ThreadPool to get the job done since not a single built-in windows or .net DNS related function supports proper (parallel) asynchronous execution.

这篇关于Dns.BeginGetHost ...阻断方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆