Skip to content

Commit

Permalink
code quality improvements on connection pool
Browse files Browse the repository at this point in the history
  • Loading branch information
deminy committed Dec 9, 2023
1 parent f3d97d5 commit e4d4b8a
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Fixed:
* Fix return value of method _\Swoole\Server\Admin::start()_. ([commit](https://github.com/swoole/library/commit/f211ae16cb3075b5977c52d7fd8f4896a8c51dc7))
* Fix method _\Swoole\MultibyteStringObject::ipos()_. ([commit](https://github.com/swoole/library/commit/3a543c1dc5f116f3fbd96c69b83413193f050086))
* Fix incorrect operator precedence used in method _\Swoole\Coroutine\Admin::start()_. ([commit](https://github.com/swoole/library/commit/49ed9a7b7ad1678a602310c50149f0e46ec0927a))
* Fix issue swoole/library#164 : set_charset() should be called if DB connection succeeds.

Changed:

Expand Down
1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ parameters:
- '#Access to an undefined property Swoole\\Coroutine\\Http\\ClientProxy::\$errCode.#'
- '#Property Swoole\\Server::\$admin_server \(Swoole\\Coroutine\\Http\\Server\) does not accept null.#'
- '#Return type \(Swoole\\StringObject\) of method Swoole\\ArrayObject::serialize\(\) should be compatible with return type \(string\|null\) of method Serializable::serialize\(\)#'
- '#Property Swoole\\ConnectionPool::\$pool \(Swoole\\Coroutine\\Channel\) does not accept null.#'
- '#Parameter \#1 \$response of method Swoole\\Coroutine\\FastCGI\\Proxy::translateResponse\(\) expects Swoole\\FastCGI\\HttpResponse, Swoole\\FastCGI\\Response given.#'
- '#Parameter \#1 \$object of function spl_object_hash expects object, array given.#'
15 changes: 4 additions & 11 deletions src/core/ConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,19 @@ class ConnectionPool
{
public const DEFAULT_SIZE = 64;

/** @var Channel */
protected $pool;
protected ?Channel $pool;

/** @var callable */
protected $constructor;

/** @var int */
protected $size;
protected int $size;

/** @var int */
protected $num = 0;
protected int $num = 0;

/** @var string|null */
protected $proxy;

public function __construct(callable $constructor, int $size = self::DEFAULT_SIZE, ?string $proxy = null)
public function __construct(callable $constructor, int $size = self::DEFAULT_SIZE, protected ?string $proxy = null)
{
$this->pool = new Channel($this->size = $size);
$this->constructor = $constructor;
$this->proxy = $proxy;
}

public function fill(): void
Expand Down
6 changes: 1 addition & 5 deletions src/core/Database/MysqliPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@
*/
class MysqliPool extends ConnectionPool
{
/** @var MysqliConfig */
protected $config;

public function __construct(MysqliConfig $config, int $size = self::DEFAULT_SIZE)
public function __construct(protected MysqliConfig $config, int $size = self::DEFAULT_SIZE)
{
$this->config = $config;
parent::__construct(function () {
$mysqli = new \mysqli();
foreach ($this->config->getOptions() as $option => $value) {
Expand Down
9 changes: 1 addition & 8 deletions src/core/Database/PDOPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,8 @@
*/
class PDOPool extends ConnectionPool
{
/** @var int */
protected $size = 64;

/** @var PDOConfig */
protected $config;

public function __construct(PDOConfig $config, int $size = self::DEFAULT_SIZE)
public function __construct(protected PDOConfig $config, int $size = self::DEFAULT_SIZE)
{
$this->config = $config;
parent::__construct(function () {
$driver = $this->config->getDriver();
return new \PDO(
Expand Down

0 comments on commit e4d4b8a

Please sign in to comment.