FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

异步mysql使用扩展内置函数 · linuxgnu/framework@3ea440f · GitHub

Repository navigation

Commit 3ea440f

Browse files
committed
异步mysql使用扩展内置函数
1 parent 31dbab7 commit 3ea440f

4 files changed

Lines changed: 248 additions & 122 deletions

File tree

‎libs/Swoole/Async/MySQL.php‎

Lines changed: 134 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ class MySQL {
3535
*/
3636
protected $wait_queue;
3737

38+
/**
39+
* 是否有swoole异步MySQL客户端
40+
* @var bool
41+
*/
42+
protected $haveSwooleAsyncMySQL = false;
43+
3844
/**
3945
* @param array $config
4046
* @param int $pool_size
@@ -57,6 +63,8 @@ public function __construct(array $config, $pool_size = 100) {
5763
}
5864
$this->config = $config;
5965
$this->pool_size = $pool_size;
66+
$this->wait_queue = new \SplQueue();
67+
$this->haveSwooleAsyncMySQL = function_exists('swoole_mysql_query');
6068
}
6169

6270
public function setPoolSize($pool_size) {
@@ -90,15 +98,25 @@ protected function createConnection() {
9098
if (!empty($config['charset'])) {
9199
$db->set_charset($config['charset']);
92100
}
93-
$db_sock = swoole_get_mysqli_sock($db);
94-
swoole_event_add($db_sock, array(
95-
$this,
96-
'onSQLReady'
97-
));
98-
$this->idle_pool[$db_sock] = array(
99-
'object' => $db,
100-
'socket' => $db_sock,
101-
);
101+
$db_sock = swoole_get_mysqli_sock($db);
102+
//内置客户端不需要加入EventLoop
103+
if (!$this->haveSwooleAsyncMySQL)
104+
{
105+
swoole_event_add($db_sock, array(
106+
$this,
107+
'onSQLReady'
108+
));
109+
}
110+
else
111+
{
112+
$db->sock = $db_sock;
113+
}
114+
//保存到空闲连接池中
115+
$this->idle_pool[$db_sock] = array(
116+
'object' => $db,
117+
'socket' => $db_sock,
118+
);
119+
//增加计数
102120
$this->connection_num++;
103121
return 0;
104122
}
@@ -108,24 +126,31 @@ protected function createConnection() {
108126
* @param $db
109127
* @return bool
110128
*/
111-
protected function removeConnection($db) {
112-
if (isset($this->work_pool[$db['socket']])) {
113-
#不能删除正在工作的连接
114-
return false;
115-
}
116-
swoole_event_del($db['socket']);
117-
$db['object']->close();
118-
if (isset($this->idle_pool[$db['socket']])) {
119-
unset($this->idle_pool[$db['socket']]);
120-
}
121-
$this->connection_num--;
122-
}
129+
protected function removeConnection($db)
130+
{
131+
if (isset($this->work_pool[$db['socket']]))
132+
{
133+
#不能删除正在工作的连接
134+
return false;
135+
}
136+
if (!$this->haveSwooleAsyncMySQL)
137+
{
138+
swoole_event_del($db['socket']);
139+
$db['object']->close();
140+
}
141+
if (isset($this->idle_pool[$db['socket']]))
142+
{
143+
unset($this->idle_pool[$db['socket']]);
144+
}
145+
$this->connection_num--;
146+
}
123147

124148
/**
125149
* @param $db_sock
126150
* @return bool
127151
*/
128-
public function onSQLReady($db_sock) {
152+
public function onSQLReady($db, $_result = null) {
153+
$db_sock = $this->haveSwooleAsyncMySQL ? $db->sock : $db;
129154
$task = empty($this->work_pool[$db_sock]) ? null : $this->work_pool[$db_sock];
130155
if (empty($task)) {
131156
#echo "MySQLi Warning: Maybe SQLReady receive a Close event , such as Mysql server close the socket !\n";
@@ -134,20 +159,46 @@ public function onSQLReady($db_sock) {
134159
}
135160
return false;
136161
}
162+
137163
/**
138164
* @var \mysqli $mysqli
139165
*/
140166
$mysqli = $task['mysql']['object'];
141167
$callback = $task['callback'];
142-
if ($result = $mysqli->reap_async_query()) {
143-
call_user_func($callback, $mysqli, $result);
144-
if (is_object($result)) {
145-
mysqli_free_result($result);
146-
}
147-
} else {
148-
call_user_func($callback, $mysqli, $result);
149-
#echo "MySQLi Error: " . mysqli_error($mysqli) . "\n";
150-
}
168+
169+
if ($this->haveSwooleAsyncMySQL)
170+
{
171+
call_user_func($callback, $mysqli, $_result);
172+
}
173+
else
174+
{
175+
$mysqli->_affected_rows = $mysqli->affected_rows;
176+
$mysqli->_insert_id = $mysqli->insert_id;
177+
$mysqli->_errno = $mysqli->errno;
178+
$mysqli->_error = $mysqli->error;
179+
180+
if ($_sql_result = $mysqli->reap_async_query())
181+
{
182+
if ($_sql_result instanceof \mysqli_result)
183+
{
184+
$result = $_sql_result->fetch_all();
185+
}
186+
else
187+
{
188+
$result = $_sql_result;
189+
}
190+
call_user_func($callback, $mysqli, $result);
191+
if (is_object($result))
192+
{
193+
mysqli_free_result($result);
194+
}
195+
}
196+
else
197+
{
198+
call_user_func($callback, $mysqli, false);
199+
}
200+
}
201+
151202
//release mysqli object
152203
unset($this->work_pool[$db_sock]);
153204
if ($this->pool_size < $this->connection_num) {
@@ -197,33 +248,58 @@ public function query($sql, callable $callback) {
197248
* @param callable $callback
198249
*/
199250
protected function doQuery($sql, callable $callback) {
200-
//remove from idle pool
201-
$db = array_pop($this->idle_pool);
202-
/**
203-
* @var \mysqli $mysqli
204-
*/
205-
$mysqli = $db['object'];
206-
for ($i = 0; $i < 2; $i++) {
207-
$result = $mysqli->query($sql, MYSQLI_ASYNC);
208-
if ($result === false) {
209-
if ($mysqli->errno == 2013 or $mysqli->errno == 2006) {
210-
$mysqli->close();
211-
$r = $mysqli->connect();
212-
if ($r === true) {
213-
continue;
214-
}
215-
} else {
216-
#echo "server exception. \n";
217-
$this->connection_num--;
218-
$this->wait_queue->push(array(
219-
'sql' => $sql,
220-
'callback' => $callback,
221-
));
222-
return;
223-
}
224-
}
225-
break;
226-
}
251+
//remove from idle pool
252+
$db = array_pop($this->idle_pool);
253+
/**
254+
* @var \mysqli $mysqli
255+
*/
256+
$mysqli = $db['object'];
257+
258+
if ($this->haveSwooleAsyncMySQL)
259+
{
260+
$ret = swoole_mysql_query($mysqli, $sql, array($this, 'onSQLReady'));
261+
if ($ret == false)
262+
{
263+
trigger_error("swoole_mysql_query($sql) failed.", E_USER_WARNING);
264+
//执行失败了,重新加入到待执行SQL队列中
265+
$this->wait_queue->push(array(
266+
'sql' => $sql,
267+
'callback' => $callback,
268+
));
269+
return;
270+
}
271+
}
272+
else
273+
{
274+
for ($i = 0; $i < 2; $i++)
275+
{
276+
$result = $mysqli->query($sql, MYSQLI_ASYNC);
277+
if ($result === false)
278+
{
279+
if ($mysqli->errno == 2013 or $mysqli->errno == 2006)
280+
{
281+
$mysqli->close();
282+
$r = $mysqli->connect();
283+
if ($r === true)
284+
{
285+
continue;
286+
}
287+
}
288+
else
289+
{
290+
#echo "server exception. \n";
291+
$this->connection_num--;
292+
$this->wait_queue->push(array(
293+
'sql' => $sql,
294+
'callback' => $callback,
295+
));
296+
return;
297+
}
298+
}
299+
break;
300+
}
301+
}
302+
227303
$task['sql'] = $sql;
228304
$task['callback'] = $callback;
229305
$task['mysql'] = $db;

‎libs/Swoole/Network/Server.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ static function setPidFile($pidFile)
4141
self::$pidFile = $pidFile;
4242
}
4343

44+
/**
45+
* 杀死所有进程
46+
* @param $name
47+
* @param int $signo
48+
* @return string
49+
*/
50+
static function killProcessByName($name, $signo = 9)
51+
{
52+
$cmd = 'ps -eaf |grep "' . $name . '" | grep -v "grep"| awk "{print $2}"|xargs kill -'.$signo;
53+
return exec($cmd);
54+
}
55+
4456
/**
4557
*
4658
* $opt->add( 'f|foo:' , 'option requires a value.' );

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL