Skip to content

Commit

Permalink
add pool init
Browse files Browse the repository at this point in the history
  • Loading branch information
stelin committed Sep 13, 2019
1 parent c2e87f2 commit a367173
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 11 deletions.
31 changes: 29 additions & 2 deletions src/connection-pool/src/AbstractPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Swoft\Connection\Pool\Contract\ConnectionInterface;
use Swoft\Connection\Pool\Contract\PoolInterface;
use Swoft\Connection\Pool\Exception\ConnectionPoolException;
use Swoft\Log\Helper\CLog;
use Swoole\Coroutine\Channel;
use Throwable;

Expand Down Expand Up @@ -59,6 +60,11 @@ abstract class AbstractPool implements PoolInterface
*/
protected $maxCloseTime = 3;

/**
* @var bool
*/
protected $init = false;

/**
* @var Channel
*/
Expand All @@ -83,6 +89,27 @@ abstract class AbstractPool implements PoolInterface
*/
protected $connectionId = 0;

/**
* Initialize pool
*
* @throws ConnectionPoolException
*/
public function initPool(): void
{
if (!$this->init) {
// return;
}

// Enable initialize pool
for ($i = 0; $i < $this->minActive; $i++) {
$connection = $this->getConnection();
$connection->setRelease(true);
$connection->release();
}

CLog::debug('Initialize ' . static::class . ' pool size=' . $this->count);
}

/**
* @return int
*/
Expand Down Expand Up @@ -189,7 +216,7 @@ private function getConnectionByChannel(): ConnectionInterface
);
}

/* @var ConnectionInterface $connection*/
/* @var ConnectionInterface $connection */
// Sleep coroutine and resume coroutine after `maxWaitTime`, Return false is waiting timeout
$connection = $this->channel->pop($this->maxWaitTime);
if ($connection === false) {
Expand Down Expand Up @@ -238,7 +265,7 @@ private function create(): ConnectionInterface
*/
private function popByChannel(): ?ConnectionInterface
{
$time = time();
$time = time();

while (!$this->channel->isEmpty()) {
/* @var ConnectionInterface $connection */
Expand Down
5 changes: 5 additions & 0 deletions src/connection-pool/src/Contract/PoolInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
*/
interface PoolInterface
{
/**
* Initialize pool
*/
public function initPool(): void;

/**
* Create connection
*
Expand Down
6 changes: 2 additions & 4 deletions src/db/src/AutoLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class AutoLoader extends SwoftComponent
{
/**
* @return array
* @throws ContainerException
* @throws ReflectionException
*/
public function beans(): array
{
Expand All @@ -29,13 +27,13 @@ public function beans(): array
'class' => Database::class,
'dsn' => 'mysql:dbname=dbname;host=127.0.0.1',
'config' => [
// fetch array
// fetch array
'fetchMode' => PDO::FETCH_ASSOC,
],
],
'db.pool' => [
'class' => Pool::class,
'database' => bean('db')
'database' => bean('db'),
]
];
}
Expand Down
38 changes: 38 additions & 0 deletions src/db/src/Listener/WorkerStartListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);


namespace Swoft\Db\Listener;


use Swoft\Bean\BeanFactory;
use Swoft\Connection\Pool\Exception\ConnectionPoolException;
use Swoft\Db\Pool;
use Swoft\Event\Annotation\Mapping\Listener;
use Swoft\Event\EventHandlerInterface;
use Swoft\Event\EventInterface;
use Swoft\Server\ServerEvent;

/**
* Class WorkerStartListener
*
* @since 2.0
*
* @Listener(event=ServerEvent::WORK_PROCESS_START)
*/
class WorkerStartListener implements EventHandlerInterface
{
/**
* @param EventInterface $event
*
* @throws ConnectionPoolException
*/
public function handle(EventInterface $event): void
{
$pools = BeanFactory::getBeans(Pool::class);

/* @var Pool $pool */
foreach ($pools as $pool) {
$pool->initPool();
}
}
}
4 changes: 1 addition & 3 deletions src/redis/src/AutoLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public function metadata(): array

/**
* @return array
* @throws ReflectionException
* @throws ContainerException
*/
public function beans(): array
{
Expand All @@ -51,7 +49,7 @@ public function beans(): array
],
'redis.pool' => [
'class' => Pool::class,
'redisDb' => bean('redis')
'redisDb' => bean('redis'),
]
];
}
Expand Down
37 changes: 37 additions & 0 deletions src/redis/src/Listener/WorkerStartListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);


namespace Swoft\Redis\Listener;

use Swoft\Bean\BeanFactory;
use Swoft\Connection\Pool\Exception\ConnectionPoolException;
use Swoft\Event\Annotation\Mapping\Listener;
use Swoft\Event\EventHandlerInterface;
use Swoft\Event\EventInterface;
use Swoft\Redis\Pool;
use Swoft\Server\ServerEvent;

/**
* Class WorkerStartListener
*
* @since 2.0
*
* @Listener(event=ServerEvent::WORK_PROCESS_START)
*/
class WorkerStartListener implements EventHandlerInterface
{
/**
* @param EventInterface $event
*
* @throws ConnectionPoolException
*/
public function handle(EventInterface $event): void
{
$pools = BeanFactory::getBeans(Pool::class);

/* @var Pool $pool */
foreach ($pools as $pool) {
$pool->initPool();
}
}
}
2 changes: 0 additions & 2 deletions src/rpc-client/src/AutoLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public function metadata(): array

/**
* @return array
* @throws ReflectionException
* @throws ContainerException
*/
public function beans(): array
{
Expand Down
37 changes: 37 additions & 0 deletions src/rpc-client/src/Listener/WorkerStartListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);


namespace Swoft\Rpc\Client\Listener;

use Swoft\Bean\BeanFactory;
use Swoft\Connection\Pool\Exception\ConnectionPoolException;
use Swoft\Event\Annotation\Mapping\Listener;
use Swoft\Event\EventHandlerInterface;
use Swoft\Event\EventInterface;
use Swoft\Rpc\Client\Pool;
use Swoft\Server\ServerEvent;

/**
* Class WorkerStartListener
*
* @since 2.0
*
* @Listener(event=ServerEvent::WORK_PROCESS_START)
*/
class WorkerStartListener implements EventHandlerInterface
{
/**
* @param EventInterface $event
*
* @throws ConnectionPoolException
*/
public function handle(EventInterface $event): void
{
$pools = BeanFactory::getBeans(Pool::class);

/* @var Pool $pool */
foreach ($pools as $pool) {
$pool->initPool();
}
}
}

0 comments on commit a367173

Please sign in to comment.