3

I am calling 5 external servers to retrieve XML-based data for each request for a particular webpage on my IIS 6 server. Present volume is between 3-5 incoming requests per second, meaning 15-20 outgoing requests per second.

99% of the outgoing requests from my server (the client) to the external servers (the server) work OK but about 100-200 per day end up with a "The operation has timed out" exception.

This suggests I have a resource problem on my server - some shortage of sockets, ports etc or a thread lock but the problem with this theory is that the failures are entirely random - there are not a number of requests in a row that all fail - and two of the external servers account for the majority of the failures.

My question is how can I further diagnose these exceptions to determine if the problem is on my end (the client) or on the other end (the servers)?

The volume of requests precludes putting an analyzer on the wire - it would be very difficult to capture these few exceptions. I have reset CONNECTIONS and THREADS in my machine.config and the basic code looks like:

Dim hRequest As HttpWebRequest
Dim responseTime As String
Dim objWatch As New Stopwatch

Try

  ' calculate time it takes to process transaction
  objWatch.Start()

  hRequest = System.Net.WebRequest.Create(url)
  ' set some defaults
  hRequest.Timeout = 5000
  hRequest.ReadWriteTimeout = 10000
  hRequest.KeepAlive = False ' to prevent open HTTP connection leak
  hRequest.SendChunked = False
  hRequest.AllowAutoRedirect = True
  hRequest.MaximumAutomaticRedirections = 3
  hRequest.Accept = "text/xml"
  hRequest.Proxy = Nothing 'do not waste time searching for a proxy 
  hRequest.ServicePoint.Expect100Continue = False

  Dim feed As New XDocument()
  ' use *Using* to auto close connections
  Using hResponse As HttpWebResponse = DirectCast(hRequest.GetResponse(), HttpWebResponse)
    Using reader As XmlReader = XmlReader.Create(hResponse.GetResponseStream())
      feed = XDocument.Load(reader)
      reader.Close()
    End Using
    hResponse.Close()
  End Using

  objWatch.Stop()
  ' Work here with returned contents in "feed" document
  Return XXX' some results here

Catch ex As Exception

  objWatch.Stop()
  hRequest.Abort()
  Return Nothing

End Try

Any suggestions?

4

2 回答 2

2

默认情况下,HttpWebRequest 将每个 HTTP/1.1 服务器限制为 2 个连接。因此,如果您的请求需要一些时间才能完成,并且您有传入的请求在服务器上排队,您将失去连接并因此超时。

您应该更改 ServicePointManager 上的最大传出连接数。

ServicePointManager.DefaultConnectionLimit = 20 // or some big value.
于 2010-03-15T18:44:06.020 回答
0

你说你对 ASP 页面的每个传入请求执行 5 个传出请求。那是5个不同的服务器,还是同一个服务器?

在发出下一个请求之前,您是否等待上一个请求完成?超时是在等待连接时发生还是在请求/响应期间发生?

如果在请求/响应期间发生超时,则意味着目标服务器处于压力之下。找出是否是这种情况的唯一方法是在其中一台机器上运行wireshark/netmon,并查看网络跟踪以查看来自应用程序的请求是否甚至通过服务器,如果它也就是说,目标服务器是否在给定的超时时间内响应。

如果这是一个线程不足问题,那么诊断它的方法之一是在您开始超时时将 windbg.exe 调试器附加到 w3wp.exe 进程。然后加载 sos.dll 调试扩展。并运行 !threads 命令,然后运行 ​​!threadpool 命令。它将向您显示使用/剩余多少工作线程和完成端口线程。如果#completionport 线程或工作线程较低,那么这将导致超时。

或者,您可以监视 ASP.NET 和 System.net 性能计数器。查看 ASP.NET 请求队列是否单调增加 - 这可能表明您的传出请求完成速度不够快。

抱歉,这里没有简单的答案。您需要探索许多途径。如果我是你,我会在你开始超时时将 windbg.exe 附加到 w3wp 并执行我之前描述的操作。

于 2010-03-30T16:53:13.287 回答