Skip to content

Commit 5fd7868

Browse files
committed
性能优化,减少函数调用
1 parent 3f9f840 commit 5fd7868

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

src/Server/Manager.php

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ class Manager
6969
*/
7070
protected $sandbox;
7171

72+
/**
73+
* @var
74+
*/
75+
protected $sandboxMode;
76+
77+
/**
78+
* @var bool
79+
*/
80+
protected $enableAccessLog = false;
81+
7282
/**
7383
* @var AccessOutput
7484
*/
@@ -90,6 +100,20 @@ class Manager
90100
'workerError', 'managerStart', 'managerStop', 'request',
91101
];
92102

103+
/**
104+
* 是否处理静态文件
105+
*
106+
* @var bool
107+
*/
108+
protected $handleStatic = false;
109+
110+
/**
111+
* 静态文件目录
112+
*
113+
* @var
114+
*/
115+
protected $publicPath;
116+
93117
/**
94118
* HTTP server manager constructor.
95119
*
@@ -306,6 +330,12 @@ public function onWorkerStart($server, $workerId)
306330
$this->setLaravelApp();
307331
$this->bindToLaravelApp();
308332
} else {
333+
$config = $this->container->make('config');
334+
$this->handleStatic = $config->get('swoole_http.handle_static_files', true);
335+
$this->publicPath = $config->get('swoole_http.server.public_path', base_path('public'));
336+
$this->enableAccessLog = $config->get('swoole_http.server.enable_access_log', false);
337+
$this->sandboxMode = $config->get('swoole_http.sandbox_mode', true);
338+
309339
$this->setProcessName('worker');
310340
$this->createApplication();
311341
$this->setLaravelApp();
@@ -330,8 +360,7 @@ public function onRequest($swooleRequest, $swooleResponse)
330360

331361
try {
332362
// handle static file request first
333-
$handleStatic = $this->container->make('config')->get('swoole_http.handle_static_files', true);
334-
if ($handleStatic && $this->handleStaticRequest($illuminateRequest, $swooleResponse)) {
363+
if ($this->handleStatic && $this->handleStaticRequest($illuminateRequest, $swooleResponse)) {
335364
return;
336365
}
337366

@@ -352,7 +381,7 @@ public function onRequest($swooleRequest, $swooleResponse)
352381
}
353382
} finally {
354383
// request's access log
355-
if ($this->container->make('config')->get('swoole_http.server.enable_access_log', false)) {
384+
if ($this->enableAccessLog) {
356385
$this->accessOutput->log($illuminateRequest, $illuminateResponse ?? null);
357386
}
358387
// Reset on every request.
@@ -470,8 +499,7 @@ protected function handleStaticRequest($illuminateRequest, $swooleResponse)
470499
return false;
471500
}
472501

473-
$publicPath = $this->container->make('config')->get('swoole_http.server.public_path', base_path('public'));
474-
$filename = $publicPath . $uri;
502+
$filename = $this->publicPath . $uri;
475503

476504
$mime = "text/html";
477505
if (!$isFile = file_exists($filename)) {
@@ -509,7 +537,7 @@ protected function handleStaticRequest($illuminateRequest, $swooleResponse)
509537
*/
510538
protected function resetOnRequest()
511539
{
512-
if ($this->container->make('config')->get('swoole_http.sandbox_mode', true)) {
540+
if ($this->sandboxMode) {
513541
// disable and recycle sandbox resource
514542
is_object($this->sandbox) && $this->sandbox->disable();
515543
}
@@ -565,7 +593,7 @@ protected function setLaravelApp()
565593
*/
566594
protected function getFastApplication($illuminateRequest)
567595
{
568-
if ($this->container->make('config')->get('swoole_http.sandbox_mode', true)) {
596+
if ($this->sandboxMode) {
569597
// set current request to sandbox and enable sandbox
570598
$this->sandbox->setRequest($illuminateRequest);
571599
$this->sandbox->enable();
@@ -581,7 +609,7 @@ protected function getFastApplication($illuminateRequest)
581609
protected function setSandbox()
582610
{
583611
// set application to sandbox environment
584-
if ($this->container->make('config')->get('swoole_http.sandbox_mode', true)) {
612+
if ($this->sandboxMode) {
585613
$this->sandbox = Sandbox::make($this->getApplication());
586614
}
587615
}

src/Tracker/Tracker.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class Tracker
3838

3939
protected $tideways = false;
4040

41+
protected $profilerFilterPath;
42+
4143
/**
4244
* Make a Tracker.
4345
* @return Tracker
@@ -56,6 +58,7 @@ public static function make()
5658
private function __construct()
5759
{
5860
$this->enable = (bool) config('swoole_http.tracker.enable');
61+
$this->profilerFilterPath = config('swoole_http.tracker.profiler.filter_path');
5962

6063
$this->tideways = extension_loaded('tideways');
6164
$this->tideways_xhprof = extension_loaded('tideways_xhprof');
@@ -95,9 +98,8 @@ public function shouldRun($illuminateRequest)
9598
}
9699

97100
// filter path
98-
$filterPath = config('swoole_http.tracker.profiler.filter_path');
99101
$path = $illuminateRequest->getPathInfo();
100-
if (is_array($filterPath) && in_array($path, $filterPath)) {
102+
if (is_array($this->profilerFilterPath) && in_array($path, $this->profilerFilterPath)) {
101103
Show::error("filter path:{$path}");
102104
$ret = false;
103105
}

0 commit comments

Comments
 (0)