在mono 3.2.8 Ubuntu Server 14上使用HttpWebRequest

时间:2014-06-29 16:14:01

标签: c# multithreading ubuntu mono garbage-collection

我有一个创建500个线程的控制台应用程序,每个线程从并发堆栈中提取url并创建HttpWebRequest对象,获取响应并记录时间。我在带有512 MB RAM的Windows Server 2012 R2机器上运行此程序,它运行正常。

我尝试过使用IO完成端口(异步方法)但性能更差,所以现在不适合我。

在Ubuntu Server 14上运行64位(1 GB 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

以下是创建网络请求的代码:

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命令行更改托儿所大小吗?我不确定查找套接字句柄的错误是什么问题,但是它可能与托儿所错误有关?

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

我建议您将单声道版本更新到最新版本。 Microsoft还将.NET更新为开源软件,因此您可能希望在单声道团队集成此特定代码时进行尝试。

答案 1 :(得分:0)

您确定您的服务器不在防火墙后面吗?在测试环境中关闭包括Windows防火墙在内的所有防火墙,然后尝试重新连接。