Skip to content

Commit

Permalink
Fix MySQL server has gone away
Browse files Browse the repository at this point in the history
  • Loading branch information
louislivi committed Dec 19, 2018
1 parent 7d9caca commit b5b0248
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
15 changes: 15 additions & 0 deletions src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use SMProxy\Log\Log;
use SMProxy\MysqlPool\MySQLException;
use SMProxy\MysqlPacket\ErrorPacket;
use function SMProxy\Helper\array_iconv;

/**
* Author: Louis Livi <574747417@qq.com>
Expand Down Expand Up @@ -102,4 +104,17 @@ public function parseDbConfig(array $_config)
}
return $config['databases'];
}

protected static function writeErrMessage(int $id, string $msg, int $errno = 0, $sqlState = 'HY000')
{
$err = new ErrorPacket();
$err->packetId = $id;
if ($errno) {
$err->errno = $errno;
}
$err->sqlState = $sqlState;
$err->message = array_iconv($msg);

return $err->write();
}
}
15 changes: 0 additions & 15 deletions src/BaseServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

namespace SMProxy;

use function SMProxy\Helper\array_iconv;
use SMProxy\Helper\ProcessHelper;
use function SMProxy\Helper\smproxy_error;
use SMProxy\Log\Log;
use SMProxy\MysqlPacket\ErrorPacket;
use Swoole\Coroutine;

/**
Expand Down Expand Up @@ -111,17 +109,4 @@ protected function onClose(\swoole_server $server, int $fd)
unset(self::$pool[$cid]);
}
}

protected function writeErrMessage(int $id, string $msg, int $errno = 0, $sqlState = 'HY000')
{
$err = new ErrorPacket();
$err->packetId = $id;
if ($errno) {
$err->errno = $errno;
}
$err->sqlState = $sqlState;
$err->message = array_iconv($msg);

return $err->write();
}
}
1 change: 1 addition & 0 deletions src/MysqlPacket/Util/ErrorCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,4 +524,5 @@ interface ErrorCode
const ER_PLUGIN_IS_NOT_LOADED = 1494;
const ER_MULTI_QUERY_TIMEOUT = 1495;
const ER_MULTI_EXEC_ERROR = 1496;
const ER_HAS_GONE_AWAY = 2006;
}
10 changes: 9 additions & 1 deletion src/MysqlPool/MySQLPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace SMProxy\MysqlPool;

use SMProxy\Base;
use function SMProxy\Helper\getString;
use SMProxy\MysqlPacket\Util\ErrorCode;
use SMProxy\MysqlProxy;
use Swoole\Coroutine\Client;

Expand Down Expand Up @@ -196,7 +198,13 @@ public static function initConn(\swoole_server $server, int $fd, string $connNam
$serverInfo['port'],
$serverInfo['timeout'] ?? 0.1
)) {
throw new MySQLException('Cann\'t connect to MySQL server: ' . json_encode($serverInfo));
--self::$initConnCount[$connName];
$message = 'SMProxy@MySQL server has gone away';
$errMessage = self::writeErrMessage(1, $message, ErrorCode::ER_HAS_GONE_AWAY);
if ($server->exist($fd)) {
$server->send($fd, getString($errMessage));
}
throw new MySQLException($message);
}
$timeout_message = 'Connection ' . $serverInfo['host'] . ':' . $serverInfo['port'] .
' waiting timeout, timeout=' . $serverInfo['timeout'];
Expand Down
7 changes: 3 additions & 4 deletions src/MysqlProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public function __construct(\swoole_server $server, int $fd, \Swoole\Coroutine\C
* @param float $timeout
* @param int $tryStep
*
* @return Client
* @throws SMProxyException
* @return bool|Client
*/
public function connect(string $host, int $port, float $timeout = 0.1, int $tryStep = 0)
{
Expand All @@ -69,7 +68,7 @@ public function connect(string $host, int $port, float $timeout = 0.1, int $tryS
return $this ->connect($host, $port, $timeout, ++$tryStep);
} else {
$this->onClientError($this ->client);
throw new SMProxyException("connect {$host}:{$port} failed. Error: {$this->client->errCode}\n");
return false;
}
} else {
self::go(function () {
Expand All @@ -79,7 +78,7 @@ public function connect(string $host, int $port, float $timeout = 0.1, int $tryS
} else {
$data = $this->client->recv();
}
if (!$data) {
if ($data === '') {
$this ->onClientClose($this ->client);
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/SMProxyServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ private function accessDenied(\swoole_server $server, int $fd, int $serverId)
{
$message = 'SMProxy@access denied for user \'' . $this->source[$fd]->user . '\'@\'' .
$server ->getClientInfo($fd)['remote_ip'] . '\' (using password: YES)';
$errMessage = $this->writeErrMessage($serverId, $message, ErrorCode::ER_ACCESS_DENIED_ERROR, 28000);
$errMessage = self::writeErrMessage($serverId, $message, ErrorCode::ER_ACCESS_DENIED_ERROR, 28000);
if ($server->exist($fd)) {
$server->send($fd, getString($errMessage));
}
Expand Down Expand Up @@ -300,7 +300,7 @@ private function compareModel(string &$model, \swoole_server $server, int $fd)
if (!array_key_exists($key, $this->dbConfig)) {
$message = 'SMProxy@Database config ' . ($this->source[$fd]->database ?: '') . ' ' . $model .
' is not exists!';
$errMessage = $this->writeErrMessage(1, $message, ErrorCode::ER_SYNTAX_ERROR, 42000);
$errMessage = self::writeErrMessage(1, $message, ErrorCode::ER_SYNTAX_ERROR, 42000);
if ($server->exist($fd)) {
$server->send($fd, getString($errMessage));
}
Expand Down

0 comments on commit b5b0248

Please sign in to comment.