diff --git a/config/reverb.php b/config/reverb.php index ae77f839..b919ea11 100644 --- a/config/reverb.php +++ b/config/reverb.php @@ -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' => [ diff --git a/src/Servers/Reverb/Console/Commands/StartServer.php b/src/Servers/Reverb/Console/Commands/StartServer.php index 5768699d..ff805281 100644 --- a/src/Servers/Reverb/Console/Commands/StartServer.php +++ b/src/Servers/Reverb/Console/Commands/StartServer.php @@ -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}"); diff --git a/src/Servers/Reverb/Factory.php b/src/Servers/Reverb/Factory.php index b54c274a..33eb9ef2 100644 --- a/src/Servers/Reverb/Factory.php +++ b/src/Servers/Reverb/Factory.php @@ -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; @@ -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); diff --git a/tests/Feature/Reverb/ServerTest.php b/tests/Feature/Reverb/ServerTest.php index 17eaf2bc..a9449f55 100644 --- a/tests/Feature/Reverb/ServerTest.php +++ b/tests/Feature/Reverb/ServerTest.php @@ -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'); diff --git a/tests/ReverbTestCase.php b/tests/ReverbTestCase.php index e92e3c55..fbf254bb 100644 --- a/tests/ReverbTestCase.php +++ b/tests/ReverbTestCase.php @@ -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); } /**