开发者

HttpWebRequest and HttpWebResponse ideal async buffer sizes

开发者 https://www.devze.com 2023-04-10 18:10 出处:网络
I\'m trying to use HttpWebRequest and HttpWebResponse in .NET 3.5, running them in asynchronously: BeginGetRequestStream, EndGetRequestStream, BeginWrite, EndWrite, BeginGetResponse, EndGetResponse, B

I'm trying to use HttpWebRequest and HttpWebResponse in .NET 3.5, running them in asynchronously: BeginGetRequestStream, EndGetRequestStream, BeginWrite, EndWrite, BeginGetResponse, EndGetResponse, BeginRead, EndRead - all parts of handling a request are asynchronous.

I have a few threads that send a large number of concurrent requests. EndRead and EndWrite are both blocking operations - they block the current thread while the actual read/write against the stream is done, I'm trying to come up with an ideal input/output buffer size for these operations.

My reasoning is this: as I have multiple requests active at a time, they'll keep firing callbacks to let the thread know there's some data available or data was sent. If my buffers are large, reading/writing the data through the wire will take longer, so EndRead/EndWrite will block longer. This would force the other requests on the same thread to wait a bit longer, since their notifications will have t开发者_如何学编程o wait until the thread is unblocked.

So, my question is, what would be a good read / write buffer size in this situation. I was thinking 2048 bytes each, but some sample code I saw in various blogs show wildly different values.

Thanks in advance for any ideas.


I think a better solution would be not to worry about the buffer sizes too much, but don't block the threads. If you pass a delegate to the callback parameter of the Begin* methods, that callback is executed when the operation completes and you can call End* from there, which will (almost) immediately return. No blocking necessary.

And regarding the buffer sizes, if they really matter to you, you should profile and find out what works best in your specific situation.


There's no definitive rule as to the actual values you should set, beyond avoiding the obvious extremes. It really depends on the type of data you're transfering, and how much of it there is. You probably want to set your write buffer quite high, but leave your read buffer lower. This is because writes are (usually) more expensive than reads when it comes to this kind of thing.

The best thing to do in this situation is try a few values and see how well they scale. You can always change them later if necessary.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号