Skip to content

Commit

Permalink
#45: Change clean up code by refactoring looping method
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Jan 11, 2020
1 parent 0b32dd9 commit f0bf18f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
4 changes: 3 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
/tests export-ignore
/phpunit.xml export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.gitignore export-ignore
/CODE_OF_CONDUCT.md export-ignore
/CONTRIBUTING.md export-ignore
27 changes: 27 additions & 0 deletions src/Components/WssMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,31 @@ private function getTypeByOpCode(string $firstByteBinary, array &$decodedData)
break;
}
}

/**
* Checks if there are less connections for amount of processes
* @param int $totalClients
* @param int $maxClients
*/
protected function lessConnThanProc(int $totalClients, int $maxClients): void
{
if ($totalClients !== 0 && $maxClients > $totalClients
&& $totalClients % $this->config->getClientsPerFork() === 0) {
exit(1);
}
}

/**
* Clean socket resources that were closed,
* thus avoiding (stream_select(): supplied resource is not a valid stream resource)
* @param array $readSocks
*/
protected function cleanSocketResources(array &$readSocks): void
{
foreach ($readSocks as $k => $sock) {
if (!is_resource($sock)) {
unset($readSocks[$k]);
}
}
}
}
17 changes: 4 additions & 13 deletions src/WebSocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
*/
class WebSocketServer extends WssMain implements WebSocketServerContract
{
protected $config;

private $clients = [];
// set any template You need ex.: GET /subscription/messenger/token
private $pathParams = [];
private $config;
private $handshakes = [];
private $headersUpgrade = [];
private $totalClients = 0;
Expand Down Expand Up @@ -128,22 +129,12 @@ private function looping($server)
$this->stepRecursion = false;
$this->eventLoop($server, true);
}

if ($this->totalClients !== 0 && $this->maxClients > $this->totalClients
&& $this->totalClients % $this->config->getClientsPerFork() === 0) { // there is less connection for amount of processes at this moment
exit(1);
}
$this->lessConnThanProc($this->totalClients, $this->maxClients);

//prepare readable sockets
$readSocks = $this->clients;
$readSocks[] = $server;

// clear socket resources that were closed, thus avoiding (stream_select(): supplied resource is not a valid stream resource)
foreach ($readSocks as $k => $sock) {
if (!is_resource($sock)) {
unset($readSocks[$k]);
}
}
$this->cleanSocketResources($readSocks);

//start reading and use a large timeout
if (!stream_select($readSocks, $write, $except, $this->config->getStreamSelectTimeout())) {
Expand Down

0 comments on commit f0bf18f

Please sign in to comment.