使用TcpListenerInterface約束該服務(wù)為tcp基礎(chǔ)協(xié)議
class HttpServer implements ProcessInterface, TcpListenerInterface
{
/** @var App|null */
protected $app;
/** @inheritDoc */
public function onWorkerStart(Worker $worker)
{
require_once base_path() . '/support/bootstrap.php';
$this->app = new App($worker, Container::instance(), Log::channel('default'), app_path(), public_path());
Http::requestClass(config('app.request_class', config('server.request_class', Request::class)));
}
/**
* @param TcpConnection $connection
* @param $data
* @return void
*/
public function onMessage(TcpConnection $connection, $data){
call_user_func([$this->app, 'onMessage'], $connection, $data);
}
/** @inheritDoc */
public function onWorkerStop(Worker $worker){}
/** @inheritDoc */
public function onWorkerReload(Worker $worker){}
/** @inheritDoc */
public function onWorkerExit(Worker $worker, $status, $pid){}
}
而不是使用配置
return [
'listen' => 'http://0.0.0.0:8787',
'transport' => 'tcp',
'context' => [],
'name' => 'webman',
'count' => cpu_count() * 2,
'user' => '',
'group' => '',
'reusePort' => false,
'event_loop' => '',
'stop_timeout' => 2,
'pid_file' => runtime_path() . '/webman.pid',
'status_file' => runtime_path() . '/webman.status',
'stdout_file' => runtime_path() . '/logs/stdout.log',
'log_file' => runtime_path() . '/logs/workerman.log',
'max_package_size' => 10 * 1024 * 1024
];
foreach ($config['services'] ?? [] as $server) {
if (!class_exists($server['handler'])) {
echo "process error: class {$server['handler']} not exists\r\n";
continue;
}
$listen = new Worker($server['listen'] ?? null, $server['context'] ?? []);
if (isset($server['listen'])) {
echo "listen: {$server['listen']}\n";
}
$instance = Container::make($server['handler'], $server['constructor'] ?? []);
worker_bind($listen, $instance);
$listen->listen();
}
將start.php中master相關(guān)的內(nèi)容移至helper.php的master_init()
function master_init(array $config){
Worker::$pidFile = $config['pid_file'] ?? runtime_path() . '/webman.pid';
Worker::$stdoutFile = $config['stdout_file'] ?? runtime_path() . '/logs/stdout.log';
Worker::$logFile = $config['log_file'] ?? runtime_path() . '/logs/workerman.log';
Worker::$eventLoopClass = $config['event_loop'] ?? '';
TcpConnection::$defaultMaxPackageSize = $config['max_package_size'] ?? 10 * 1024 * 1024;
if (property_exists(Worker::class, 'statusFile')) {
Worker::$statusFile = $config['status_file'] ?? '';
}
if (property_exists(Worker::class, 'stopTimeout')) {
Worker::$stopTimeout = $config['stop_timeout'] ?? 2;
}
Worker::$onMasterReload = function () {
if (function_exists('opcache_get_status') and function_exists('opcache_invalidate')) {
if ($status = opcache_get_status()) {
if (isset($status['scripts']) && $scripts = $status['scripts']) {
foreach (array_keys($scripts) as $file) {
opcache_invalidate($file, true);
}
}
}
}
};
}
app.php
return [
'debug' => true,
'error_reporting' => E_ALL,
'default_timezone' => 'Asia/Shanghai',
'request_class' => Request::class,
'public_path' => base_path() . DIRECTORY_SEPARATOR . 'public',
'runtime_path' => base_path(false) . DIRECTORY_SEPARATOR . 'runtime',
'controller_suffix' => '',
'event_loop' => '',
'stop_timeout' => 2,
'pid_file' => runtime_path() . '/webman.pid',
'status_file' => runtime_path() . '/webman.status',
'stdout_file' => runtime_path() . '/logs/stdout.log',
'log_file' => runtime_path() . '/logs/workerman.log',
'max_package_size' => 10 * 1024 * 1024
];
webman是否可以提供測(cè)試用例,以便我測(cè)試我的PR代碼是否合規(guī),也方便其他開(kāi)發(fā)者PR的自審;比如 https://www.dtm.pub/other/develop.html
webman是否可以提供issue模板或者是開(kāi)源參與的流程,用于區(qū)分是bug還是提問(wèn)亦或是Request for Comments,這樣其他開(kāi)發(fā)者可以更好的參與進(jìn)來(lái),也可以在PR的時(shí)候關(guān)聯(lián)對(duì)應(yīng)的issue;
webman是否可以在官網(wǎng)描述中提供并強(qiáng)調(diào)一下框架的設(shè)計(jì)哲學(xué)或者說(shuō)設(shè)計(jì)理念,以便開(kāi)源貢獻(xiàn)者或使用者能夠更深的理解框架的內(nèi)涵,統(tǒng)一思路;
1、你的想法和我一樣。目前在開(kāi)發(fā)中的1.4版本可以用process.php配置多個(gè)httpserver,也在考慮將server.php合并到process.php。但是pid_file stdout_file這部分作為服務(wù)器配置放在應(yīng)用配置app.php感覺(jué)也不太合適,單獨(dú)放在server.php又感覺(jué)啰嗦??赡苓€是要有個(gè)server.php,里面只配置pid_file stdout_file等這些,不再配置http服務(wù)。1.4版本將master內(nèi)容封裝到support/App.php 類(lèi)中了,類(lèi)似你說(shuō)的master_init()方法。
worker_start()有一段關(guān)于配置services的處理,這部分于微服務(wù)沒(méi)有關(guān)系,是用來(lái)實(shí)現(xiàn)在一個(gè)進(jìn)程內(nèi)實(shí)現(xiàn)多個(gè)端口的監(jiān)聽(tīng)。這部分用的人不多,后面可能會(huì)去掉。
2、目前沒(méi)有測(cè)試用例。沒(méi)有issue模版,issue大部分是使用咨詢,沒(méi)必要用模版。如果有小的優(yōu)化改動(dòng)直接發(fā)pr即可。
3、webman理念主頁(yè)有介紹,總結(jié)起來(lái)就是內(nèi)核盡量簡(jiǎn)單,其它靠組件、插件擴(kuò)展。