Seems reasonable to me, but Wez should probably weigh in on it.  I vaguely
recall a conversation with him when he first implemented stream_socket_*()
and a reason why listen wasn't in the API.

-Sara


On Thu, Sep 5, 2013 at 10:30 AM, Daniel Lowrey <rdlow...@gmail.com> wrote:

> 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 it's not possible 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:
>
> $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:
>
> $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($server);
>
> Existing functionality is not modified and there are no BC breaks. I.E. you
> can still bind + listen in a single step like before:
>
> $stream = stream_socket_server('tcp://0.0.0.0:0', $errNo, $errStr,
> STREAM_SERVER_BIND | STREAM_SERVER_LISTEN);
>
> This addition seemed a bit trivial for an RFC, so and I didn't bother
> hitting the wiki. The link to the relevant pull request follows. Any
> thoughts, comments and opinions are welcome:
>
> https://github.com/php/php-src/pull/431
>

Reply via email to