init
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LayLink\Node;
|
||||
|
||||
use Workerman\Connection\TcpConnection;
|
||||
|
||||
final class NodeConnection
|
||||
{
|
||||
public int $lastHeartbeat;
|
||||
public int $activeSessions = 0;
|
||||
|
||||
public function __construct(
|
||||
public readonly string $nodeId,
|
||||
public readonly string $nodeType,
|
||||
public readonly string $nodeZone,
|
||||
public readonly TcpConnection $connection,
|
||||
) {
|
||||
$this->lastHeartbeat = time();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LayLink\Node;
|
||||
|
||||
use Workerman\Connection\TcpConnection;
|
||||
|
||||
final class NodeRegistry
|
||||
{
|
||||
/** @var array<string, NodeConnection> */
|
||||
private array $nodes = [];
|
||||
|
||||
public function register(NodeConnection $node): void
|
||||
{
|
||||
$this->nodes[$node->nodeId] = $node;
|
||||
}
|
||||
|
||||
public function unregisterByConnection(TcpConnection $connection): ?NodeConnection
|
||||
{
|
||||
foreach ($this->nodes as $nodeId => $node) {
|
||||
if ($node->connection === $connection) {
|
||||
unset($this->nodes[$nodeId]);
|
||||
return $node;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function get(string $nodeId): ?NodeConnection
|
||||
{
|
||||
return $this->nodes[$nodeId] ?? null;
|
||||
}
|
||||
|
||||
public function isOnline(string $nodeId): bool
|
||||
{
|
||||
return isset($this->nodes[$nodeId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return NodeConnection[]
|
||||
*/
|
||||
public function all(): array
|
||||
{
|
||||
return array_values($this->nodes);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user