HttpWebRequest and HttpWebResponse ideal async buffer sizes

最后都变了- 提交于 2019-12-11 23:18:04

问题


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 to 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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/7666268/httpwebrequest-and-httpwebresponse-ideal-async-buffer-sizes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!