diff --git a/README.md b/README.md index 31d8def4..7b35ac3e 100644 --- a/README.md +++ b/README.md @@ -425,6 +425,7 @@ $server = new React\Socket\Server('[::1]:8080', $loop, array( their defaults and effects of changing these may vary depending on your system and/or PHP version. Passing unknown context options has no effect. + The `backlog` context option defaults to `511` unless given explicitly. For BC reasons, you can also pass the TCP socket context options as a simple array without wrapping this in another array under the `tcp` key. @@ -577,6 +578,7 @@ $server = new React\Socket\TcpServer('[::1]:8080', $loop, array( their defaults and effects of changing these may vary depending on your system and/or PHP version. Passing unknown context options has no effect. +The `backlog` context option defaults to `511` unless given explicitly. Whenever a client connects, it will emit a `connection` event with a connection instance implementing [`ConnectionInterface`](#connectioninterface): diff --git a/src/TcpServer.php b/src/TcpServer.php index 8f074323..6d5503b5 100644 --- a/src/TcpServer.php +++ b/src/TcpServer.php @@ -113,6 +113,7 @@ final class TcpServer extends EventEmitter implements ServerInterface * their defaults and effects of changing these may vary depending on your system * and/or PHP version. * Passing unknown context options has no effect. + * The `backlog` context option defaults to `511` unless given explicitly. * * @param string|int $uri * @param LoopInterface $loop @@ -158,7 +159,7 @@ public function __construct($uri, LoopInterface $loop, array $context = array()) $errno, $errstr, \STREAM_SERVER_BIND | \STREAM_SERVER_LISTEN, - \stream_context_create(array('socket' => $context)) + \stream_context_create(array('socket' => $context + array('backlog' => 511))) ); if (false === $this->master) { throw new \RuntimeException('Failed to listen on "' . $uri . '": ' . $errstr, $errno); diff --git a/tests/FunctionalTcpServerTest.php b/tests/FunctionalTcpServerTest.php index dcceba95..3f228a06 100644 --- a/tests/FunctionalTcpServerTest.php +++ b/tests/FunctionalTcpServerTest.php @@ -285,6 +285,38 @@ public function testEmitsConnectionWithLocalIpv6() $this->assertEquals($server->getAddress(), $local); } + public function testServerPassesContextOptionsToSocket() + { + $loop = Factory::create(); + + $server = new TcpServer(0, $loop, array( + 'backlog' => 4 + )); + + $ref = new \ReflectionProperty($server, 'master'); + $ref->setAccessible(true); + $socket = $ref->getValue($server); + + $context = stream_context_get_options($socket); + + $this->assertEquals(array('socket' => array('backlog' => 4)), $context); + } + + public function testServerPassesDefaultBacklogSizeViaContextOptionsToSocket() + { + $loop = Factory::create(); + + $server = new TcpServer(0, $loop); + + $ref = new \ReflectionProperty($server, 'master'); + $ref->setAccessible(true); + $socket = $ref->getValue($server); + + $context = stream_context_get_options($socket); + + $this->assertEquals(array('socket' => array('backlog' => 511)), $context); + } + public function testEmitsConnectionWithInheritedContextOptions() { if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.13', '<')) {