Skip to content

Commit fee5f36

Browse files
committed
Rename AccoutingServer to LimitingServer to avoid confusion
1 parent 00e0dc8 commit fee5f36

File tree

4 files changed

+59
-47
lines changed

4 files changed

+59
-47
lines changed

README.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ and [`Stream`](https://github.com/reactphp/stream) components.
2121
* [close()](#close)
2222
* [Server](#server)
2323
* [SecureServer](#secureserver)
24-
* [AccountingServer](#accountingserver)
24+
* [LimitingServer](#limitingserver)
2525
* [getConnections()](#getconnections)
2626
* [ConnectionInterface](#connectioninterface)
2727
* [getRemoteAddress()](#getremoteaddress)
@@ -380,20 +380,23 @@ If you use a custom `ServerInterface` and its `connection` event does not
380380
meet this requirement, the `SecureServer` will emit an `error` event and
381381
then close the underlying connection.
382382

383-
### AccountingServer
383+
### LimitingServer
384384

385-
The `AccountingServer` decorators wraps a given `ServerInterface` and is responsible
386-
for keeping track of open connections to this server instance.
385+
The `LimitingServer` decorator wraps a given `ServerInterface` and is responsible
386+
for limiting and keeping track of open connections to this server instance.
387387

388-
Whenever the underlying server emits a `connection` event, it will keep track
389-
of this connection by adding it to the list of open connections and then
390-
forward the `connection` event (unless its limits are exceeded).
388+
Whenever the underlying server emits a `connection` event, it will check its
389+
limits and then either
390+
- keep track of this connection by adding it to the list of
391+
open connections and then forward the `connection` event
392+
- or reject (close) the connection when its limits are exceeded and will
393+
forward an `error` event instead.
391394

392395
Whenever a connection closes, it will remove this connection from the list of
393396
open connections.
394397

395398
```php
396-
$server = new AccountingServer($server);
399+
$server = new LimitingServer($server, 100);
397400
$server->on('connection', function (ConnectionInterface $connection) {
398401
$connection->write('hello there!' . PHP_EOL);
399402
@@ -402,19 +405,25 @@ $server->on('connection', function (ConnectionInterface $connection) {
402405

403406
See also the [second example](examples) for more details.
404407

405-
You can optionally pass a maximum number of open connections to ensure
408+
You have to pass a maximum number of open connections to ensure
406409
the server will automatically reject (close) connections once this limit
407410
is exceeded. In this case, it will emit an `error` event to inform about
408411
this and no `connection` event will be emitted.
409412

410413
```php
411-
$server = new AccountingServer($server, 50);
414+
$server = new LimitingServer($server, 100);
412415
$server->on('connection', function (ConnectionInterface $connection) {
413416
$connection->write('hello there!' . PHP_EOL);
414417
415418
});
416419
```
417420

421+
You MAY pass a `null` limit in order to put no limit on the number of
422+
open connections and keep accepting new connection until you run out of
423+
operating system resources (such as open file handles). This may be
424+
useful it you do not want to take care of applying a limit but still want
425+
to use the `getConnections()` method.
426+
418427
You can optionally configure the server to pause accepting new
419428
connections once the connection limit is reached. In this case, it will
420429
pause the underlying server and no longer process any new connections at
@@ -431,7 +440,7 @@ protocols that demand immediate responses (such as a "welcome" message in
431440
an interactive chat).
432441

433442
```php
434-
$server = new AccountingServer($server, 50, true);
443+
$server = new LimitingServer($server, 100, true);
435444
$server->on('connection', function (ConnectionInterface $connection) {
436445
$connection->write('hello there!' . PHP_EOL);
437446

examples/02-chat-server.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use React\Socket\Server;
1616
use React\Socket\ConnectionInterface;
1717
use React\Socket\SecureServer;
18-
use React\Socket\AccountingServer;
18+
use React\Socket\LimitingServer;
1919

2020
require __DIR__ . '/../vendor/autoload.php';
2121

@@ -30,7 +30,7 @@
3030
));
3131
}
3232

33-
$server = new AccountingServer($server);
33+
$server = new LimitingServer($server, null);
3434

3535
$server->on('connection', function (ConnectionInterface $client) use ($server) {
3636
// whenever a new message comes in
Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55
use Evenement\EventEmitter;
66

77
/**
8-
* The `AccountingServer` decorators wraps a given `ServerInterface` and is responsible
9-
* for keeping track of open connections to this server instance.
8+
* The `LimitingServer` decorator wraps a given `ServerInterface` and is responsible
9+
* for limiting and keeping track of open connections to this server instance.
1010
*
11-
* Whenever the underlying server emits a `connection` event, it will keep track
12-
* of this connection by adding it to the list of open connections and then
13-
* forward the `connection` event (unless its limits are exceeded).
11+
* Whenever the underlying server emits a `connection` event, it will check its
12+
* limits and then either
13+
* - keep track of this connection by adding it to the list of
14+
* open connections and then forward the `connection` event
15+
* - or reject (close) the connection when its limits are exceeded and will
16+
* forward an `error` event instead.
1417
*
1518
* Whenever a connection closes, it will remove this connection from the list of
1619
* open connections.
1720
*
1821
* ```php
19-
* $server = new AccountingServer($server);
22+
* $server = new LimitingServer($server, 100);
2023
* $server->on('connection', function (ConnectionInterface $connection) {
2124
* $connection->write('hello there!' . PHP_EOL);
2225
* …
@@ -28,7 +31,7 @@
2831
* @see ServerInterface
2932
* @see ConnectionInterface
3033
*/
31-
class AccountingServer extends EventEmitter implements ServerInterface
34+
class LimitingServer extends EventEmitter implements ServerInterface
3235
{
3336
private $connections = array();
3437
private $server;
@@ -39,21 +42,27 @@ class AccountingServer extends EventEmitter implements ServerInterface
3942
private $manuPaused = false;
4043

4144
/**
42-
* Instantiates a new AccountingServer.
45+
* Instantiates a new LimitingServer.
4346
*
44-
* You can optionally pass a maximum number of open connections to ensure
47+
* You have to pass a maximum number of open connections to ensure
4548
* the server will automatically reject (close) connections once this limit
4649
* is exceeded. In this case, it will emit an `error` event to inform about
4750
* this and no `connection` event will be emitted.
4851
*
4952
* ```php
50-
* $server = new AccountingServer($server, 50);
53+
* $server = new LimitingServer($server, 100);
5154
* $server->on('connection', function (ConnectionInterface $connection) {
5255
* $connection->write('hello there!' . PHP_EOL);
5356
* …
5457
* });
5558
* ```
5659
*
60+
* You MAY pass a `null` limit in order to put no limit on the number of
61+
* open connections and keep accepting new connection until you run out of
62+
* operating system resources (such as open file handles). This may be
63+
* useful it you do not want to take care of applying a limit but still want
64+
* to use the `getConnections()` method.
65+
*
5766
* You can optionally configure the server to pause accepting new
5867
* connections once the connection limit is reached. In this case, it will
5968
* pause the underlying server and no longer process any new connections at
@@ -70,18 +79,18 @@ class AccountingServer extends EventEmitter implements ServerInterface
7079
* an interactive chat).
7180
*
7281
* ```php
73-
* $server = new AccountingServer($server, 50, true);
82+
* $server = new LimitingServer($server, 100, true);
7483
* $server->on('connection', function (ConnectionInterface $connection) {
7584
* $connection->write('hello there!' . PHP_EOL);
7685
* …
7786
* });
7887
* ```
7988
*
8089
* @param ServerInterface $server
81-
* @param null|int $connectionLimit
90+
* @param int|null $connectionLimit
8291
* @param bool $pauseOnLimit
8392
*/
84-
public function __construct(ServerInterface $server, $connectionLimit = null, $pauseOnLimit = false)
93+
public function __construct(ServerInterface $server, $connectionLimit, $pauseOnLimit = false)
8594
{
8695
$this->server = $server;
8796
$this->limit = $connectionLimit;
Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,19 @@
22

33
namespace React\Tests\Socket;
44

5-
use React\Socket\AccountingServer;
5+
use React\Socket\LimitingServer;
66
use React\Socket\Server;
77
use React\EventLoop\Factory;
88
use Clue\React\Block;
99

10-
class AccountingServerTest extends TestCase
10+
class LimitingServerTest extends TestCase
1111
{
12-
public function testA()
13-
{
14-
$server = $this->getMockBuilder('React\Socket\ServerInterface')->getMock();
15-
$server = new AccountingServer($server);
16-
}
17-
1812
public function testGetAddressWillBePassedThroughToTcpServer()
1913
{
2014
$tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock();
2115
$tcp->expects($this->once())->method('getAddress')->willReturn('127.0.0.1:1234');
2216

23-
$server = new AccountingServer($tcp);
17+
$server = new LimitingServer($tcp, 100);
2418

2519
$this->assertEquals('127.0.0.1:1234', $server->getAddress());
2620
}
@@ -30,7 +24,7 @@ public function testPauseWillBePassedThroughToTcpServer()
3024
$tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock();
3125
$tcp->expects($this->once())->method('pause');
3226

33-
$server = new AccountingServer($tcp);
27+
$server = new LimitingServer($tcp, 100);
3428

3529
$server->pause();
3630
}
@@ -40,7 +34,7 @@ public function testPauseTwiceWillBePassedThroughToTcpServerOnce()
4034
$tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock();
4135
$tcp->expects($this->once())->method('pause');
4236

43-
$server = new AccountingServer($tcp);
37+
$server = new LimitingServer($tcp, 100);
4438

4539
$server->pause();
4640
$server->pause();
@@ -51,7 +45,7 @@ public function testResumeWillBePassedThroughToTcpServer()
5145
$tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock();
5246
$tcp->expects($this->once())->method('resume');
5347

54-
$server = new AccountingServer($tcp);
48+
$server = new LimitingServer($tcp, 100);
5549

5650
$server->pause();
5751
$server->resume();
@@ -62,7 +56,7 @@ public function testResumeTwiceWillBePassedThroughToTcpServerOnce()
6256
$tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock();
6357
$tcp->expects($this->once())->method('resume');
6458

65-
$server = new AccountingServer($tcp);
59+
$server = new LimitingServer($tcp, 100);
6660

6761
$server->pause();
6862
$server->resume();
@@ -74,7 +68,7 @@ public function testCloseWillBePassedThroughToTcpServer()
7468
$tcp = $this->getMockBuilder('React\Socket\ServerInterface')->getMock();
7569
$tcp->expects($this->once())->method('close');
7670

77-
$server = new AccountingServer($tcp);
71+
$server = new LimitingServer($tcp, 100);
7872

7973
$server->close();
8074
}
@@ -85,7 +79,7 @@ public function testSocketErrorWillBeForwarded()
8579

8680
$tcp = new Server(0, $loop);
8781

88-
$server = new AccountingServer($tcp);
82+
$server = new LimitingServer($tcp, 100);
8983

9084
$server->on('error', $this->expectCallableOnce());
9185

@@ -100,7 +94,7 @@ public function testSocketConnectionWillBeForwarded()
10094

10195
$tcp = new Server(0, $loop);
10296

103-
$server = new AccountingServer($tcp);
97+
$server = new LimitingServer($tcp, 100);
10498
$server->on('connection', $this->expectCallableOnceWith($connection));
10599
$server->on('error', $this->expectCallableNever());
106100

@@ -120,7 +114,7 @@ public function testSocketConnectionWillBeClosedOnceLimitIsReached()
120114

121115
$tcp = new Server(0, $loop);
122116

123-
$server = new AccountingServer($tcp, 1);
117+
$server = new LimitingServer($tcp, 1);
124118
$server->on('connection', $this->expectCallableOnceWith($first));
125119
$server->on('error', $this->expectCallableOnce());
126120

@@ -138,7 +132,7 @@ public function testPausingServerWillBePausedOnceLimitIsReached()
138132

139133
$connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
140134

141-
$server = new AccountingServer($tcp, 1, true);
135+
$server = new LimitingServer($tcp, 1, true);
142136

143137
$tcp->emit('connection', array($connection));
144138
}
@@ -152,7 +146,7 @@ public function testSocketDisconnectionWillRemoveFromList()
152146
$socket = stream_socket_client('tcp://' . $tcp->getAddress());
153147
fclose($socket);
154148

155-
$server = new AccountingServer($tcp);
149+
$server = new LimitingServer($tcp, 100);
156150
$server->on('connection', $this->expectCallableOnce());
157151
$server->on('error', $this->expectCallableNever());
158152

@@ -166,7 +160,7 @@ public function testPausingServerWillEmitOnlyOneButAcceptTwoConnectionsDueToOper
166160
$loop = Factory::create();
167161

168162
$server = new Server(0, $loop);
169-
$server = new AccountingServer($server, 1, true);
163+
$server = new LimitingServer($server, 1, true);
170164
$server->on('connection', $this->expectCallableOnce());
171165
$server->on('error', $this->expectCallableNever());
172166

@@ -187,7 +181,7 @@ public function testPausingServerWillEmitTwoConnectionsFromBacklog()
187181
$twice->expects($this->exactly(2))->method('__invoke');
188182

189183
$server = new Server(0, $loop);
190-
$server = new AccountingServer($server, 1, true);
184+
$server = new LimitingServer($server, 1, true);
191185
$server->on('connection', $twice);
192186
$server->on('error', $this->expectCallableNever());
193187

0 commit comments

Comments
 (0)