From db8a1073141d65838c5ebc963dc0263bd27e672c Mon Sep 17 00:00:00 2001 From: bingcool <2437667702@qq.com> Date: Mon, 7 Aug 2023 21:23:29 +0800 Subject: [PATCH] =?UTF-8?q?fixed=EF=BC=9A=E4=BF=AE=E5=A4=8D=E5=8D=8F?= =?UTF-8?q?=E7=A8=8B=E6=B1=A0=E5=AF=B9=E8=B1=A1=E5=88=9B=E5=BB=BA=E6=8A=A5?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Test/Controller/IndexController.php | 196 +++++++++++++++++++++------- Test/Event.php | 2 +- Test/Scripts/FixedUser.php | 2 +- src/Core/Application.php | 2 +- src/Core/ComponentTrait.php | 11 +- src/Core/Coroutine/PoolsHandler.php | 9 +- src/Core/Dto/ContainerObjectDto.php | 21 ++- 7 files changed, 184 insertions(+), 59 deletions(-) diff --git a/Test/Controller/IndexController.php b/Test/Controller/IndexController.php index 44417dfa..ee93c3c4 100644 --- a/Test/Controller/IndexController.php +++ b/Test/Controller/IndexController.php @@ -9,6 +9,8 @@ class IndexController extends BController { + protected $db; + public function index() { $log = LogManager::getInstance()->getLogger('log'); @@ -144,18 +146,29 @@ public function testOrderList(int $uid, int $page = 1, int $limit = 20) public function testTransactionAddOrder() { /** - * @var \Common\Library\Db\Mysql $db - */ - $db = Application::getApp()->get('db'); - - $db->beginTransaction(); + * @var \Common\Library\Db\Mysql $db1 + */ + $this->db = Application::getApp()->get('db'); + + $this->db->newQuery()->query( + "insert into tbl_order (`order_id`,`receiver_user_name`,`receiver_user_phone`,`user_id`,`order_amount`,`address`,`order_product_ids`,`order_status`) values(:order_id,:receiver_user_name,:receiver_user_phone,:user_id,:order_amount,:address,:order_product_ids,:order_status)", + [ + ':order_id' => time() + 6, + ':receiver_user_name' => '张三-444555', + ':receiver_user_phone' => '12345666', + ':user_id' => 10000, + ':order_amount' => 105, + ':address' => "深圳市宝安区xxxx", + ':order_product_ids' => json_encode([1,2,3,rand(1,1000)]), + ':order_status' => 1 + ]); - try { - $db = Application::getApp()->get('db'); - $db->createCommand("insert into tbl_order (`order_id`,`receiver_user_name`,`receiver_user_phone`,`user_id`,`order_amount`,`address`,`order_product_ids`,`order_status`) values(:order_id,:receiver_user_name,:receiver_user_phone,:user_id,:order_amount,:address,:order_product_ids,:order_status)" ) - ->insert([ - ':order_id' => time() + 5, - ':receiver_user_name' => '张三', + goApp(function () { + $this->db->newQuery()->query( + "insert into tbl_order (`order_id`,`receiver_user_name`,`receiver_user_phone`,`user_id`,`order_amount`,`address`,`order_product_ids`,`order_status`) values(:order_id,:receiver_user_name,:receiver_user_phone,:user_id,:order_amount,:address,:order_product_ids,:order_status)", + [ + ':order_id' => time() + 7, + ':receiver_user_name' => '张三-992', ':receiver_user_phone' => '12345666', ':user_id' => 10000, ':order_amount' => 105, @@ -163,47 +176,136 @@ public function testTransactionAddOrder() ':order_product_ids' => json_encode([1,2,3,rand(1,1000)]), ':order_status' => 1 ]); + }); - $rowCount = $db->getNumRows(); - - $db->commit(); - - - goApp(function() { - /** - * @var \Common\Library\Db\Mysql $db - */ - $db = Application::getApp()->get('db'); - $db->beginTransaction(); - try { - $db->createCommand("insert into tbl_order (`order_id`,`receiver_user_name`,`receiver_user_phone`,`user_id`,`order_amount`,`order_product_ids`,`order_status`) values(:order_id,:receiver_user_name,:receiver_user_phone,:user_id,:order_amount,:order_product_ids,:order_status)" ) - ->insert([ - ':order_id' => time() + 6, - ':receiver_user_name' => '张三', - ':receiver_user_phone' => '12345666', - ':user_id' => 10000, - ':order_amount' => 105, - ':order_product_ids' => json_encode([1,2,3,rand(1,1000)]), - ':order_status' => 1 - ]); - - $db->commit(); - - }catch (\Throwable $e) { - $db->rollback(); - var_dump($e->getMessage()); - } - }); - }catch (\Throwable $e) { - $db->rollback(); - var_dump($e->getMessage()); - return; - } + goApp(function() { + /** + * @var \Common\Library\Db\Mysql $db1 + */ + $db1 = Application::getApp()->get('db'); + var_dump('beginTransaction'); + + try { + $db1->beginTransaction(); + +// $db1->createCommand("insert into tbl_order (`order_id`,`receiver_user_name`,`receiver_user_phone`,`user_id`,`order_amount`,`address`,`order_product_ids`,`order_status`) values(:order_id,:receiver_user_name,:receiver_user_phone,:user_id,:order_amount,:address,:order_product_ids,:order_status)" ) +// ->insert([ +// ':order_id' => time() + 6, +// ':receiver_user_name' => '张三-2', +// ':receiver_user_phone' => '12345666', +// ':user_id' => 10000, +// ':order_amount' => 105, +// ':address' => "深圳市宝安区xxxx", +// ':order_product_ids' => json_encode([1,2,3,rand(1,1000)]), +// ':order_status' => 1 +// ]); +// var_dump('hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh'); + + $db1->newQuery()->query( + "insert into tbl_order (`order_id`,`receiver_user_name`,`receiver_user_phone`,`user_id`,`order_amount`,`address`,`order_product_ids`,`order_status`) values(:order_id,:receiver_user_name,:receiver_user_phone,:user_id,:order_amount,:address,:order_product_ids,:order_status)", + [ + ':order_id' => time() + 9, + ':receiver_user_name' => '张三-2', + ':receiver_user_phone' => '12345666', + ':user_id' => 10000, + ':order_amount' => 105, + ':address' => "深圳市宝安区xxxx", + ':order_product_ids' => json_encode([1,2,3,rand(1,1000)]), + ':order_status' => 1 + ]); + + + $db1->commit(); + + }catch (\Throwable $e) { + var_dump($e->getMessage()); + $db1->rollback(); + } + }); $this->returnJson([ - 'num' => $rowCount + 'num' => rand(1,1000) ]); + +// +// try { +// /** +// * @var \Common\Library\Db\Mysql $db +// */ +// $db = Application::getApp()->get('db'); +// $db->beginTransaction(); +// $db->createCommand("insert into tbl_order (`order_id`,`receiver_user_name`,`receiver_user_phone`,`user_id`,`order_amount`,`address`,`order_product_ids`,`order_status`) values(:order_id,:receiver_user_name,:receiver_user_phone,:user_id,:order_amount,:address,:order_product_ids,:order_status)" ) +// ->insert([ +// ':order_id' => time() + 5, +// ':receiver_user_name' => '张三-1', +// ':receiver_user_phone' => '12345666', +// ':user_id' => 10000, +// ':order_amount' => 105, +// ':address' => "深圳市宝安区xxxx", +// ':order_product_ids' => json_encode([1,2,3,rand(1,1000)]), +// ':order_status' => 1 +// ]); +// +// $rowCount = $db->getNumRows(); +// +// goApp(function() { +// /** +// * @var \Common\Library\Db\Mysql $db1 +// */ +// $db1 = Application::getApp()->get('db'); +// var_dump('beginTransaction'); +// +// try { +// $db1->beginTransaction(); +// +//// $db1->createCommand("insert into tbl_order (`order_id`,`receiver_user_name`,`receiver_user_phone`,`user_id`,`order_amount`,`address`,`order_product_ids`,`order_status`) values(:order_id,:receiver_user_name,:receiver_user_phone,:user_id,:order_amount,:address,:order_product_ids,:order_status)" ) +//// ->insert([ +//// ':order_id' => time() + 6, +//// ':receiver_user_name' => '张三-2', +//// ':receiver_user_phone' => '12345666', +//// ':user_id' => 10000, +//// ':order_amount' => 105, +//// ':address' => "深圳市宝安区xxxx", +//// ':order_product_ids' => json_encode([1,2,3,rand(1,1000)]), +//// ':order_status' => 1 +//// ]); +//// var_dump('hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh'); +// +// $db1->newQuery()->query( +// "insert into tbl_order (`order_id`,`receiver_user_name`,`receiver_user_phone`,`user_id`,`order_amount`,`address`,`order_product_ids`,`order_status`) values(:order_id,:receiver_user_name,:receiver_user_phone,:user_id,:order_amount,:address,:order_product_ids,:order_status)", +// [ +// ':order_id' => time() + 6, +// ':receiver_user_name' => '张三-2', +// ':receiver_user_phone' => '12345666', +// ':user_id' => 10000, +// ':order_amount' => 105, +// ':address' => "深圳市宝安区xxxx", +// ':order_product_ids' => json_encode([1,2,3,rand(1,1000)]), +// ':order_status' => 1 +// ]); +// +// +// $db1->commit(); +// +// }catch (\Throwable $e) { +// var_dump($e->getMessage()); +// $db1->rollback(); +// } +// }); +// +// +// +// $db->commit(); +// }catch (\Throwable $e) { +// $db->rollback(); +// var_dump($e->getMessage()); +// } +// +// $this->returnJson([ +// 'num' => $rowCount +// ]); + } public function ws() diff --git a/Test/Event.php b/Test/Event.php index 7057b21f..a7568260 100644 --- a/Test/Event.php +++ b/Test/Event.php @@ -87,7 +87,7 @@ public function onInit() { // redis的订阅进程 - ProcessManager::getInstance()->addProcess('redis_subscribe_test', \Test\Process\SubscribeProcess\Subscribe::class); + //ProcessManager::getInstance()->addProcess('redis_subscribe_test', \Test\Process\SubscribeProcess\Subscribe::class); // 这里为什么获取不到pid,那是应为process需要server执行start后才会创建,而在这里只是创建实例,server还没正式启动 //$pid = ProcessManager::getInstance()->getProcessByName('redis_list_test')->getPid(); diff --git a/Test/Scripts/FixedUser.php b/Test/Scripts/FixedUser.php index a8f4b0d3..afb97001 100644 --- a/Test/Scripts/FixedUser.php +++ b/Test/Scripts/FixedUser.php @@ -35,7 +35,7 @@ public function fixName() try { var_dump('CID11='.\Swoole\Coroutine::getCid()); var_dump('spl_object_id-11='.spl_object_id($this->db)); - $result1 = Application::getApp()->get('db')->newQuery()->table('tbl_users')->limit(1)->select()->toArray(); + $result1 = $this->db->newQuery()->table('tbl_users')->limit(1)->select()->toArray(); var_dump($result1); }catch (\Throwable $exception) { var_dump($exception->getMessage()); diff --git a/src/Core/Application.php b/src/Core/Application.php index 8f388d95..bb5a71fa 100644 --- a/src/Core/Application.php +++ b/src/Core/Application.php @@ -87,7 +87,7 @@ public static function removeApp(?int $coroutineId = null): bool if ($coroutineId) { $cid = $coroutineId; } else { - $cid = CoroutineManager::getInstance()->getCoroutineId(); + $cid = \Swoole\Coroutine::getCid(); } if (isset(self::$apps[$cid])) { unset(self::$apps[$cid]); diff --git a/src/Core/ComponentTrait.php b/src/Core/ComponentTrait.php index 8b6c898e..45714a71 100644 --- a/src/Core/ComponentTrait.php +++ b/src/Core/ComponentTrait.php @@ -203,8 +203,15 @@ private function buildContainerObject(object $object, string $comAliasName) $containerObjectDto->__coroutineId = \Swoole\Coroutine::getCid(); $containerObjectDto->__objInitTime = time(); $containerObjectDto->__object = $object; - $containerObjectDto->__objExpireTime = null; $containerObjectDto->__comAliasName = $comAliasName; + + if (!empty($appConf['enable_component_pools']) && is_array($appConf['enable_component_pools'])) { + $liveTime = $appConf['enable_component_pools'][$comAliasName]['live_time'] ?? 10; + $containerObjectDto->__objExpireTime = time() + $liveTime + rand(1, 10); + }else { + $containerObjectDto->__objExpireTime = null; + } + return $containerObjectDto; } @@ -303,7 +310,7 @@ final public function get(string $name) } if (empty($this->componentPools)) { - if (isset($appConf['enable_component_pools']) && is_array($appConf['enable_component_pools']) && !empty($appConf['enable_component_pools'])) { + if (!empty($appConf['enable_component_pools']) && is_array($appConf['enable_component_pools']) ) { $enableComponentPools = array_keys($appConf['enable_component_pools']); $this->componentPools = $enableComponentPools; } diff --git a/src/Core/Coroutine/PoolsHandler.php b/src/Core/Coroutine/PoolsHandler.php index 071c83f4..d8b3c28c 100644 --- a/src/Core/Coroutine/PoolsHandler.php +++ b/src/Core/Coroutine/PoolsHandler.php @@ -183,7 +183,7 @@ public function pushObj(object $obj) { \Swoole\Coroutine::create(function () use ($obj) { $isPush = true; - if (isset($obj->__objExpireTime) && time() > $obj->__objExpireTime) { + if (!is_null($obj->__objExpireTime) && time() > $obj->__objExpireTime) { $isPush = false; } @@ -267,6 +267,7 @@ protected function make(int $num = 1) $containerObject = $this->buildContainerObject($obj, $this->poolName); $this->channel->push($containerObject, $this->pushTimeout); + unset($obj); } } @@ -292,15 +293,13 @@ private function buildContainerObject(object $object, string $poolName) protected function pop() { $containerObject = $this->channel->pop($this->popTimeout); - - if (is_object($containerObject) && isset($containerObject->__objExpireTime) && time() > $containerObject->__objExpireTime) { + if (is_object($containerObject) && !is_null($containerObject->__objExpireTime) && time() > $containerObject->__objExpireTime) { //rebuild object unset($containerObject); $this->make(1); + $containerObject = $this->channel->pop($this->popTimeout); } - $containerObject = $this->channel->pop($this->popTimeout); - return is_object($containerObject) ? $containerObject : null; } } \ No newline at end of file diff --git a/src/Core/Dto/ContainerObjectDto.php b/src/Core/Dto/ContainerObjectDto.php index cbcb91bc..6ee84caf 100644 --- a/src/Core/Dto/ContainerObjectDto.php +++ b/src/Core/Dto/ContainerObjectDto.php @@ -11,6 +11,7 @@ namespace Swoolefy\Core\Dto; +use Swoolefy\Core\Swfy; use Swoolefy\Core\Application; class ContainerObjectDto extends AbstractDto @@ -88,9 +89,25 @@ public function getObject() public function __call($name, $arguments) { $cid = \Swoole\Coroutine::getCid(); - if ($cid != $this->__coroutineId) { - return Application::getApp()->get($this->__comAliasName)->$name(...$arguments); + $appConf = Swfy::getAppConf(); + if (!empty($appConf['enable_component_pools']) && is_array($appConf['enable_component_pools'])) { + if (Swfy::isWorkerProcess()) { + if (!isset($appConf['enable_component_pools'][$this->__comAliasName])) { + if ($cid != $this->__coroutineId) { + return Application::getApp()->get($this->__comAliasName)->$name(...$arguments); + } + } + }else { + if ($cid != $this->__coroutineId) { + return Application::getApp()->get($this->__comAliasName)->$name(...$arguments); + } + } + }else { + if ($cid != $this->__coroutineId) { + return Application::getApp()->get($this->__comAliasName)->$name(...$arguments); + } } + return $this->__object->$name(...$arguments); }