Skip to content

Commit

Permalink
Add configure path setting
Browse files Browse the repository at this point in the history
  • Loading branch information
louislivi committed Nov 28, 2018
1 parent f509fae commit edf9087
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 42 deletions.
9 changes: 5 additions & 4 deletions README-EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ composer install --no-dev # If you want to contribute to this repo, please DO NO
- `bin/server restart` : Restart service
- `bin/server status` : Query service running status
- `bin/server reload` : Smooth restart
- `bin/server -h` : help
- `bin/server -v` : view service version
- `bin/server -h` : Help
- `bin/server -v` : View service version
- `bin/server -c` : Setting configure path

## Connection Test

Expand Down Expand Up @@ -196,8 +197,8 @@ The configuration files are located in the `smproxy/conf` directory, the upperca
| Account..password password | serverInfo..host database connection address\[Array:read and write multiple\] | databases..maxSpareConns maximum idle connections |
| | serverInfo..prot database port | databases..maxConns maximum number of connections |
| | serverInfo..timeout database timeout duration (seconds) | databases..charset database encoding format |
| | serverInfo..flag TCP type currently supports 0 blocking Not supported 1. Non-blocking | databases..maxSpareExp Maximum idle time |
| | serverInfo..account corresponds to databases.account | databases..startConns service startup connections |
| | serverInfo..account corresponds to databases.account | databases..maxSpareExp Maximum idle time |
| | | databases..startConns service startup connections |

### server.json

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ composer install --no-dev # 如果你想贡献你的代码,请不要使用 --n
- `bin/server reload` : 平滑重启
- `bin/server -h` : 帮助
- `bin/server -v` : 查看当前服务版本
- `bin/server -c` : 设置配置项目录

## SMProxy连接测试

Expand Down Expand Up @@ -182,8 +183,8 @@ QQ群:722124111
| account..password 密码 | serverInfo..host 数据库连接地址\[数组:多读多写\] | databases..maxSpareConns 最大空闲连接数 |
| | serverInfo..prot 数据库端口 | databases..maxConns 最大连接数 |
| | serverInfo..timeout 数据库超时时长(秒) | databases..charset 数据库编码格式 |
| | serverInfo..flag TCP类型目前支持0阻塞 不支持1.非阻塞 | databases..maxSpareExp 最大空闲时间 |
| | serverInfo..account 与 databases.account 对应 | databases..startConns 服务启动连接数 |
| | serverInfo..account 与 databases.account 对应 | databases..maxSpareExp 最大空闲时间 |
| | | databases..startConns 服务启动连接数 |

### server.json

Expand Down
2 changes: 1 addition & 1 deletion box.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"in": "."
}
],
"compression": "GZ",
"compression": "NONE",
"compactors": [
"KevinGH\\Box\\Compactor\\Json",
"KevinGH\\Box\\Compactor\\Php"
Expand Down
8 changes: 3 additions & 5 deletions conf/database.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,23 @@
"host": ["127.0.0.1"],
"port": 3306,
"timeout": 0.5,
"flag": 0,
"account": "root"
},
"read": {
"host": ["127.0.0.1"],
"port": 3306,
"timeout": 0.5,
"flag": 0,
"account": "root"
}
}
},
"databases": {
"dbname": {
"serverInfo": "server1",
"startConns": 10,
"maxSpareConns": 10,
"startConns": "swoole_cpu_num()*10",
"maxSpareConns": "swoole_cpu_num()*10",
"maxSpareExp": 3600,
"maxConns": 20,
"maxConns": "swoole_cpu_num()*20",
"charset": "utf-8"
}
}
Expand Down
4 changes: 2 additions & 2 deletions conf/server.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
}
},
"swoole": {
"worker_num": 1,
"worker_num": "swoole_cpu_num()",
"max_coro_num": 6000,
"open_tcp_nodelay": true,
"daemonize": 1,
"heartbeat_check_interval": 60,
"heartbeat_idle_time": 600,
"reload_async": true,
"log_file": "ROOT/logs/error.log",
"log_file": "ROOT/logs/swoole.log",
"pid_file": "ROOT/logs/pid/server.pid"
},
"swoole_client_setting": {
Expand Down
8 changes: 0 additions & 8 deletions src/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,5 @@ public function bootstrap()
} else {
exit('ERROR: Swoole not installed!');
}

// 读取配置文件
$configName = ROOT . '/conf/';
if (file_exists($configName)) {
define('CONFIG', initConfig($configName));
} else {
smproxy_error("ERROR: $configName No such file or directory!");
}
}
}
35 changes: 28 additions & 7 deletions src/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,48 @@
class Command
{
/**
* @throws \InvalidArgumentException
* @throws \ReflectionException
* @param array $argv
*
* @throws \SMProxy\SMProxyException
*/
public function run(array $argv)
{

$command = count($argv) >= 2 ? $argv[count($argv) - 1] : false;
$configPath = ROOT . '/conf/';
foreach ($argv as $key => $value) {
switch ($value) {
case '-c':
case '--config':
// 读取配置文件
$configPath = $argv[$key + 1];
break;
}
}

if (file_exists($configPath)) {
define('CONFIG_PATH', realpath($configPath) . '/');
define('CONFIG', initConfig(realpath(CONFIG_PATH) . '/'));
} else {
smproxy_error('ERROR: ' . $configPath . ' No such file or directory!');
}

$serverCommand = new ServerCommand();
if (!isset($argv[1]) || '-h' == $argv[1] || '--help' == $argv[1]) {
if (!$command || '-h' == $command || '--help' == $command) {
echo $serverCommand->desc, PHP_EOL;

return;
}
if ('-v' == $argv[1] || '--version' == $argv[1]) {
if ('-v' == $command || '--version' == $command) {
echo $serverCommand->logo, PHP_EOL;

return;
}
if (!method_exists($serverCommand, $argv[1])) {
smproxy_error("ERROR: Unknown option \"{$argv[1]}\"" . PHP_EOL . "Try `server -h' for more information.");
if (!method_exists($serverCommand, $command)) {
smproxy_error("ERROR: Unknown option \"{$command}\"" . PHP_EOL . "Try `server -h' for more information.");

return;
}
PhpHelper::call([$serverCommand, $argv[1]], ...array_copy($argv, 1, count($argv) - 2));
PhpHelper::call([$serverCommand, $command]);
}
}
4 changes: 2 additions & 2 deletions src/MysqlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public function __construct()
*
* @throws SMProxyException
*/
public function connect(string $host, int $port, float $timeout = 0.1, int $flag = 0)
public function connect(string $host, int $port, float $timeout = 0.1)
{
if (!$this->client->connect($host, $port, $timeout = 0.1, $flag = 0)) {
if (!$this->client->connect($host, $port, $timeout = 0.1)) {
$this->onClientError($this ->client);
$mysql_log = Log::getLogger('mysql');
$mysql_log->error("connect {$host}:{$port} failed. Error: {$this->client->errCode}\n");
Expand Down
3 changes: 1 addition & 2 deletions src/MysqlPool/MySQLPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ public static function initConn(\swoole_server $server, int $fd, string $connNam
if (false == $conn->connect(
$serverInfo['host'],
$serverInfo['port'],
$serverInfo['timeout'] ?? 0.1,
$serverInfo['flag'] ?? 0
$serverInfo['timeout'] ?? 0.1
)) {
$mysql_log = Log::getLogger('mysql');
$mysql_log->warning('Cann\'t connect to MySQL server: ' . json_encode($serverInfo));
Expand Down
18 changes: 9 additions & 9 deletions src/SMProxyServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function onWorkerStart(\swoole_server $server, int $worker_id)
} else {
ProcessHelper::setProcessTitle('SMProxy worker process');
}
$this->dbConfig = $this->parseDbConfig(initConfig(ROOT . '/conf/'));
$this->dbConfig = $this->parseDbConfig(initConfig(CONFIG_PATH));
//初始化链接
MySQLPool::init($this->dbConfig);
if ($worker_id === (CONFIG['server']['swoole']['worker_num'] - 1)) {
Expand Down Expand Up @@ -320,6 +320,7 @@ private function getPackageLength(string $data, int $step, int $offset)
*/
private function setStartConns()
{
$clients = [];
foreach ($this->dbConfig as $key => $value) {
if (count(explode('_', $key)) < 2) {
continue;
Expand All @@ -336,7 +337,6 @@ private function setStartConns()
}
$value['startConns'] = ($value['startConns'] > $value['maxSpareConns']) ?
$value['maxSpareConns'] : $value['startConns'];
$clients = [];
while ($value['startConns']) {
//初始化startConns
$mysql = new \Swoole\Coroutine\MySQL();
Expand All @@ -362,14 +362,14 @@ private function setStartConns()
$clients[] = $mysql;
$value['startConns']--;
}
foreach ($clients as $client) {
$client->recv();
if ($client ->errno) {
throw new MySQLException($client ->error);
}
$client->close();
}
foreach ($clients as $client) {
$client->recv();
if ($client ->errno) {
throw new MySQLException($client ->error);
}
unset($clients);
$client->close();
}
unset($clients);
}
}

0 comments on commit edf9087

Please sign in to comment.