用HttpWebRequest去Call别的Service,怎样才能提高并发量和性能?
因为建立一个TCP连接要三次握手,.Net 默认使用 keepAlive = Ture 去重用连接,避免重建Connection的开销,还有一些设置我们也要注意:
DefaultConnectionLimit
description: Maximum number of concurrent connections to a single ServicePoint
default: 2
suggested: 12*N, N is the number of CPU
MaxServicePointIdleTime
description: Maximum idle time of ServicePoint object, but in the testing, i found it's the maximum idle time of the TCP connection in connection pool, http://msdn.microsoft.com/en-US/library/system.net.servicepointmanager.maxservicepointidletime.aspx
default: 100000ms
suggested: 3600000ms, 1 hour, set a long time to keep the connection, avoid creating connection every call.
ConnectionLeaseTimeout
description: A Int32 that specifies the number of milliseconds that an active ServicePoint connection remains open. The default is -1, which allows an active ServicePoint connection to stay connected indefinitely. Set this property to 0 to force ServicePoint connections to close after servicing a request.
default: -1
suggested: -1
MaxServicePoints
description: Maximum number of concurrent ServicePoint objects
default: 0, means no limited.
suggested: 0
TcpKeepAliveTimeInMillis
description: after idle more than n ms, a keepalive tcp package will be send by client, to verify the connection is still available or not.
default: 7200000ms, 2 hours
suggested: 10000ms, 10s, recyle the bad connections in time, reduce the burden of the server.
TcpKeepAliveIntervalInMillis
description: between when successive keep-alive packets are sent if no acknowledgement is received, until received a response, if 5 packages were sent, and no any response, this connection will be drop, http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.settcpkeepalive.aspx
default: 1000ms
suggested: 1000ms
ServicePointManager.Expect100Continue = false;
ServicePointManager.UseNagleAlgorithm = false;
MaxWorkerThreads 100
MaxIOThreads 100
MaxConnection 12*N
MinfreeThreads 88*N
MinLocalRequestfreeThreads 88*N
MinWorkerThreds 50 (MaxWorkerThreads/2)
参考资料: