Skip to content

Commit acdcab8

Browse files
authored
Merge pull request #242 from clue-labs/backlog
Change default socket backlog size to 511
2 parents 284d72d + f776265 commit acdcab8

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ $server = new React\Socket\Server('[::1]:8080', $loop, array(
425425
their defaults and effects of changing these may vary depending on your system
426426
and/or PHP version.
427427
Passing unknown context options has no effect.
428+
The `backlog` context option defaults to `511` unless given explicitly.
428429
For BC reasons, you can also pass the TCP socket context options as a simple
429430
array without wrapping this in another array under the `tcp` key.
430431

@@ -577,6 +578,7 @@ $server = new React\Socket\TcpServer('[::1]:8080', $loop, array(
577578
their defaults and effects of changing these may vary depending on your system
578579
and/or PHP version.
579580
Passing unknown context options has no effect.
581+
The `backlog` context option defaults to `511` unless given explicitly.
580582

581583
Whenever a client connects, it will emit a `connection` event with a connection
582584
instance implementing [`ConnectionInterface`](#connectioninterface):

src/TcpServer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ final class TcpServer extends EventEmitter implements ServerInterface
113113
* their defaults and effects of changing these may vary depending on your system
114114
* and/or PHP version.
115115
* Passing unknown context options has no effect.
116+
* The `backlog` context option defaults to `511` unless given explicitly.
116117
*
117118
* @param string|int $uri
118119
* @param LoopInterface $loop
@@ -158,7 +159,7 @@ public function __construct($uri, LoopInterface $loop, array $context = array())
158159
$errno,
159160
$errstr,
160161
\STREAM_SERVER_BIND | \STREAM_SERVER_LISTEN,
161-
\stream_context_create(array('socket' => $context))
162+
\stream_context_create(array('socket' => $context + array('backlog' => 511)))
162163
);
163164
if (false === $this->master) {
164165
throw new \RuntimeException('Failed to listen on "' . $uri . '": ' . $errstr, $errno);

tests/FunctionalTcpServerTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,38 @@ public function testEmitsConnectionWithLocalIpv6()
285285
$this->assertEquals($server->getAddress(), $local);
286286
}
287287

288+
public function testServerPassesContextOptionsToSocket()
289+
{
290+
$loop = Factory::create();
291+
292+
$server = new TcpServer(0, $loop, array(
293+
'backlog' => 4
294+
));
295+
296+
$ref = new \ReflectionProperty($server, 'master');
297+
$ref->setAccessible(true);
298+
$socket = $ref->getValue($server);
299+
300+
$context = stream_context_get_options($socket);
301+
302+
$this->assertEquals(array('socket' => array('backlog' => 4)), $context);
303+
}
304+
305+
public function testServerPassesDefaultBacklogSizeViaContextOptionsToSocket()
306+
{
307+
$loop = Factory::create();
308+
309+
$server = new TcpServer(0, $loop);
310+
311+
$ref = new \ReflectionProperty($server, 'master');
312+
$ref->setAccessible(true);
313+
$socket = $ref->getValue($server);
314+
315+
$context = stream_context_get_options($socket);
316+
317+
$this->assertEquals(array('socket' => array('backlog' => 511)), $context);
318+
}
319+
288320
public function testEmitsConnectionWithInheritedContextOptions()
289321
{
290322
if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.13', '<')) {

0 commit comments

Comments
 (0)