Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a non-forking web server work?

Tags:

algorithm

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?

like image 336
Ludwig Weinzierl Avatar asked Feb 04 '23 11:02

Ludwig Weinzierl


1 Answers

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
  • Though select() & friends block, socket I/O is not blocking. You're only blocked until you have something fun to do.
  • Processing individual requests normally involved reading a file descriptor from a file (static resource) or process (dynamic resource) and then writing to the socket. This can be done handily without keeping much state.
  • So 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:

  • Not sure about the others, but nginx has 2 processes: a master and a worker. The master does the listening and then feeds the accepted connection to the worker for processing.
like image 140
dwc Avatar answered Feb 24 '23 15:02

dwc