Skip to content

Commit

Permalink
Merge pull request #18 from yunwuxin/master
Browse files Browse the repository at this point in the history
兼容php8.2
  • Loading branch information
hhxsv5 authored Aug 25, 2023
2 parents f70e47d + 237fd1e commit 09e3bc9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
],
"require": {
"php": ">=7.0.0",
"php": ">=8.0.0",
"ext-json": "*",
"ext-swoole": ">=4.2.9"
},
Expand Down
12 changes: 7 additions & 5 deletions src/ConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Smf\ConnectionPool\Connectors\ConnectorInterface;
use Swoole\Coroutine\Channel;
use Swoole\Coroutine;
use WeakMap;

class ConnectionPool implements ConnectionPoolInterface
{
Expand All @@ -14,8 +15,8 @@ class ConnectionPool implements ConnectionPoolInterface
/**@var float The minimum interval to check the idle connections */
const MIN_CHECK_IDLE_INTERVAL = 10;

/**@var string The key about the last active time of connection */
const KEY_LAST_ACTIVE_TIME = '__lat';
/**@var WeakMap The last active time of connection */
protected $lastActiveTime;

/**@var bool Whether the connection pool is initialized */
protected $initialized;
Expand Down Expand Up @@ -66,6 +67,7 @@ class ConnectionPool implements ConnectionPoolInterface
*/
public function __construct(array $poolConfig, ConnectorInterface $connector, array $connectionConfig)
{
$this->lastActiveTime = new WeakMap();
$this->initialized = false;
$this->closed = false;
$this->minActive = $poolConfig['minActive'] ?? 20;
Expand Down Expand Up @@ -161,7 +163,7 @@ public function return($connection): bool
$this->removeConnection($connection);
return false;
}
$connection->{static::KEY_LAST_ACTIVE_TIME} = time();
$this->lastActiveTime[$connection] = time();
$ret = $this->pool->push($connection, static::CHANNEL_TIMEOUT);
if ($ret === false) {
$this->removeConnection($connection);
Expand Down Expand Up @@ -240,7 +242,7 @@ protected function startBalanceTimer(float $interval)
if ($connection === false) {
continue;
}
$lastActiveTime = $connection->{static::KEY_LAST_ACTIVE_TIME} ?? 0;
$lastActiveTime = $this->lastActiveTime[$connection] ?? 0;
if ($now - $lastActiveTime < $this->maxIdleTime) {
$validConnections[] = $connection;
} else {
Expand All @@ -261,7 +263,7 @@ protected function createConnection()
{
$this->connectionCount++;
$connection = $this->connector->connect($this->connectionConfig);
$connection->{static::KEY_LAST_ACTIVE_TIME} = time();
$this->lastActiveTime[$connection] = time();
return $connection;
}

Expand Down

0 comments on commit 09e3bc9

Please sign in to comment.