This commit is contained in:
2021-04-23 19:07:39 +08:00
parent 61ac65c0d3
commit 3d814895ed
+21 -11
View File
@@ -110,7 +110,7 @@ abstract class Pool extends Component
/** /**
* @param $name * @param $name
*/ */
protected function clearCreateLog($name) protected function clearCreateLog($name): void
{ {
if (!isset($this->hasCreate[$name])) { if (!isset($this->hasCreate[$name])) {
return; return;
@@ -125,14 +125,18 @@ abstract class Pool extends Component
* @param $retain_number * @param $retain_number
* @throws Exception * @throws Exception
*/ */
protected function pop($channel, $name, $retain_number) protected function pop($channel, $name, $retain_number): void
{ {
while ($channel->length() > $retain_number) { if ($channel->errCode == 0) {
$connection = $channel->pop(); while ($channel->length() > $retain_number) {
if ($connection) { $connection = $channel->pop();
unset($connection); if ($connection) {
unset($connection);
}
$this->decrement($name);
} }
$this->decrement($name); } else {
$this->clearCreateLog($name);
} }
} }
@@ -187,9 +191,9 @@ abstract class Pool extends Component
*/ */
private function createByCallback($name, mixed $callback) private function createByCallback($name, mixed $callback)
{ {
// if (!$this->canCreate($name)) { if (!$this->canCreate($name)) {
// return; return;
// } }
if ($this->creates === -1 && !is_callable($callback)) { if ($this->creates === -1 && !is_callable($callback)) {
$this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']); $this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']);
} }
@@ -260,6 +264,9 @@ abstract class Pool extends Component
public function hasItem(string $name): bool public function hasItem(string $name): bool
{ {
if (isset($this->_items[$name])) { if (isset($this->_items[$name])) {
if ($this->_items[$name]->errCode == -2) {
$this->_items[$name] = new Channel($this->max);
}
return !$this->_items[$name]->isEmpty(); return !$this->_items[$name]->isEmpty();
} }
return false; return false;
@@ -275,7 +282,7 @@ abstract class Pool extends Component
if (!Context::inCoroutine()) { if (!Context::inCoroutine()) {
return 0; return 0;
} }
if (!isset($this->_items[$name])) { if (!isset($this->_items[$name]) || $this->_items[$name]->errCode == -2) {
return 0; return 0;
} }
return $this->_items[$name]->length(); return $this->_items[$name]->length();
@@ -294,6 +301,9 @@ abstract class Pool extends Component
if (!isset($this->_items[$name])) { if (!isset($this->_items[$name])) {
$this->_items[$name] = new Channel($this->max); $this->_items[$name] = new Channel($this->max);
} }
if ($this->_items[$name]->errCode == -2) {
$this->_items[$name] = new Channel($this->max);
}
if (!$this->_items[$name]->isFull()) { if (!$this->_items[$name]->isFull()) {
$this->_items[$name]->push($client); $this->_items[$name]->push($client);
} }