Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
refactor(Config): Sort Config of httpServer and add Server hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Sep 20, 2019
1 parent bfdddde commit f47c458
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 34 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
- **torrents/tags:** Store torrent tags in TABLE `torrents` (4d573e2)

### Fix
- **Auth:** Fix class check for route in AuthMiddleware failed (007d262)
- **Auth:** Fix user session can't storage in database (30b1049)
- **Auth:** Fix class check for route in AuthMiddleware failed (007d262)
- **Auth/Login:** Fix user can't login after commit `6009dc8` (d509127)
- **Component:** Fix parent::onRequest{Before,After} miss (200926f)
- **Search:** Search keywords in NPHP ways (ccde9c0)
- **Server:** Fix daemon and hot-reload not work (a206217)
- **Torrent:** Fix can't download torrent due to Declaration compatible (36588cc)
- **Torrent/Comment:** Fix user can't see anonymous uploader's comment (bd2d821)
- **User:** Fix User Class miss in string format (3680444)
Expand Down Expand Up @@ -69,6 +70,8 @@
### Style
- **Bencode:** Move Bencode library to App\Library but not part of framework (01abc98)
- **EnvironmentLoader:** Use Dotenv to load Loads environment variables (e6394a6)
- **Folder:** Rename folder name `<root>/private` to `<root>/storage` (f540d8f)
- **Nginx:** Rename Nginx Migration filename (108324e)
- **Redis:** rewrite namespace of cache keys (0c4e1a2)
- **dir:** rename folder `apps\` to `src\` (a01035c)
- **dir:** move apps\public to top dir (cb3beae)
Expand Down
8 changes: 6 additions & 2 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ if (!PHP_SAPI === 'cli') {
*
* phpdotenv provides a automagically loader for
* Loads environment variables from .env to getenv(), $_ENV and $_SERVER
* So we can then use env($name = null, $default = '') to get environment
* variables quickly and simply.
* So we can then use function env($name = null, $default = '') to get
* environment variables quickly and simply.
*
*/
$dotenv = Dotenv\Dotenv::create(RIDPT_ROOT);
Expand All @@ -60,6 +60,10 @@ $dotenv->load(); // $dotenv->overload();
* --------------------------------------------------------------------------
* Start Console Application
* --------------------------------------------------------------------------
*
* A Console Application started to parse our input and run the registered
* commands (defined in `/config/commands.php`).
*
*/
$console = new \Ahc\Cli\Application(PROJECT_NAME, PROJECT_VERSION);
$commands = require RIDPT_ROOT . '/config/commands.php';
Expand Down
File renamed without changes.
65 changes: 49 additions & 16 deletions config/httpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,65 @@
* Time: 2019
*/

use Swoole\Http\Request;
use Swoole\Http\Server;

return [
// 虚拟主机:运行在服务器内的 HTTP 服务
'virtualHost' => [
'host' => '127.0.0.1',
'port' => 9501,
'configFile' => __DIR__ . '/http_base.php',
],
//异步Server对象运行的主机地址
'host' => '127.0.0.1',

//异步Server对象监听的端口
'port' => 9501,

// 独立进程的配置文件
'configFile' => __DIR__ . '/application.php',

// 运行参数:https://wiki.swoole.com/wiki/page/274.html
'settings' => [
'settings' => [
'enable_coroutine' => false, // 开启协程
'reactor_num' => 1, // 连接处理线程数
'worker_num' => 5, // 工作进程数
'pid_file' => '/var/run/rid-httpd.pid', // PID 文件
'log_file' => '/tmp/rid-httpd.log', // 日志文件路径
'max_request' => 3000, // 进程的最大任务数
'max_wait_time' => 60, // 退出等待时间
'reactor_num' => 1, // 连接处理线程数
'worker_num' => 5, // 工作进程数
'pid_file' => dirname(__DIR__) . '/var/runtime/ridpt.pid', // PID 文件
'log_file' => dirname(__DIR__) . '/var/runtime/ridpt.error.log', // 日志文件路径
'max_request' => 3000, // 进程的最大任务数
'max_wait_time' => 60, // 退出等待时间
'package_max_length' => 6242880, // 最大上传包大小,单位 Bytes
'buffer_output_size' => 33554432, // 发送缓存区大小,影响向用户发送文件的最大大小,单位 Bytes
'reload_async' => true, // 异步安全重启
'reload_async' => true, // 异步安全重启
/* 'user' => 'www', // 子进程运行用户 */
],

// TODO Hook
// 注册回调方法Hook
'hook' => [
// 主进程启动事件(onStart)回调
'hook_start' => function (Server $server) { },

// 主进程停止事件回调
'hook_shutdown' => function (Server $server) { },

// 管理进程启动事件回调
'hook_manager_start' => function (Server $server) { },

// 管理进程停止事件回调
'hook_manager_stop' => function (Server $server) { },

// 工作进程启动事件回调
'hook_worker_start' => function (Server $server, int $worker_id) { },

// 工作进程停止事件回调
'hook_worker_stop' => function (Server $server, int $workerId) { },

// 工作进程错误事件(onWorkerError)回调
'hook_worker_error' => function (Server $server, int $workerId, int $workerPid, int $exitCode, int $signal) { },

// 工作进程退出事件(onWorkerExit)回调
'hook_worker_exit' => function (Server $server, int $workerId) { },

// 请求成功(onRequestSuccess)回调
'hook_request_success' => function (Server $server, Request $request) { },

// 请求错误(onRequestException)回调
'hook_request_error' => function (Server $server, Request $request) { },
],

// 用户自定义进程 (用于常驻的任务清理,将会使用Server->addProcess添加到Server
Expand All @@ -51,7 +84,7 @@
],

// 定时器配置
'timer' => [
'timer' => [
//'crontab' => [
// 'class' => App\Timer\CronTabProcess::class,
// 'type' => Rid\Base\Timer::TICK,
Expand Down
51 changes: 36 additions & 15 deletions framework/Http/HttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Rid\Helpers\ProcessHelper;

use Swoole\Server;
use Swoole\Table;
use Throwable;

/**
* Http服务器类
Expand Down Expand Up @@ -53,8 +55,8 @@ public function __construct(array $config)
public function start()
{
// 初始化参数
$this->_host = $this->_config['virtualHost']['host'];
$this->_port = $this->_config['virtualHost']['port'];
$this->_host = $this->_config['host'];
$this->_port = $this->_config['port'];

// 实例化服务器
$this->_server = new \Swoole\Http\Server($this->_host, $this->_port);
Expand Down Expand Up @@ -85,9 +87,9 @@ public function start()
$this->welcome();

// 在此处创建全局的 \Swoole\Table
$configTable = new \Swoole\Table(4096);
$configTable->column('value' , \Swoole\Table::TYPE_STRING, 4096);
$configTable->column('type', \Swoole\Table::TYPE_STRING, 64);
$configTable = new Table(4096);
$configTable->column('value', Table::TYPE_STRING, 4096);
$configTable->column('type', Table::TYPE_STRING, 64);
$configTable->create();
$this->_server->configTable = $configTable;

Expand All @@ -102,6 +104,8 @@ public function start()
public function onStart(Server $server)
{
ProcessHelper::setTitle(PROJECT_NAME . ": master {$this->_host}:{$this->_port}");
// 执行回调
$this->_config['hook']['hook_start'] and call_user_func($this->_config['hook']['hook_start'], $server);
}

/**
Expand All @@ -111,7 +115,8 @@ public function onStart(Server $server)
*/
public function onShutdown(Server $server)
{

// 执行回调
$this->_config['hook']['hook_shutdown'] and call_user_func($this->_config['hook']['hook_shutdown'], $server);
}

/**
Expand All @@ -122,6 +127,8 @@ public function onShutdown(Server $server)
public function onManagerStart(Server $server)
{
ProcessHelper::setTitle(PROJECT_NAME . ": manager"); // 进程命名
// 执行回调
$this->_config['hook']['hook_manager_start'] and call_user_func($this->_config['hook']['hook_manager_start'], $server);
}

/**
Expand All @@ -130,7 +137,8 @@ public function onManagerStart(Server $server)
*/
public function onManagerStop(Server $server)
{

// 执行回调
$this->_config['hook']['hook_manager_stop'] and call_user_func($this->_config['hook']['hook_manager_stop'], $server);
}

/**
Expand All @@ -153,7 +161,7 @@ public function onWorkerStart(\Swoole\Http\Server $server, int $workerId)
}

// 实例化App
$config = require $this->_config['virtualHost']['configFile'];
$config = require $this->_config['configFile'];
$app = new Application($config);
$app->setServ($this->_server);
$app->loadAllComponents();
Expand All @@ -167,6 +175,9 @@ public function onWorkerStart(\Swoole\Http\Server $server, int $workerId)
}
}
}

// 执行回调
$this->_config['hook']['hook_worker_start'] and call_user_func($this->_config['hook']['hook_worker_start'], $server, $workerId);
}

/**
Expand All @@ -176,7 +187,8 @@ public function onWorkerStart(\Swoole\Http\Server $server, int $workerId)
*/
public function onWorkerStop(Server $server, int $workerId)
{

// 执行回调
$this->_config['hook']['hook_worker_stop'] and call_user_func($this->_config['hook']['hook_worker_stop'], $server, $workerId);
}

/**
Expand All @@ -190,7 +202,8 @@ public function onWorkerStop(Server $server, int $workerId)
*/
public function onWorkerError(Server $server, int $workerId, int $workerPid, int $exitCode, int $signal)
{

// 执行回调
$this->_config['hook']['hook_worker_error'] and call_user_func($this->_config['hook']['hook_worker_error'], $server, $workerId, $workerPid, $exitCode, $signal);
}

/**
Expand All @@ -201,7 +214,8 @@ public function onWorkerError(Server $server, int $workerId, int $workerPid, int
*/
public function onWorkerExit(Server $server, int $workerId)
{

// 执行回调
$this->_config['hook']['hook_worker_exit'] and call_user_func($this->_config['hook']['hook_worker_exit'], $server, $workerId);
}

/**
Expand All @@ -212,11 +226,18 @@ public function onWorkerExit(Server $server, int $workerId)
public function onRequest(\Swoole\Http\Request $request, \Swoole\Http\Response $response)
{
try {
// 执行请求
app()->request->setRequester($request);
app()->response->setResponder($response);
app()->run(); // 执行请求
} catch (\Throwable $e) {
app()->run();

// 执行回调
$this->_config['hook']['hook_request_success'] and call_user_func($this->_config['hook']['hook_request_success'], $this->_server, $request);
} catch (Throwable $e) {
// 错误处理
app()->error->handleException($e);
// 执行回调
$this->_config['hook']['hook_request_error'] and call_user_func($this->_config['hook']['hook_request_error'], $this->_server, $request);
}
}

Expand All @@ -231,7 +252,7 @@ private function addCustomProcess()
if ($process_config['title']) ProcessHelper::setTitle('rid-httpd: ' . $process_config['title']);

// FIXME 实例化App
$config = require $this->_config['virtualHost']['configFile'];
$config = require $this->_config['configFile'];
$app = new Application($config);
$app->setServ($this->_server);
$app->loadAllComponents(array_flip($process_config['components']));
Expand Down Expand Up @@ -259,7 +280,7 @@ protected function welcome()
println('Worker Num: ' . $this->settings['worker_num']);
println('Hot Update: ' . ($this->settings['max_request'] == 1 ? 'enabled' : 'disabled'));
println('Coroutine Mode: ' . ($this->settings['enable_coroutine'] ? 'enabled' : 'disabled'));
println('Config File: ' . $this->_config['virtualHost']['configFile']);
println('Config File: ' . $this->_config['configFile']);
println('───────────────────────────────────────');
}

Expand Down
2 changes: 2 additions & 0 deletions var/runtime/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore

0 comments on commit f47c458

Please sign in to comment.