-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
390 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Swoft\Event\Events; | ||
|
||
use Swoft\Event\Event; | ||
|
||
/** | ||
* TransactionEvent | ||
*/ | ||
class TransactionReleaseEvent extends Event | ||
{ | ||
/** | ||
* @var \SplStack[] | ||
*/ | ||
private $tsStacks; | ||
|
||
/** | ||
* @var [] | ||
*/ | ||
private $connections; | ||
|
||
public function __construct($name = null, array $tsStacks, array &$connections) | ||
{ | ||
parent::__construct($name); | ||
$this->tsStacks = $tsStacks; | ||
$this->connections = $connections; | ||
} | ||
|
||
/** | ||
* @return \SplStack[] | ||
*/ | ||
public function getTsStacks(): array | ||
{ | ||
return $this->tsStacks; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getConnections() | ||
{ | ||
return $this->connections; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Swoft\Event\Listeners; | ||
|
||
use Swoft\App; | ||
use Swoft\Bean\Annotation\Listener; | ||
use Swoft\Core\RequestContext; | ||
use Swoft\Event\AppEvent; | ||
use Swoft\Event\EventHandlerInterface; | ||
use Swoft\Event\EventInterface; | ||
use Swoft\Event\Events\TransactionReleaseEvent; | ||
use Swoft\Helper\PoolHelper; | ||
use Swoft\Log\Log; | ||
|
||
/** | ||
* Resource release listener | ||
* | ||
* @Listener(AppEvent::RESOURCE_RELEASE) | ||
*/ | ||
class ResourceReleaseListener implements EventHandlerInterface | ||
{ | ||
/** | ||
* @param \Swoft\Event\EventInterface $event | ||
* @throws \Swoft\Redis\Exception\RedisException | ||
*/ | ||
public function handle(EventInterface $event) | ||
{ | ||
$contextTsKey = PoolHelper::getContextTsKey(); | ||
$connectionKey = PoolHelper::getContextCntKey(); | ||
$tsStacks = RequestContext::getContextDataByKey($contextTsKey, []); | ||
$connections = RequestContext::getContextDataByKey($connectionKey, []); | ||
|
||
if (empty($connections)) { | ||
return; | ||
} | ||
|
||
/* @var \Swoft\Pool\ConnectionInterface $connection */ | ||
foreach ($connections as $connection){ | ||
if (App::isCoContext() && !$connection->isRecv()) { | ||
$connection->receive(); | ||
} | ||
} | ||
|
||
if (!empty($tsStacks)) { | ||
$event = new TransactionReleaseEvent(AppEvent::TRANSACTION_RELEASE, $tsStacks, $connections); | ||
App::trigger($event); | ||
} | ||
|
||
/* @var \Swoft\Pool\ConnectionInterface $connection */ | ||
foreach ($connections as $connectionId => $connection) { | ||
Log::error(sprintf('%s connection is not released ,forget to getResult() or em->close', get_class($connection))); | ||
$connection->release(true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Swoft\Helper; | ||
|
||
use Swoft\Core\Coroutine; | ||
|
||
/** | ||
* PoolHelper | ||
*/ | ||
class PoolHelper | ||
{ | ||
/** | ||
* @return string | ||
*/ | ||
public static function getContextCntKey(): string | ||
{ | ||
return sprintf('connectioins'); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public static function getContextTsKey(): string | ||
{ | ||
return sprintf('transactions'); | ||
} | ||
|
||
/** | ||
* @param string $poolId | ||
* | ||
* @return string | ||
*/ | ||
public static function getCidPoolId(string $poolId) | ||
{ | ||
$cid = Coroutine::id(); | ||
|
||
return sprintf('%d-%s', $cid, $poolId); | ||
} | ||
} |
Oops, something went wrong.