mattgodbolt / seasocks

Simple, small, C++ embeddable webserver with WebSockets support
BSD 2-Clause "Simplified" License
724 stars 120 forks source link

Server: add SO_REUSEPORT socket option if available #146

Closed kanak closed 3 years ago

kanak commented 3 years ago

This allows us to have multiple threads listening on the same port in Linux. Also add an example demonstrating usage.

kanak commented 3 years ago

Hi @mattgodbolt , please let me know if the changes look good to you and if you'd like further edits.

I need this change to move my application on to seasocks; my websocket commands often result in intensive computation and the easiest way to move things over without rearchitecting everything was to just run N threads per process and let each thread do blocking work if necessary.

codecov[bot] commented 3 years ago

Codecov Report

Merging #146 (6db2169) into master (5cc8f38) will decrease coverage by 0.00%. The diff coverage is 50.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #146      +/-   ##
==========================================
- Coverage   38.71%   38.70%   -0.01%     
==========================================
  Files          52       52              
  Lines        2379     2382       +3     
==========================================
+ Hits          921      922       +1     
- Misses       1458     1460       +2     
Impacted Files Coverage Δ
src/main/c/Server.cpp 29.64% <50.00%> (+0.03%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 5cc8f38...6db2169. Read the comment docs.

offa commented 3 years ago

Sorry for the late response. Since SO_REUSEADDR is already set, I don't see any drawbacks.

@kanak / @mattgodbolt: Does it make sense to set this via option instead of unconditionally?

mattgodbolt commented 3 years ago

Thansk both! Sorry I didn't spot this earlier. I've since fixed up my email filters so I should spot things in future!

kanak commented 3 years ago

Sorry for the late response. Since SO_REUSEADDR is already set, I don't see any drawbacks.

@kanak / @mattgodbolt: Does it make sense to set this via option instead of unconditionally?

Hi @offa,

I think a flag just for SO_REUSEPORT might not be a good idea, because SO_REUSEADDR by itself on Linux has some annoying limitations ( https://stackoverflow.com/a/14388707) and SO_REUSEPORT has some nice performance implications ( https://lwn.net/Articles/542629/ ).

However, a flag to disable both REUSE flags altogether might be a good idea? That way you could be sure that there is no other thread/process on the same host that is also listening on the same port.

offa commented 3 years ago

I see, so we can add such a flag later (if even necessary).

mattgodbolt commented 3 years ago

Agreed we can add flags for either or both later if needed. Thanks!