This commit is contained in:
2023-08-28 12:01:30 +08:00
parent 4a607a5687
commit 9b4f5d59dc
7 changed files with 234 additions and 192 deletions
+86 -74
View File
@@ -8,91 +8,103 @@ use Swoole\Coroutine;
class ActorManager class ActorManager
{ {
/** @var array<string, ActorInterface> */ /** @var array<string, ActorInterface> */
private array $nodes = []; private array $nodes = [];
/** /**
* @param Actor $actor * @param Actor $actor
* @return void * @return void
*/ */
public function addActor(ActorInterface $actor): void public function addActor(ActorInterface $actor): void
{ {
$this->nodes[$actor->uniqueId] = $actor; $this->nodes[$actor->uniqueId] = $actor;
Coroutine::create(function (Actor $actor) { Coroutine::create(function (Actor $actor) {
$actor->run(); $actor->run();
}, $actor); }, $actor);
} }
/** /**
* @param $name * @param $name
* @return void * @return void
*/ */
public function closeActor($name): void public function closeActor($name): void
{ {
$node = $this->nodes[$name] ?? null; $node = $this->nodes[$name] ?? null;
if (is_null($node)) { if (is_null($node)) {
return; return;
} }
foreach ($node as $actor) { foreach ($node as $actor) {
$actor->shutdown(); $actor->shutdown();
} }
} }
/** /**
* @param $name * @param $name
* @param $message * @param $message
* @return bool * @return bool
*/ */
public function write($name, $message): bool public function write($name, $message): bool
{ {
$actor = $this->nodes[$name] ?? null; $actor = $this->nodes[$name] ?? null;
if (is_null($actor)) { if (is_null($actor)) {
return false; return false;
} }
return $actor->write($message); return $actor->write($message);
} }
/** /**
* @param $name * @param $name
* @return array * @return array
*/ */
public function lists($name): array public function lists($name): array
{ {
$array = []; $array = [];
foreach ($this->nodes[$name] as $actor) { foreach ($this->nodes[$name] as $actor) {
$array[] = [ $array[] = [
'id' => $actor->getName(), 'id' => $actor->getName(),
'state' => $actor->getState()->name, 'state' => $actor->getState()->name,
'runTime' => $actor->getRunTime() 'runTime' => $actor->getRunTime()
]; ];
} }
return $array; return $array;
} }
/** /**
* @param string $uniqueId * @param string $uniqueId
* @return bool * @return bool
*/ */
public function hasActor(string $uniqueId): bool public function hasActor(string $uniqueId): bool
{ {
return isset($this->nodes[$uniqueId]) && $this->nodes[$uniqueId] instanceof ActorInterface; return isset($this->nodes[$uniqueId]) && $this->nodes[$uniqueId] instanceof ActorInterface;
} }
/** /**
* @return void * @param array|null $data
*/ * @return void
public function clean(): void */
{ public static function exec(?array $data): void
foreach ($this->nodes as $actor) { {
$actor->shutdown(); if (is_null($data)) {
} return;
$this->nodes = []; }
} }
/**
* @return void
*/
public function clean(): void
{
foreach ($this->nodes as $actor) {
$actor->shutdown();
}
$this->nodes = [];
}
} }
+4 -1
View File
@@ -3,6 +3,8 @@ declare(strict_types=1);
namespace Kiri\Actor; namespace Kiri\Actor;
use JetBrains\PhpStorm\ArrayShape;
class ActorMessage implements \JsonSerializable class ActorMessage implements \JsonSerializable
{ {
@@ -63,7 +65,8 @@ class ActorMessage implements \JsonSerializable
/** /**
* @return array * @return array
*/ */
public function jsonSerialize(): array #[ArrayShape(['userId' => "int", 'event' => "string", 'body' => "array"])]
public function jsonSerialize(): array
{ {
return [ return [
'userId' => $this->userId, 'userId' => $this->userId,
+55
View File
@@ -0,0 +1,55 @@
<?php
namespace Kiri\Actor;
use Kiri\Server\Abstracts\BaseProcess;
use Swoole\Coroutine;
use Swoole\Process;
class ActorProcess extends BaseProcess
{
/**
* @var bool
*/
protected bool $enable_coroutine = true;
/**
* @return string
*/
public function getName(): string
{
// TODO: Change the autogenerated stub
return '[' . \config('id', 'system-service') . '].Actor Manager';
}
public function process(?Process $process): void
{
// TODO: Implement process() method.
while ($this->isStop() === false) {
$read = $process->read();
ActorManager::exec(json_decode($read, true));
Coroutine::sleep(1000 / 120);
}
}
/**
* @return $this
*/
public function onSigterm(): static
{
// TODO: Implement onSigterm() method.
Coroutine::create(function () {
$sign = Coroutine::waitSignal(SIGINT | SIGTERM);
if ($sign) {
$this->onShutdown(true);
}
});
return $this;
}
}
+62 -52
View File
@@ -22,66 +22,76 @@ class Component implements Configure
{ {
/** /**
* BaseAbstract constructor. * BaseAbstract constructor.
*/ */
public function __construct() public function __construct()
{ {
} }
/** /**
* @return void * @return void
*/ */
public function init(): void public function init(): void
{ {
} }
/** /**
* @return string * @return string
*/ */
#[Pure] public static function className(): string #[Pure] public static function className(): string
{ {
return static::class; return static::class;
} }
/** /**
* @param string $name * @return Kiri\Error\StdoutLogger
* @return mixed * @throws \ReflectionException
* @throws Exception */
*/ public function getLogger(): Kiri\Error\StdoutLogger
public function __get(string $name) {
{ return Kiri::getLogger();
$method = 'get' . ucfirst($name); }
if (method_exists($this, $method)) {
return $this->{$method}();
} else if (method_exists($this, $name)) {
return $this->{$name};
} else {
throw new Exception('Unable getting property ' . get_called_class() . '::' . $name);
}
}
/** /**
* @param string $name * @param string $name
* @param $value * @return mixed
* @return void * @throws Exception
* @throws Exception */
*/ public function __get(string $name)
public function __set(string $name, $value): void {
{ $method = 'get' . ucfirst($name);
$method = 'set' . ucfirst($name); if (method_exists($this, $method)) {
if (method_exists($this, $method)) { return $this->{$method}();
$this->{$method}($value); } else if (method_exists($this, $name)) {
} else if (method_exists($this, $name)) { return $this->{$name};
$this->{$name} = $value; } else {
} else { throw new Exception('Unable getting property ' . get_called_class() . '::' . $name);
throw new Exception('Unable setting property ' . get_called_class() . '::' . $name); }
} }
}
/**
* @param string $name
* @param $value
* @return void
* @throws Exception
*/
public function __set(string $name, $value): void
{
$method = 'set' . ucfirst($name);
if (method_exists($this, $method)) {
$this->{$method}($value);
} else if (method_exists($this, $name)) {
$this->{$name} = $value;
} else {
throw new Exception('Unable setting property ' . get_called_class() . '::' . $name);
}
}
} }
+8 -4
View File
@@ -10,6 +10,7 @@ declare(strict_types=1);
namespace Kiri\Error; namespace Kiri\Error;
use Closure; use Closure;
use ErrorException;
use Exception; use Exception;
use Kiri; use Kiri;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
@@ -18,6 +19,7 @@ use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
use ReflectionException; use ReflectionException;
use Kiri\Events\OnSystemError; use Kiri\Events\OnSystemError;
use Throwable;
/** /**
* Class ErrorHandler * Class ErrorHandler
@@ -110,13 +112,13 @@ class ErrorHandler extends Component implements ErrorInterface
/** /**
* @param \Throwable $exception * @param Throwable $exception
* *
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
* @throws Exception * @throws Exception
*/ */
public function exceptionHandler(\Throwable $exception): void public function exceptionHandler(Throwable $exception): void
{ {
$this->category = 'exception'; $this->category = 'exception';
@@ -127,7 +129,7 @@ class ErrorHandler extends Component implements ErrorInterface
/** /**
* @throws \ErrorException * @throws ErrorException
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
* @throws ReflectionException * @throws ReflectionException
@@ -136,8 +138,10 @@ class ErrorHandler extends Component implements ErrorInterface
{ {
$error = func_get_args(); $error = func_get_args();
var_dump($error);
event(new OnSystemError()); event(new OnSystemError());
throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]); throw new ErrorException($error[1], $error[0], 1, $error[2], $error[3]);
} }
} }
+9 -12
View File
@@ -1,17 +1,14 @@
<?php <?php
use function Swoole\Coroutine\run;
use Swoole\Runtime; run(function () {
use Swoole\Coroutine\Http\Server;
Runtime::enableCoroutine(true); $channel = new \Swoole\Coroutine\Channel(100);
for ($i = 0; $i < 90; $i++) {
\Co\run(function () { $channel->push(100);
Swoole\Coroutine::create(function () { }
var_dump(1); $channel->close();
}); $channel = null;
$server = new Server('0.0.0.0',9501); var_dump($channel);
$server->handle('/', function () {
});
$server->start();
}); });
+9 -48
View File
@@ -1,52 +1,13 @@
<?php <?php
$i = 0;
namespace Ar; while ($i <= 100) {
for ($ii = 0; $ii < 10; $ii++) {
echo "\r";
$i++;
//require_once "a.php"; echo '[' . $i . '%]' . "\r";
// }
// sleep(1);
//$time = microtime(); }
//
//echo \count([]) . PHP_EOL;
//
//echo $time . PHP_EOL;
//echo microtime() . PHP_EOL;
//
//
//
//$time = microtime();
//
//echo count([]) . PHP_EOL;
//
//echo $time . PHP_EOL;
//echo microtime() . PHP_EOL;
//
use Swoole\Coroutine\Http\Client;
use function Swoole\Coroutine\run;
//run(function () {
//
// $client = new Client('47.92.194.207',8500);
// $client->get('/v1/agent/services?filter=Service == FriendRpcService');
// $client->close();
// var_dump($client->getBody());
//
//});
$spl = new \SplPriorityQueue();
$spl->insert(1,0);
$spl->insert(2,0);
$spl->insert(3,0);
$spl->insert(4,0);
$spl->insert(5,0);
$spl->insert(6,0);
$spl->insert(7,0);
$spl->compare();
$spl->extract();
var_dump($spl);