Repository navigation
Expand file tree
/
Copy pathPool.php
More file actions
257 lines (222 loc) · 5.13 KB
/
Copy pathPool.php
File metadata and controls
257 lines (222 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
namespace MyQEE\Server\Async;
/**
* 通用的连接池框架
*/
abstract class Pool
{
/**
* 连接池的尺寸,最大连接数
*
* @var int $poolSize
*/
protected $poolSize;
/**
* idle connection
*
* @var array $resourcePool
*/
protected $resourcePool = [];
protected $resourceNum = 0;
protected $failureCount = 0;
/**
* @var \SplQueue
*/
protected $idlePool;
/**
* @var \SplQueue
*/
protected $taskQueue;
protected $createFunction;
/**
* @var array
*/
protected $config;
/**
* 默认端口
*
* @var int
*/
const DEFAULT_PORT = 0;
/**
* 默认连接池数量
*
* @var int
*/
const DEFAULT_POOL_SIZE = 10;
/**
*
* @param array|string $config 支持字符串 `127.0.0.1:3306` 或数组 `['host' => '127.0.0.1', 'port' => 3306]`
* @param int $poolSize 不设置则使用默认值
* @throws \Exception
*/
public function __construct($config, $poolSize = null)
{
if (is_string($config))
{
$tmp = explode(':', $config);
$config = [
'host' => $tmp[0],
'port' => isset($tmp[1]) ?: static::DEFAULT_PORT,
];
}
if (empty($config['host']))
{
throw new \Exception("require host option.");
}
if (empty($config['port']))
{
$config = static::DEFAULT_PORT;
}
$this->poolSize = $poolSize ?: static::DEFAULT_POOL_SIZE;
$this->taskQueue = new \SplQueue();
$this->idlePool = new \SplQueue();
$this->config = $config;
$this->create([$this, 'connect']);
}
public function __destruct()
{
$this->close();
}
/**
* 加入到连接池中
*
* @param $resource
*/
function join($resource)
{
# 保存到空闲连接池中
$this->resourcePool[spl_object_hash($resource)] = $resource;
$this->release($resource);
}
/**
* 失败计数
*/
function failure()
{
$this->resourceNum--;
$this->failureCount++;
}
/**
* @param $callback
*/
function create($callback)
{
$this->createFunction = $callback;
}
/**
* 修改连接池尺寸
*
* @param $newSize
*/
function setPoolSize($newSize)
{
$this->poolSize = $newSize;
}
/**
* 移除资源
*
* @param $resource
* @return bool
*/
function remove($resource)
{
$rid = spl_object_hash($resource);
if (!isset($this->resourcePool[$rid]))
{
return false;
}
# 从resourcePool中删除
unset($this->resourcePool[$rid]);
$this->resourceNum--;
return true;
}
/**
* 请求资源
*
* @param callable $callback
* @return bool
*/
public function request(callable $callback)
{
# 入队列
$this->taskQueue->enqueue($callback);
if (count($this->idlePool) > 0)
{
# 有可用资源
$this->doTask();
return true;
}
elseif (count($this->resourcePool) < $this->poolSize and $this->resourceNum < $this->poolSize)
{
# 没有可用的资源, 创建新的连接
call_user_func($this->createFunction);
$this->resourceNum++;
return true;
}
else
{
return false;
}
}
/**
* 释放资源
*
* @param $resource
*/
public function release($resource)
{
$this->idlePool->enqueue($resource);
# 有任务要做
if (count($this->taskQueue) > 0)
{
$this->doTask();
}
}
public function isFree()
{
return 0 == $this->taskQueue->count() && $this->idlePool->count() == count($this->resourcePool);
}
protected function doTask()
{
$resource = null;
# 从空闲队列中取出可用的资源
while (count($this->idlePool) > 0)
{
$_resource = $this->idlePool->dequeue();
$rid = spl_object_hash($_resource);
# 资源已经不可用了,连接已关闭
if (!isset($this->resourcePool[$rid]))
{
continue;
}
else
{
# 找到可用连接
$resource = $_resource;
break;
}
}
# 没有可用连接,继续等待
if (!$resource)
{
if (count($this->resourcePool) == 0)
{
call_user_func($this->createFunction);
$this->resourceNum++;
}
return;
}
$callback = $this->taskQueue->dequeue();
call_user_func($callback, $resource);
}
/**
* @return array
*/
function getConfig()
{
return $this->config;
}
abstract protected function connect();
abstract protected function close();
}