forked from jmuyuyang/co-psf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnection.php
54 lines (47 loc) · 1.6 KB
/
Connection.php
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
<?php
/**
* connection管理类
* @author yuyang
*
*/
class Connection
{
private static $_connnectionPools = array();
protected $_adapter;
protected $_maxPoolSize;
public function __construct($adapter,$maxPoolSize = 0){
$this->_adapter = $adapter;
$this->_maxPoolSize = $maxPoolSize;
}
protected function _borrowConnection($clientKey,$initData){
if(!isset(self::$_connnectionPools[$clientKey])){
self::$_connnectionPools[$clientKey] = array(
"idlePool" => new \SplQueue(),
"createCount" => 0
);
}
if(!self::$_connnectionPools[$clientKey]['idlePool']->isEmpty()){
return self::$_connnectionPools[$clientKey]['idlePool']->pop();
}else{
if(!$this->_maxPoolSize || self::$_connnectionPools[$clientKey]["createCount"] < $this->_maxPoolSize){
self::$_connnectionPools[$clientKey]['createCount']++;
$adapterObject = call_user_func_array(__NAMESPACE__."\Client\\".$this->_adapter."::factory",$initData);
$adapterObject->setClientKey($clientKey);
return $adapterObject;
}
}
return null;
}
public function get($initData){
$clientKey = $initData['client_key'];
$initData = $initData['init_data'];
$adapterObject = $this->_borrowConnection($clientKey,$initData);
if($adapterObject && $adapterObject instanceof \Client\Base){
return $adapterObject;
}
return null;
}
public function put($connection){
self::$_connnectionPools[$connection->getClientKey()]['idlePool']->push($connection);
}
}