This commit is contained in:
2021-02-23 19:27:28 +08:00
parent 074762cdae
commit 38eb573945
5 changed files with 28 additions and 36 deletions
+28 -1
View File
@@ -18,13 +18,20 @@ use Snowflake\Snowflake;
class ObjectPool extends \Snowflake\Abstracts\Pool
{
private array $_waitRecover = [];
/**
* set pool max length
* @throws ComponentException
* @throws Exception
*/
public function init()
{
$this->max = 100;
$event = Snowflake::app()->getEvent();
$event->on(Event::EVENT_AFTER_REQUEST, [$this, 'destruct']);
}
@@ -62,7 +69,27 @@ class ObjectPool extends \Snowflake\Abstracts\Pool
*/
public function release(string $name, mixed $object)
{
$this->push($name, clone $object);
$this->_waitRecover[$name][] = $object;
}
public function destruct()
{
if (empty($this->_waitRecover)) {
return;
}
foreach ($this->_waitRecover as $name => $value) {
if (empty($value)) {
continue;
}
foreach ($value as $object) {
if (method_exists($object, 'clean')) {
$object->clean();
}
$this->push($name, $object);
}
}
$this->_waitRecover = [];
}
}