New function: stream_socket_listen() #431
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The stream socket functions are incredibly useful and obviate the need for the sockets extension for the vast majority of potential use-cases. However, it's currently not possible to bind a socket and listen for connections in separate steps using
stream_socket_server()
.This can be done with the aid of ext/sockets like so:
Why is this useful? Well, for example, binding to a port in the main process and then listening individually in child forks/threads trivializes scaling socket servers. By listening on the same bound socket in multiple processes you get the advantage of the OS distributing new client connections to the individual workers instead of routing them in userland. Additionally, newer linux kernel versions (3.9x) now support the SO_REUSEPORT socket option which only makes this functionality more attractive. Admittedly you still need ext/sockets to reap the benefits of SO_REUSEPORT, but this may not always be the case.
My proposed patch adds a very simple function so that listening may be decoupled from binding without the need for ext/sockets:
Existing functionality is not modified and there are no BC breaks. I.E. you can still bind + listen in a single step like before.