3 ms·
You never needed to prefork. One process can open a listening socket and share it with an unrelated process via file-descriptor passing.
by joosters 13y ago
You never needed to prefork. One process can open a listening socket and share it with an unrelated process via file-descriptor passing.
- rdtsc 13y agoThat is a cool trick! Does that work via pipes and maybe also unix sockets? I suspect latency might still be slightly better with a pool of pre-forked processes/threads.
- bratsche 13y agoYes, you can do it with unix sockets.
- joosters 13y agoYes, you can do it with unix sockets. Not sure about the latency, but you can pass the listen sockets in advance, rather than having one process accept()ing incoming connections and then passing those to other processes to handle. So unless you're binding to new ports all the time, it's all just a little extra startup work and won't impact the performance of the server.
- bdarnell 13y agoIt uses unix sockets. Latency is the same as the standard pre-forking model; the only difference is that file-descriptor passing lets you manage the worker processes independently instead of requiring them to have a common parent process (this is important when rolling out new code to a service with a lot of active connections, since it's disruptive to restart all the workers at once). Here's a demo in Python: https://gist.github.com/bdarnell/1073945 https://gist.github.com/bdarnell/1073945
- rdtsc 13y agoThat is pretty neat, thanks for sharing. I added it to my notes and code snippet for future.
- wmf 13y agoThat was covered in the LWN article: "The first of the traditional approaches is to have a single listener thread that accepts all incoming connections and then passes these off to other threads for processing. The problem with this approach is that the listening thread can become a bottleneck in extreme cases." But since most people are dealing with non-extreme cases, they should be aware of fd passing.
- joosters 13y agoThat sounds like the 'standard' model of several threads calling accept(), after all if you are multi-threaded you don't need to pass a file descriptor around, each thread will already have it. With FD passing, you can have multiple processes, related or unrelated, pulling incoming connections from the same socket. You use the FD passing to share the listening socket.