Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:68899 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 84431 invoked from network); 5 Sep 2013 17:30:38 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Sep 2013 17:30:38 -0000 Authentication-Results: pb1.pair.com smtp.mail=rdlowrey@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rdlowrey@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.223.169 as permitted sender) X-PHP-List-Original-Sender: rdlowrey@gmail.com X-Host-Fingerprint: 209.85.223.169 mail-ie0-f169.google.com Received: from [209.85.223.169] ([209.85.223.169:35253] helo=mail-ie0-f169.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F8/70-54585-BBFB8225 for ; Thu, 05 Sep 2013 13:30:36 -0400 Received: by mail-ie0-f169.google.com with SMTP id tp5so4488260ieb.0 for ; Thu, 05 Sep 2013 10:30:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=GXAOK4XjZnl3P/sr1YZd55MhXunAevAh87PVojOxuvY=; b=X86yPmR0wIgj/XzyKVbrx+BuZZjR/puY6Ektke+d8DTxCLm7N/jl6T18ExHwwHkzSd Qn2gQubEpxmKcgpI32bc+e64ZWzaGdPsye7ahMQTIoDNCmkWFLolyukowK5otbaHj1sg 2Sg/o0FlKniq0dANQDdSS5DMMIYNNi1BNhYWi3yKOanP4LnIPHIHdOho7EBFQTEwnrbA b6P/JJmQo+dP0Hj2k86CJzIXZZEkLrbhpctNdXVDuHOL7ItLpH51Ke4lOsPkT4zyaNHN 4nHfy6yFRSc0Eg+Fm4OQqtwcSh/qzDdhRVuIVJcC7Pn7wTC8TciAvsn105R64+WZjEKh 4zSQ== MIME-Version: 1.0 X-Received: by 10.50.87.74 with SMTP id v10mr6845448igz.27.1378402233390; Thu, 05 Sep 2013 10:30:33 -0700 (PDT) Received: by 10.50.157.199 with HTTP; Thu, 5 Sep 2013 10:30:33 -0700 (PDT) Date: Thu, 5 Sep 2013 13:30:33 -0400 Message-ID: To: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=089e0102f84a7aa15704e5a64762 Subject: New function: stream_socket_listen() From: rdlowrey@gmail.com (Daniel Lowrey) --089e0102f84a7aa15704e5a64762 Content-Type: text/plain; charset=ISO-8859-1 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 --089e0102f84a7aa15704e5a64762--