Non-forking (aka single-threaded or select()
-based) webservers like lighttpd or nginx are
gaining in popularity more and more.
While there is a multitude of documents explaining forking servers (at various levels of detail), documentation for non-forking servers is sparse.
I am looking for a bird eyes view of how a non-forking web server works. (Pseudo-)code or a state machine diagram, stripped down to the bare minimum, would be great.
I am aware of the following resources and found them helpful.
However, I am interested in the principles, not implementation details.
Specifically:
Why is this type of server sometimes called non-blocking, when select()
essentially blocks?
Processing of a request can take some time. What happens with new requests during this time when there is no specific listener thread or process? Is the request processing somehow interrupted or time sliced?
Edit: As I understand it, while a request is processed (e.g file read or CGI script run) the server cannot accept new connections. Wouldn't this mean that such a server could miss a lot of new connections if a CGI script runs for, let's say, 2 seconds or so?
Basic pseudocode:
setup
while true
select/poll/kqueue
with fd needing action do
read/write fd
if fd was read and well formed request in buffer
service request
other stuff
select()
& friends block, socket I/O is not blocking. You're only blocked until you have something fun to do.service request
above typically means opening a file, adding it to the list for select
, and noting that stuff read from there goes out to a certain socket. Substitute FastCGI for file when appropriate.EDIT:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With