Skip to content

Commit

Permalink
fixed:修复协程池对象创建报错
Browse files Browse the repository at this point in the history
  • Loading branch information
bingcool committed Aug 7, 2023
1 parent eeb735f commit db8a107
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 59 deletions.
196 changes: 149 additions & 47 deletions Test/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

class IndexController extends BController {

protected $db;

public function index()
{
$log = LogManager::getInstance()->getLogger('log');
Expand Down Expand Up @@ -144,66 +146,166 @@ 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,
':address' => "深圳市宝安区xxxx",
':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()
Expand Down
2 changes: 1 addition & 1 deletion Test/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion Test/Scripts/FixedUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
11 changes: 9 additions & 2 deletions src/Core/ComponentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down
9 changes: 4 additions & 5 deletions src/Core/Coroutine/PoolsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -267,6 +267,7 @@ protected function make(int $num = 1)

$containerObject = $this->buildContainerObject($obj, $this->poolName);
$this->channel->push($containerObject, $this->pushTimeout);
unset($obj);
}
}

Expand All @@ -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;
}
}
21 changes: 19 additions & 2 deletions src/Core/Dto/ContainerObjectDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Swoolefy\Core\Dto;

use Swoolefy\Core\Swfy;
use Swoolefy\Core\Application;

class ContainerObjectDto extends AbstractDto
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit db8a107

Please sign in to comment.