This commit is contained in:
2021-10-28 18:46:14 +08:00
parent 87f2825cf3
commit df2d887d97
3 changed files with 17 additions and 12 deletions
+6 -2
View File
@@ -6,8 +6,9 @@ namespace Kiri\Pool;
use Closure;
use Database\Mysql\PDO;
use Exception;
use Kiri\Context;
use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config;
use Kiri\Context;
use Kiri\Kiri;
use Swoole\Error;
use Throwable;
@@ -92,8 +93,11 @@ class Connection extends Component
if (($pdo = Context::getContext($coroutineName)) instanceof PDO) {
return $pdo;
}
$minx = Config::get('databases.pool.min', 1);
/** @var PDO $connections */
$connections = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config));
$connections = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config), $minx);
if (Context::hasContext('begin_' . $coroutineName)) {
$connections->beginTransaction();
}
+6 -5
View File
@@ -115,14 +115,16 @@ class Pool extends Component
/**
* @param $name
* @param $callback
* @param $minx
* @return array
* @throws ConfigException
* @throws Exception
*/
public function get($name, $callback): mixed
public function get($name, $callback, $minx): mixed
{
$channel = $this->getChannel($name);
if (!$channel->isEmpty()) {
return $this->maxIdleQuantity($channel);
return $this->maxIdleQuantity($channel, $minx);
}
return $callback();
}
@@ -130,14 +132,13 @@ class Pool extends Component
/**
* @param $channel
* @param $minx
* @return mixed
* @throws ConfigException
* @throws Exception
*/
private function maxIdleQuantity($channel): mixed
protected function maxIdleQuantity($channel, $minx): mixed
{
$connection = $channel->pop();
$minx = Config::get('databases.pool.min', 1);
if ($channel->length() > $minx) {
$this->pop($channel, $minx);
}
+5 -5
View File
@@ -5,15 +5,12 @@ declare(strict_types=1);
namespace Kiri\Pool;
use Annotation\Inject;
use Closure;
use Exception;
use Kiri\Context;
use Kiri\Abstracts\Component;
use Kiri\Events\EventProvider;
use Kiri\Context;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
use Server\Events\OnWorkerExit;
/**
* Class RedisClient
@@ -37,7 +34,10 @@ class Redis extends Component
if (Context::hasContext($coroutineName)) {
return Context::getContext($coroutineName);
}
$clients = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config));
$pool = $config['pool'] ?? ['min' => 1, 'max' => 100];
$clients = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config), $pool['min'] ?? 1);
return Context::setContext($coroutineName, $clients);
}