Skip to content

Commit

Permalink
fixed: 修复协程池fetch对象不同协程创建
Browse files Browse the repository at this point in the history
  • Loading branch information
bingcool committed Aug 7, 2023
1 parent 2284e96 commit 7be00a7
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Test/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function testTransactionAddOrder()
]);

$db->commit();

var_dump('commit');
}catch (\Throwable $e) {
$db->rollback();
var_dump($e->getMessage());
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
13 changes: 5 additions & 8 deletions src/Core/Coroutine/CoroutineManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,12 @@ public function getCoroutineStatus(): array
*/
public function listCoroutines(): array
{
if (method_exists('Swoole\\Coroutine', 'list')) {
$cids = [];
$coros = \Swoole\Coroutine::list();
foreach ($coros as $cid) {
array_push($cids, $cid);
}
return $cids;
$cids = [];
$coros = \Swoole\Coroutine::list();
foreach ($coros as $cid) {
array_push($cids, $cid);
}
return [];
return $cids;
}

/**
Expand Down
10 changes: 5 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($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,11 +267,13 @@ protected function make(int $num = 1)

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

/**
* @param object $object
* @param string $poolName
* @return ContainerObjectDto
*/
private function buildContainerObject(object $object, string $poolName)
Expand All @@ -291,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;
}
}
6 changes: 3 additions & 3 deletions src/Core/Coroutine/Timer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public static function tick(int $timeMs, callable $callable)
/**
* cancel tick timer
*
* @param Channel $channel
* @param Channel $timeChannel
* @return bool
*/
public static function cancel(Channel $channel): bool
public static function cancel(Channel $timeChannel): bool
{
return $channel->push(1);
return $timeChannel->push(1);
}

/**
Expand Down
21 changes: 21 additions & 0 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 @@ -87,6 +88,26 @@ public function getObject()
*/
public function __call($name, $arguments)
{
$cid = \Swoole\Coroutine::getCid();
$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 7be00a7

Please sign in to comment.