Skip to content

Commit

Permalink
refactor and add id/isRev/receive
Browse files Browse the repository at this point in the history
  • Loading branch information
stelin committed Mar 13, 2018
1 parent 452ba3c commit c3194ff
Show file tree
Hide file tree
Showing 11 changed files with 390 additions and 34 deletions.
27 changes: 10 additions & 17 deletions src/Core/AbstractCoResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Swoft\Core;

use Swoft\Pool\ConnectionInterface;
use Swoft\Pool\PoolInterface;

/**
* The result of cor
Expand All @@ -15,11 +14,6 @@ abstract class AbstractCoResult implements ResultInterface
*/
protected $connection;

/**
* @var PoolInterface
*/
protected $pool;

/**
* @var string
*/
Expand All @@ -28,13 +22,11 @@ abstract class AbstractCoResult implements ResultInterface
/**
* AbstractCorResult constructor.
*
* @param mixed $connection
* @param string $profileKey
* @param PoolInterface $pool
* @param mixed $connection
* @param string $profileKey
*/
public function __construct(ConnectionInterface $connection = null, string $profileKey = '', PoolInterface $pool = null)
public function __construct($connection = null, string $profileKey = '')
{
$this->pool = $pool;
$this->connection = $connection;
$this->profileKey = $profileKey;
}
Expand All @@ -48,15 +40,16 @@ public function __construct(ConnectionInterface $connection = null, string $prof
*/
public function recv($defer = false)
{
$result = $this->connection->recv();
if ($this->connection instanceof ConnectionInterface) {
$result = $this->connection->receive();
$this->connection->release();

// 重置延迟设置
if ($defer) {
$this->connection->setDefer(false);
return $result;
}

if ($this->pool !== null) {
$this->pool->release($this->connection);
$result = $this->connection->recv();
if ($defer) {
$this->connection->setDefer(false);
}

return $result;
Expand Down
29 changes: 17 additions & 12 deletions src/Core/AbstractDataResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,17 @@
namespace Swoft\Core;

use Swoft\Pool\ConnectionInterface;
use Swoft\Pool\PoolInterface;

/**
* Sync result
*/
abstract class AbstractDataResult implements ResultInterface
{
/**
* @var ConnectionInterface
* @var mixed
*/
protected $connection;

/**
* @var PoolInterface
*/
protected $pool;

/**
* @var mixed
*/
Expand All @@ -28,14 +22,25 @@ abstract class AbstractDataResult implements ResultInterface
/**
* AbstractDataResult constructor.
*
* @param mixed $data
* @param ConnectionInterface $connection
* @param PoolInterface $pool
* @param mixed $data
* @param mixed $connection
*/
public function __construct($data, ConnectionInterface $connection = null, PoolInterface $pool = null)
public function __construct($data, $connection = null)
{
$this->data = $data;
$this->pool = $pool;
$this->connection = $connection;
}

/**
* @return void
*/
protected function release()
{
if ($this->connection instanceof ConnectionInterface) {
$this->connection->release();
}

return;
}

}
43 changes: 43 additions & 0 deletions src/Core/RequestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,49 @@ public static function getContextDataByKey(string $key, $default = null)
return $default;
}

/**
* Update context data by child key
*
* @param string $key
* @param string $child
* @param mixed $val
*/
public static function setContextDataByChildKey(string $key, string $child, $val)
{
$coroutineId = self::getCoroutineId();
self::$context[$coroutineId][self::DATA_KEY][$key][$child] = $val;
}

/**
* Get context data by child key
*
* @param string $key
* @param string $child
* @param mixed $default
* @return mixed
*/
public static function getContextDataByChildKey(string $key, string $child, $default = null)
{
$coroutineId = self::getCoroutineId();
if (isset(self::$context[$coroutineId][self::DATA_KEY][$key][$child])) {
return self::$context[$coroutineId][self::DATA_KEY][$key][$child];
}

return $default;
}

/**
* Get context data by child key
*
* @param string $key
* @param string $child
*/
public static function removeContextDataByChildKey(string $key, string $child)
{
$coroutineId = self::getCoroutineId();
unset(self::$context[$coroutineId][self::DATA_KEY][$key][$child]);
}

/**
* Get Current Request Log ID
*
Expand Down
5 changes: 5 additions & 0 deletions src/Event/AppEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class AppEvent
*/
const RESOURCE_RELEASE = 'resourceRelease';

/**
* Resource transaction event behind application
*/
const TRANSACTION_RELEASE = 'transactionRelease';

/**
* Worker start event
*/
Expand Down
44 changes: 44 additions & 0 deletions src/Event/Events/TransactionReleaseEvent.php
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;
}
}
55 changes: 55 additions & 0 deletions src/Event/Listeners/ResourceReleaseListener.php
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);
}
}
}
39 changes: 39 additions & 0 deletions src/Helper/PoolHelper.php
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);
}
}
Loading

0 comments on commit c3194ff

Please sign in to comment.