在mono 3.2.8 Ubuntu Server 14上使用HttpWebRequest
本文关键字:HttpWebRequest Server Ubuntu mono | 更新日期: 2023-09-27 18:10:47
我有一个控制台应用程序,它创建了500个线程,每个线程从并发堆栈中提取url,并创建HttpWebRequest对象,获得响应并记录时间。我在内存512mb的Windows Server 2012 R2机器上运行了这个程序,运行得很好。
我已经尝试使用IO完成端口(异步方法),但已经得到了更差的性能,所以这不是我现在的选择。
在Ubuntu Server 14 64位(1gb RAM)上运行会导致以下错误显示:
Warning: Degraded allocation. Consider increasing nursery-size if the warning persists.
_wapi_connect: error looking up socket handle 0x1f (error 10038)
_wapi_connect: error looking up socket handle 0x91 (error 10038)
_wapi_connect: error looking up socket handle 0xb (error 10022)
_wapi_connect: error looking up socket handle 0x6c (error 10038)
_wapi_connect: error looking up socket handle 0x6b (error 10038)
_wapi_connect: error looking up socket handle 0x1c (error 10038)
_wapi_connect: error looking up socket handle 0x1b (error 10038)
_wapi_connect: error looking up socket handle 0xbd (error 10038)
_wapi_connect: error looking up socket handle 0x31 (error 10038)
_wapi_connect: error looking up socket handle 0x1a2 (error 10038)
_wapi_connect: error looking up socket handle 0x187 (error 10038)
_wapi_connect: error looking up socket handle 0x18c (error 10038)
Warning: Degraded allocation. Consider increasing nursery-size if the warning persists.
_wapi_connect: error looking up socket handle 0xa5
_wapi_connect: error looking up socket handle 0x141 (error 10038)
_wapi_connect: error looking up socket handle 0x44
下面是创建web请求的代码:
private static HttpWebRequest CreateRequest(string url)
{
HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest;
req.UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36";
req.Method = "GET";
req.AllowAutoRedirect = true;
req.KeepAlive = false;
req.MaximumAutomaticRedirections = 1;
req.Timeout = 5000;
req.Proxy = null;
return req;
}
线程创建代码:
Thread thread = new Thread(() =>
{
Stopwatch s = Stopwatch.StartNew();
string url;
while (urlsToPull.TryPop(out url))
{
try
{
Stopwatch indiv = Stopwatch.StartNew();
using (HttpWebResponse res = CreateRequest(url).GetResponse() as HttpWebResponse)
{
if (res.StatusCode != HttpStatusCode.OK)
{
Interlocked.Increment(ref badStatus);
}
}
indiv.Stop();
Add(ref urlSum, indiv.Elapsed.TotalSeconds);
lock (goodFile)
{
goodFile.WriteLine(url);
}
}
catch
{
lock (errorFile)
{
errorFile.WriteLine(url);
}
Interlocked.Increment(ref errors);
}
Interlocked.Increment(ref urlsChecked);
}
Interlocked.Decrement(ref tasks);
s.Stop();
}, 4096);
托儿所大小可以通过mono命令行更改吗?我不确定查找套接字句柄错误的问题是什么,但也许它与托儿所错误有关?
我建议您将mono版本更新到最新版本。微软还将。net更新为开源,所以如果/当mono团队集成了这些特定的代码时,您可能想要再试一次。
您确定您的服务器没有在防火墙后面吗?在测试环境中关闭所有防火墙(包括windows防火墙),然后尝试重新连接。