Skip to content

Commit 6b4315c

Browse files
authored
Merge pull request #155 from xudianyang/master
优化连接池,提供max_conn,max_time,min_conn选项
2 parents 6ffdc27 + dd8adc2 commit 6b4315c

File tree

5 files changed

+86
-88
lines changed

5 files changed

+86
-88
lines changed

src/Helpers/Common.php

-103 Bytes
Binary file not shown.

src/Pools/AsynPool.php

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ abstract class AsynPool implements IAsynPool
2323
*/
2424
const MAX_TOKEN = 655360;
2525

26+
/**
27+
* 连接池类型名称
28+
*/
29+
const ASYN_NAME = '';
30+
2631
/**
2732
* @var Config 配置对象
2833
*/
@@ -68,22 +73,34 @@ abstract class AsynPool implements IAsynPool
6873
*/
6974
protected $waitConnectNum = 0;
7075

76+
/**
77+
* @var int 连接峰值
78+
*/
79+
protected $establishedConn = 0;
80+
7181
/**
7282
* @var AsynPoolManager 连接池管理器
7383
*/
7484
protected $asynManager;
7585

86+
/**
87+
* @var string 连接池标识
88+
*/
89+
protected $active;
90+
7691
/**
7792
* AsynPool constructor.
7893
*
7994
* @param Config $config 配置对象
95+
* @param string $active 连接池名称
8096
*/
81-
public function __construct($config)
97+
public function __construct($config, $active)
8298
{
8399
$this->callBacks = [];
84100
$this->commands = new \SplQueue();
85101
$this->pool = new \SplQueue();
86102
$this->config = $config;
103+
$this->active = $active;
87104
}
88105

89106
/**
@@ -158,14 +175,58 @@ public function workerInit($workerId)
158175
*/
159176
public function pushToPool($client)
160177
{
161-
$this->pool->push($client);
162-
if (count($this->commands) > 0) {//有残留的任务
163-
$command = $this->commands->shift();
164-
$this->execute($command);
178+
$maxTime = $this->config[static::ASYN_NAME][$this->active]['max_time'] ?? 3600;
179+
$minConn = $this->config[static::ASYN_NAME][$this->active]['min_conn'] ?? 0;
180+
181+
//回归连接
182+
if (((time() - $client->genTime) < $maxTime)
183+
|| (($this->establishedConn + $this->waitConnectNum) <= $minConn)
184+
) {
185+
$this->pool->push($client);
186+
if (count($this->commands) > 0) {//有残留的任务
187+
$command = $this->commands->shift();
188+
$this->execute($command);
189+
}
190+
} else {
191+
$client->close();
165192
}
193+
166194
return $this;
167195
}
168196

197+
/**
198+
* 创建一个连接
199+
*/
200+
public function prepareOne()
201+
{
202+
$maxConn = $this->config[static::ASYN_NAME][$this->active]['max_conn'] ?? null;
203+
if ($maxConn) {
204+
if ($maxConn > ($this->waitConnectNum + $this->establishedConn)) {
205+
$this->reconnect();
206+
}
207+
} else {
208+
$this->reconnect();
209+
}
210+
}
211+
212+
/**
213+
* 返回唯一的连接池名称
214+
*
215+
* @return string
216+
*/
217+
public function getAsynName()
218+
{
219+
return self::ASYN_NAME . '.' . $this->active;
220+
}
221+
222+
/**
223+
* 建立连接
224+
*
225+
* @param null $client
226+
* @return mixed
227+
*/
228+
abstract public function reconnect($client = null);
229+
169230
/**
170231
* 获取同步
171232
*

src/Pools/IAsynPool.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,12 @@ public function addTokenCallback($callback);
8484
* @throws Exception
8585
*/
8686
public function getSync();
87+
88+
/**
89+
* 建立连接
90+
*
91+
* @param null $client
92+
* @return mixed
93+
*/
94+
public function reconnect($client = null);
8795
}

src/Pools/MysqlAsynPool.php

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class MysqlAsynPool extends AsynPool
2626
/**
2727
* 连接池类型名称
2828
*/
29-
const ASYN_NAME = 'mysql.';
29+
const ASYN_NAME = 'mysql';
3030

3131
/**
3232
* @var Miner SQL Builder
@@ -38,16 +38,6 @@ class MysqlAsynPool extends AsynPool
3838
*/
3939
public $bindPool;
4040

41-
/**
42-
* @var int 连接峰值
43-
*/
44-
protected $mysqlMaxCount = 0;
45-
46-
/**
47-
* @var string 连接池标识
48-
*/
49-
private $active;
50-
5141
/**
5242
* MysqlAsynPool constructor.
5343
*
@@ -56,8 +46,7 @@ class MysqlAsynPool extends AsynPool
5646
*/
5747
public function __construct($config, $active)
5848
{
59-
parent::__construct($config);
60-
$this->active = $active;
49+
parent::__construct($config, $active);
6150
$this->bindPool = [];
6251
}
6352

@@ -161,15 +150,7 @@ public function execute($data)
161150

162151
//不是绑定的连接就回归连接
163152
if (!isset($data['bind_id'])) {
164-
//回归连接
165-
if (((time() - $client->genTime) < 3600)
166-
|| (($this->mysqlMaxCount + $this->waitConnectNum) <= 30)
167-
) {
168-
$this->pushToPool($client);
169-
} else {
170-
$client->close();
171-
$this->mysqlMaxCount--;
172-
}
153+
$this->pushToPool($client);
173154
} else {//事务
174155
$bindId = $data['bind_id'];
175156
if ($sql == 'commit' || $sql == 'rollback') {//结束事务
@@ -179,14 +160,6 @@ public function execute($data)
179160
});
180161
}
181162

182-
/**
183-
* 创建一个Mysql连接
184-
*/
185-
public function prepareOne()
186-
{
187-
$this->reconnect();
188-
}
189-
190163
/**
191164
* 重连或者连接
192165
*
@@ -207,8 +180,8 @@ public function reconnect($client = null)
207180
} else {
208181
$client->isClose = false;
209182
if (!isset($client->client_id)) {
210-
$client->client_id = $this->mysqlMaxCount;
211-
$this->mysqlMaxCount++;
183+
$client->client_id = $this->establishedConn;
184+
$this->establishedConn++;
212185
}
213186
$this->pushToPool($client);
214187
}
@@ -239,19 +212,10 @@ public function freeBind($bindId, Context $context = null)
239212
*/
240213
public function onClose($client)
241214
{
215+
$this->establishedConn--;
242216
$client->isClose = true;
243217
}
244218

245-
/**
246-
* 返回唯一的连接池名称
247-
*
248-
* @return string
249-
*/
250-
public function getAsynName()
251-
{
252-
return self::ASYN_NAME . $this->active;
253-
}
254-
255219
/**
256220
* 开启一个同步事务
257221
*

src/Pools/RedisAsynPool.php

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,13 @@ class RedisAsynPool extends AsynPool
2424
/**
2525
* 连接池类型名称
2626
*/
27-
const ASYN_NAME = 'redis.';
27+
const ASYN_NAME = 'redis';
2828

2929
/**
3030
* @var array 连接配置信息
3131
*/
3232
public $connect;
3333

34-
/**
35-
* @var int 连接峰值
36-
*/
37-
protected $redisMaxCount = 0;
38-
39-
/**
40-
* @var string 连接池标识
41-
*/
42-
private $active;
43-
4434
/**
4535
* @var CoroutineRedisProxy 连接池辅助类
4636
*/
@@ -80,8 +70,7 @@ class RedisAsynPool extends AsynPool
8070
*/
8171
public function __construct($config, string $active)
8272
{
83-
parent::__construct($config);
84-
$this->active = $active;
73+
parent::__construct($config, $active);
8574

8675
$config = $this->config->get('redis.' . $this->active, null);
8776
if (!$config) {
@@ -456,27 +445,12 @@ public function execute($data)
456445
//给worker发消息
457446
$this->asynManager->sendMessageToWorker($this, $data);
458447
//回归连接
459-
if (((time() - $client->genTime) < 3600)
460-
|| (($this->redisMaxCount + $this->waitConnectNum) <= 30)
461-
) {
462-
$this->pushToPool($client);
463-
} else {
464-
$client->close();
465-
$this->redisMaxCount--;
466-
}
448+
$this->pushToPool($client);
467449
};
468450
$client->__call($data['name'], array_values($arguments));
469451
}
470452
}
471453

472-
/**
473-
* 创建一个Redis连接
474-
*/
475-
public function prepareOne()
476-
{
477-
$this->reconnect();
478-
}
479-
480454
/**
481455
* 重连或者连接
482456
*
@@ -512,8 +486,8 @@ public function reconnect($client = null)
512486

513487
$client->isClose = false;
514488
if (!isset($client->client_id)) {
515-
$client->client_id = $this->redisMaxCount;
516-
$this->redisMaxCount++;
489+
$client->client_id = $this->establishedConn;
490+
$this->establishedConn++;
517491
}
518492
$this->pushToPool($client);
519493
});
@@ -527,15 +501,6 @@ public function reconnect($client = null)
527501
public function onClose($client)
528502
{
529503
$client->isClose = true;
530-
}
531-
532-
/**
533-
* 返回唯一的连接池名称
534-
*
535-
* @return string
536-
*/
537-
public function getAsynName()
538-
{
539-
return self::ASYN_NAME . $this->active;
504+
$this->establishedConn--;
540505
}
541506
}

0 commit comments

Comments
 (0)