From 4c6f9fa176d34917100494d354a949f98dbb724d Mon Sep 17 00:00:00 2001 From: hemengze Date: Sun, 5 Jul 2020 15:38:07 +0800 Subject: [PATCH] update: dynamic process number --- composer.json | 3 +- .../src/Annotation/Mapping/Process.php | 24 ++++------- .../src/Annotation/Parser/ProcessParser.php | 2 +- src/process/src/ProcessPool.php | 7 +--- src/process/src/ProcessRegister.php | 32 ++++++++------- src/process/test/testing/AutoLoader.php | 41 +++++++++++++++++++ src/process/test/testing/Process/Process1.php | 30 ++++++++++++++ src/process/test/testing/Process/Process2.php | 29 +++++++++++++ src/process/test/testing/Process/Process3.php | 29 +++++++++++++ src/process/test/unit/ProcessNumTest.php | 41 +++++++++++++++++++ 10 files changed, 200 insertions(+), 38 deletions(-) create mode 100644 src/process/test/testing/AutoLoader.php create mode 100644 src/process/test/testing/Process/Process1.php create mode 100644 src/process/test/testing/Process/Process2.php create mode 100644 src/process/test/testing/Process/Process3.php create mode 100644 src/process/test/unit/ProcessNumTest.php diff --git a/composer.json b/composer.json index b75d39e15..5eaf0e83f 100644 --- a/composer.json +++ b/composer.json @@ -187,6 +187,7 @@ "tcp": "php run.php -c src/tcp/phpunit.xml", "tcp-server": "php run.php -c src/tcp-server/phpunit.xml", "validator": "php run.php -c src/validator/phpunit.xml", - "websocket-server": "php run.php -c src/websocket-server/phpunit.xml" + "websocket-server": "php run.php -c src/websocket-server/phpunit.xml", + "process": "php run.php -c src/process/phpunit.xml" } } diff --git a/src/process/src/Annotation/Mapping/Process.php b/src/process/src/Annotation/Mapping/Process.php index e185e7a18..f809ed376 100644 --- a/src/process/src/Annotation/Mapping/Process.php +++ b/src/process/src/Annotation/Mapping/Process.php @@ -22,22 +22,16 @@ * @Annotation * @Target("CLASS") * @Attributes({ - * @Attribute("workerId", type="array"), + * @Attribute("workerNum", type="int"), * }) */ class Process { - /** - * Default - */ - public const DEFAULT = -1; /** - * @var array + * @var int */ - private $workerId = [ - self::DEFAULT - ]; + private $workerNum = 1; /** * Process constructor. @@ -47,18 +41,18 @@ class Process public function __construct(array $values) { if (isset($values['value'])) { - $this->workerId = (array)$values['value']; + $this->workerNum = (int)$values['value']; } - if (isset($values['workerId'])) { - $this->workerId = (array)$values['workerId']; + if (isset($values['workerNum'])) { + $this->workerNum = (int)$values['workerNum']; } } /** - * @return array + * @return int */ - public function getWorkerId(): array + public function getWorkerNum(): int { - return $this->workerId; + return $this->workerNum; } } diff --git a/src/process/src/Annotation/Parser/ProcessParser.php b/src/process/src/Annotation/Parser/ProcessParser.php index 249b22278..371e85f2e 100644 --- a/src/process/src/Annotation/Parser/ProcessParser.php +++ b/src/process/src/Annotation/Parser/ProcessParser.php @@ -36,7 +36,7 @@ class ProcessParser extends Parser public function parse(int $type, $annotationObject): array { // Register - ProcessRegister::registerProcess($this->className, $annotationObject->getWorkerId()); + ProcessRegister::registerProcess($this->className, $annotationObject->getWorkerNum()); return [$this->className, $this->className, Bean::SINGLETON, '']; } diff --git a/src/process/src/ProcessPool.php b/src/process/src/ProcessPool.php index ad0aa664d..c35d61532 100644 --- a/src/process/src/ProcessPool.php +++ b/src/process/src/ProcessPool.php @@ -40,11 +40,6 @@ class ProcessPool */ private $pool; - /** - * @var int - */ - private $workerNum = 3; - /** * @var int */ @@ -106,7 +101,7 @@ class ProcessPool */ public function start(): void { - $this->pool = new Pool($this->workerNum, $this->ipcType, $this->msgQueueKey, $this->coroutine); + $this->pool = new Pool(ProcessRegister::getWorkerNum(), $this->ipcType, $this->msgQueueKey, $this->coroutine); foreach ($this->on as $name => $listener) { $listenerInterface = SwooleEvent::LISTENER_MAPPING[$name] ?? ''; if (empty($listenerInterface)) { diff --git a/src/process/src/ProcessRegister.php b/src/process/src/ProcessRegister.php index 2676bb895..dcbe4bdce 100644 --- a/src/process/src/ProcessRegister.php +++ b/src/process/src/ProcessRegister.php @@ -31,20 +31,21 @@ class ProcessRegister */ private static $process = []; + /** + * @var int + */ + private static $workerId = 0; + /** * @param string $className - * @param array $workerIds + * @param int $workerNum * * @throws ProcessException */ - public static function registerProcess(string $className, array $workerIds): void + public static function registerProcess(string $className, int $workerNum): void { - foreach ($workerIds as $workerId) { - if (isset(self::$process[$workerId])) { - throw new ProcessException(sprintf('Worker process(%d) for process pool must be only one!', $workerId)); - } - - self::$process[$workerId]['class'] = $className; + for ($i = 0; $i < $workerNum; $i ++) { + self::$process[self::$workerId ++]['class'] = $className; } } @@ -60,13 +61,14 @@ public static function getProcess(int $workerId): string if (!empty($className)) { return $className; } + throw new ProcessException(sprintf('Worker process(%d) for process pool must be defined!', $workerId)); + } - $default = ProcessAnnotation::DEFAULT; - $className = self::$process[$default]['class'] ?? ''; - if (empty($className)) { - throw new ProcessException(sprintf('Worker process(%d) for process pool must be defined!', $workerId)); - } - - return $className; + /** + * @return int + */ + public static function getWorkerNum(): int + { + return count(self::$process); } } diff --git a/src/process/test/testing/AutoLoader.php b/src/process/test/testing/AutoLoader.php new file mode 100644 index 000000000..65dbcd471 --- /dev/null +++ b/src/process/test/testing/AutoLoader.php @@ -0,0 +1,41 @@ + __DIR__, + ]; + } + + /** + * @return array + */ + public function metadata(): array + { + return []; + } +} diff --git a/src/process/test/testing/Process/Process1.php b/src/process/test/testing/Process/Process1.php new file mode 100644 index 000000000..d0a086113 --- /dev/null +++ b/src/process/test/testing/Process/Process1.php @@ -0,0 +1,30 @@ +assertEquals(ProcessRegister::getWorkerNum(), 6); + } + + function testProcessClassMap(): void + { + $workerNum = ProcessRegister::getWorkerNum(); + + $processClassMap = []; + + for ($workerId = 0; $workerId < $workerNum; $workerId ++) { + $class = ProcessRegister::getProcess($workerId); + isset($processClassMap[$class]) ? $processClassMap[$class] ++ : $processClassMap[$class] = 1; + } + + $this->assertArrayHasKey(Process1::class, $processClassMap); + $this->assertArrayHasKey(Process2::class, $processClassMap); + $this->assertArrayHasKey(Process3::class, $processClassMap); + + $this->assertEquals($processClassMap[Process1::class], 3); + $this->assertEquals($processClassMap[Process2::class], 2); + $this->assertEquals($processClassMap[Process3::class], 1); + } +} \ No newline at end of file