Skip to content

Commit

Permalink
Refactoring + optimization for 8K+ connections
Browse files Browse the repository at this point in the history
  • Loading branch information
alexslipknot committed Nov 25, 2017
1 parent 4233f27 commit 7013693
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
9 changes: 3 additions & 6 deletions src/Master.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
class Master
{
private $workers = [];
private $clients = [];
private $connector;

/**
Expand All @@ -18,7 +17,7 @@ class Master
*/
public function __construct($workers, $connector)
{
$this->clients = $this->workers = $workers;
$this->workers = $workers;
$this->connector = $connector;
}

Expand All @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions src/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7013693

Please sign in to comment.