| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -35,6 +35,12 @@ class MySQL { | |||
| 35 | 35 | */ | |
| 36 | 36 | protected $wait_queue; | |
| 37 | 37 | ||
| 38 | + /** | ||
| 39 | + * 是否有swoole异步MySQL客户端 | ||
| 40 | + * @var bool | ||
| 41 | + */ | ||
| 42 | + protected $haveSwooleAsyncMySQL = false; | ||
| 43 | + | ||
| 38 | 44 | /** | |
| 39 | 45 | * @param array $config | |
| 40 | 46 | * @param int $pool_size | |
@@ -57,6 +63,8 @@ public function __construct(array $config, $pool_size = 100) { | |||
| 57 | 63 | } | |
| 58 | 64 | $this->config = $config; | |
| 59 | 65 | $this->pool_size = $pool_size; | |
| 66 | + $this->wait_queue = new \SplQueue(); | ||
| 67 | + $this->haveSwooleAsyncMySQL = function_exists('swoole_mysql_query'); | ||
| 60 | 68 | } | |
| 61 | 69 | ||
| 62 | 70 | public function setPoolSize($pool_size) { | |
@@ -90,15 +98,25 @@ protected function createConnection() { | |||
| 90 | 98 | if (!empty($config['charset'])) { | |
| 91 | 99 | $db->set_charset($config['charset']); | |
| 92 | 100 | } | |
| 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 | + //增加计数 | ||
| 102 | 120 | $this->connection_num++; | |
| 103 | 121 | return 0; | |
| 104 | 122 | } | |
@@ -108,24 +126,31 @@ protected function createConnection() { | |||
| 108 | 126 | * @param $db | |
| 109 | 127 | * @return bool | |
| 110 | 128 | */ | |
| 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 | + } | ||
| 123 | 147 | ||
| 124 | 148 | /** | |
| 125 | 149 | * @param $db_sock | |
| 126 | 150 | * @return bool | |
| 127 | 151 | */ | |
| 128 | - public function onSQLReady($db_sock) { | ||
| 152 | + public function onSQLReady($db, $_result = null) { | ||
| 153 | + $db_sock = $this->haveSwooleAsyncMySQL ? $db->sock : $db; | ||
| 129 | 154 | $task = empty($this->work_pool[$db_sock]) ? null : $this->work_pool[$db_sock]; | |
| 130 | 155 | if (empty($task)) { | |
| 131 | 156 | #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) { | |||
| 134 | 159 | } | |
| 135 | 160 | return false; | |
| 136 | 161 | } | |
| 162 | + | ||
| 137 | 163 | /** | |
| 138 | 164 | * @var \mysqli $mysqli | |
| 139 | 165 | */ | |
| 140 | 166 | $mysqli = $task['mysql']['object']; | |
| 141 | 167 | $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 | + | ||
| 151 | 202 | //release mysqli object | |
| 152 | 203 | unset($this->work_pool[$db_sock]); | |
| 153 | 204 | if ($this->pool_size < $this->connection_num) { | |
@@ -197,33 +248,58 @@ public function query($sql, callable $callback) { | |||
| 197 | 248 | * @param callable $callback | |
| 198 | 249 | */ | |
| 199 | 250 | 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 | + | ||
| 227 | 303 | $task['sql'] = $sql; | |
| 228 | 304 | $task['callback'] = $callback; | |
| 229 | 305 | $task['mysql'] = $db; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,6 +41,18 @@ static function setPidFile($pidFile) | |||
| 41 | 41 | self::$pidFile = $pidFile; | |
| 42 | 42 | } | |
| 43 | 43 | ||
| 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 | + | ||
| 44 | 56 | /** | |
| 45 | 57 | * | |
| 46 | 58 | * $opt->add( 'f|foo:' , 'option requires a value.' ); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments