Skip to content

New function: stream_socket_listen() #431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

New function: stream_socket_listen() #431

wants to merge 1 commit into from

Conversation

rdlowrey
Copy link
Contributor

@rdlowrey rdlowrey commented Sep 5, 2013

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:

<?php
$stream = stream_socket_server(
    'tcp://0.0.0.0:0',
    $errno,
    $errstr,
    STREAM_SERVER_BIND
);
$sock = socket_import_stream($stream);
socket_listen($sock);

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:

<?php
$stream = stream_socket_server(
    'tcp://0.0.0.0:0',
    $errno,
    $errstr,
    STREAM_SERVER_BIND
);
// do stuff, fork, etc. Then in the child process:
stream_socket_listen($stream);

Existing functionality is not modified and there are no BC breaks. I.E. you can still bind + listen in a single step like before.

@ghost ghost assigned dsp Sep 16, 2013
@rdlowrey
Copy link
Contributor Author

I'm going to close this and resubmit at a later time when I'm able to add more functionality for the non-extension socket implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants