gigi81 / sharpfastcgi

C# fastcgi protocol implementation plus shome usage examples. A good example on how to self-host your web application without the need of iis or mono.
MIT License
32 stars 15 forks source link

(A)synchronous pattern? #3

Closed OhSoGood closed 12 years ago

OhSoGood commented 12 years ago

Hi again,

Reading the TcpLayer class, I was a little surprised to see you are using synchronous read/write and not an asynchronous pattern. Is there any reason behind that (for instance, due to the nature of FastCGI, or because of the proximity of the front-end http server and the FastCGI app )?

gigi81 commented 12 years ago

The tcplayer read/write operations are synchronous because most webservers implements fastcgi in the most basic way sending just one request per channel. Having an asynchronous tcplayer in this case would mean creating/destroyng more threads than the one actually needed (one per request). Anyway the tcp server uses the asynchronous pattern to listen for new connections.

The design of the library is meant to easily implement an asynchronous version: for example all arrays instances are immutable that means thread safe. This is something I will probably do in a near future. However this would require a specialized asynchronous tcp layer and a more refined implementation than the SimpleFastCgiChannel we are using now.

Hope to have replied your question.

OhSoGood commented 12 years ago

Thanks for the reply. About your sentence "most webservers implements fastcgi in the most basic way" :

gigi81 commented 12 years ago

nginx supports only this method. there is a plugin to have the async fastcgi but it's not so stable so not a recomended solution. I'm not sure for the others. you can have a look.

fastcgi is meant to have a complete request sent to the "script" and receive back a complete reply (this is all done in memory). if you mean to stream videos this is absolutely NOT the way to go.