From 70136936d9f08af81a3e383e84139220a7cf1c53 Mon Sep 17 00:00:00 2001 From: Olexy Date: Sat, 25 Nov 2017 19:38:42 +0200 Subject: [PATCH] Refactoring + optimization for 8K+ connections --- src/Master.php | 9 +++------ src/Worker.php | 6 ++++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Master.php b/src/Master.php index e47bd5f..1d444ab 100644 --- a/src/Master.php +++ b/src/Master.php @@ -7,7 +7,6 @@ class Master { private $workers = []; - private $clients = []; private $connector; /** @@ -18,7 +17,7 @@ class Master */ public function __construct($workers, $connector) { - $this->clients = $this->workers = $workers; + $this->workers = $workers; $this->connector = $connector; } @@ -30,12 +29,10 @@ public function __construct($workers, $connector) public function dispatch(): void { while (true) { - $read = $this->clients; + $read = $this->workers; $read[] = $this->connector; - if (!@stream_select($read, $write, $except, null)) { - continue; - } + @stream_select($read, $write, $except, null); foreach ($read as $client) { if ($client === $this->connector) { diff --git a/src/Worker.php b/src/Worker.php index a29fa73..1d47aa7 100644 --- a/src/Worker.php +++ b/src/Worker.php @@ -35,8 +35,10 @@ public function __construct($server, $master) */ protected function sendToAll(string $msg, bool $global = true): void { - foreach ($this->clients as $client) { - @fwrite($client, $msg); + if (!empty($this->clients) && stream_select($read, $this->clients, $except, 0)) { + foreach ($this->clients as $client) { + @fwrite($client, $msg); + } } if ($global) {