Skip to content

Commit

Permalink
性能优化,减少函数调用
Browse files Browse the repository at this point in the history
  • Loading branch information
toxmc committed May 18, 2020
1 parent 5fd7868 commit d9c23a3
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/Server/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ class Manager
*/
protected $tracker;

/**
* 内存限制 0为关闭
* @var int
*/
protected $taskMemoryLimit = 0;
protected $workerMemoryLimit = 0;

/**
* Server events.
*
Expand Down Expand Up @@ -323,14 +330,17 @@ public function onWorkerStart($server, $workerId)
$this->container->make('events')->dispatch(Event::WORKER_START, func_get_args());
$this->clearCache();

$config = $this->container->make('config');
// init laravel app in task workers
if ($server->taskworker) {
$this->taskMemoryLimit = $config->get('swoole_http.server.task_memory_limit');

$this->setProcessName('task');
$this->createTaskApplication();
$this->setLaravelApp();
$this->bindToLaravelApp();
} else {
$config = $this->container->make('config');
$this->workerMemoryLimit = $config->get('swoole_http.server.worker_memory_limit');
$this->handleStatic = $config->get('swoole_http.handle_static_files', true);
$this->publicPath = $config->get('swoole_http.server.public_path', base_path('public'));
$this->enableAccessLog = $config->get('swoole_http.server.enable_access_log', false);
Expand Down Expand Up @@ -762,11 +772,7 @@ protected function logError(Exception $e)
*/
protected function memoryLeakCheck()
{
if (isTaskWorkerStatus()) {
$limit = $this->container->make('config')->get('swoole_http.server.task_memory_limit');
} else {
$limit = $this->container->make('config')->get('swoole_http.server.worker_memory_limit');
}
$limit = isTaskWorkerStatus() ? $this->taskMemoryLimit : $this->workerMemoryLimit;
if ($limit) {
$memory = memory_get_usage(true);
if ($memory > $limit) {
Expand Down

0 comments on commit d9c23a3

Please sign in to comment.