Skip to content

Commit

Permalink
implement connection limit
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon committed Nov 27, 2023
1 parent a1c15ca commit 4efc296
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions config/reverb.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
'enabled' => env('REVERB_SCALING_ENABLED', false),
'channel' => env('REVERB_SCALING_CHANNEL', 'reverb'),
],
'connection_limit' => env('REVERB_CONNECTION_LIMIT', null),
],

'api_gateway' => [
Expand Down
2 changes: 1 addition & 1 deletion src/Servers/Reverb/Console/Commands/StartServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function handle(): void
$this->subscribeToRedis($loop);
$this->scheduleCleanup($loop);

$server = ServerFactory::make($host, $port, $loop);
$server = ServerFactory::make($host, $port, $config['connection_limit'], $loop);

$this->components->info("Starting server on {$host}:{$port}");

Expand Down
8 changes: 6 additions & 2 deletions src/Servers/Reverb/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Laravel\Reverb\Server;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Socket\LimitingServer;
use React\Socket\SocketServer;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
Expand All @@ -25,10 +26,13 @@ class Factory
/**
* Create a new WebSocket server instance.
*/
public static function make(string $host = '0.0.0.0', string $port = '8080', LoopInterface $loop = null)
public static function make(string $host = '0.0.0.0', string $port = '8080', ?int $connectionLimit = null, LoopInterface $loop = null)
{
$loop = $loop ?: Loop::get();
$socket = new SocketServer("{$host}:{$port}", [], $loop);
$socket = new LimitingServer(
new SocketServer("{$host}:{$port}", [], $loop), $connectionLimit
);

$router = new Router(new UrlMatcher(static::routes(), new RequestContext));

return new HttpServer($socket, $router, $loop);
Expand Down
9 changes: 9 additions & 0 deletions tests/Feature/Reverb/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,15 @@
$this->connect();
});

it('cconnections can be limited', function () {
$this->app['config']->set('reverb.servers.reverb.connection_limit', 1);
$this->stopServer();
$this->startServer();
$this->connect();

$this->connect();
})->throws('Connection closed before handshake');

it('clears application state between requests', function () {
$this->subscribe('test-channel');

Expand Down
2 changes: 1 addition & 1 deletion tests/ReverbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function usingRedis()
public function startServer($host = '0.0.0.0', $port = '8080')
{
$this->resetFiber();
$this->server = Factory::make($host, $port, $this->loop);
$this->server = Factory::make($host, $port, config('reverb.servers.reverb.connection_limit'), $this->loop);
}

/**
Expand Down

0 comments on commit 4efc296

Please sign in to comment.