by qwertox · 4y ago · 0 comments
When I read that multiple processes can use the port it finally made sense to me how nginx is able to spawn so many workers. If they are listening to port 80 or 443 with SO_REUSEPORT, each worker would get a new client in a round robin fashion, effectively spreading the workload without depending on the main parent process to pipe the data to them (and back to the client), which is how I thought that it was working.
The funny thing about this submission was that when I read the title I somehow thought I was reading it on Stack Overflow and was expecting a noob asking this question and didn't know what kind of answers I was about to be confronted with and if I should get excited to see a really good one.
Then, while in the article, I liked how it started to raise so many issues, fanning out into these special cases which really made it a valid question to ask what it does mean to listen to a port, how complicated it can become.
Sadly the article does not go into any detail, like showing snippets of C code from the networking stack and explaining how it is really working, how each requirement (or kluge, for that matter) is being satisfied, but it's OK, it's an article which touches the subject in an enticing enough way while having an excuse to not deep-dive into the details by keeping it in the form of a dialog between two students.
I liked it.
I think this point deserves extra attention. I've been finding that in situations like this it is often much faster to read the implementation source code than try to work it out from documentation and trial-and-error. Something in this (quite realistic) vignette is that there is a lot of potential to mis-learn when the Linux source code for this is probably quite short and readable.
This is a particularly useful skill for terse languages where the source code is easy to read. And a good time to feel a bit of empathy towards the poor souls who are forced to use closed source software.
In fact, I now know what I'll be skimming through tonight ... [0]. Not short, but a lot more useful than guesswork.
[0] https://github.com/torvalds/linux/blob/master/include/net/so...
But, when you say "I am OK with this co-existing", you can listen to a port that is in TIMEWAIT, and quick restart is then possible.
It’s the socket descriptor that matters. When the listen call unblocks and the thread accepts the connection it gets a new socket descriptor, it’s through this socket the subsequent read/write happens for the connection.