Ver código fonte

Use Task mode to render the view template, and adjust some server properties (#5081)

* Use Task mode to render the view, and inject the dependecies via annotation.

* Removed useless abstract model

* Adjust the database connections

* Adjust the server config

* Add WaitGroup to wait the result of child coroutine

* Fixed queries and updates

* Fixed render
黄朝晖 5 anos atrás
pai
commit
e357083310

+ 24 - 10
frameworks/PHP/hyperf/app/Controller/IndexController.php

@@ -15,6 +15,7 @@ namespace App\Controller;
 use App\Model\Fortune;
 use App\Model\World;
 use App\Render;
+use Hyperf\Di\Annotation\Inject;
 use Hyperf\HttpServer\Annotation\Controller;
 use Hyperf\HttpServer\Annotation\GetMapping;
 use Hyperf\HttpServer\Contract\ResponseInterface;
@@ -24,12 +25,25 @@ use Hyperf\HttpServer\Contract\ResponseInterface;
  */
 class IndexController
 {
+
+    /**
+     * @Inject()
+     * @var Render
+     */
+    private $render;
+
+    /**
+     * @Inject()
+     * @var ResponseInterface
+     */
+    private $response;
+
     /**
      * @GetMapping(path="/json")
      */
     public function json()
     {
-        return ['message' => 'Hello, World!'];
+        return $this->response->json(['message' => 'Hello, World!']);
     }
 
     /**
@@ -37,28 +51,29 @@ class IndexController
      */
     public function db()
     {
-        return World::find(random_int(1, 10000));
+        return $this->response->json(World::find(random_int(1, 10000)));
     }
 
     /**
      * @GetMapping(path="/queries/[{queries}]")
      */
-    public function queries($queries = 1, ResponseInterface $response)
+    public function queries($queries = 1)
     {
         $queries = $this->clamp($queries);
 
         $rows = [];
+
         while ($queries--) {
             $rows[] = World::find(random_int(1, 10000));
         }
 
-        return $response->json($rows);
+        return $this->response->json($rows);
     }
 
     /**
      * @GetMapping(path="/fortunes")
      */
-    public function fortunes(Render $render)
+    public function fortunes()
     {
         $rows = Fortune::all();
 
@@ -69,13 +84,13 @@ class IndexController
         $rows->add($insert);
         $rows = $rows->sortBy('message');
 
-        return $render->render('fortunes', ['rows' => $rows]);
+        return $this->render->render('fortunes', ['rows' => $rows]);
     }
 
     /**
      * @GetMapping(path="/updates/[{queries}]")
      */
-    public function updates($queries = 1, ResponseInterface $response)
+    public function updates($queries = 1)
     {
         $queries = $this->clamp($queries);
 
@@ -85,11 +100,10 @@ class IndexController
             $row = World::find(random_int(1, 10000));
             $row->randomNumber = random_int(1, 10000);
             $row->save();
-
             $rows[] = $row;
         }
 
-        return $response->json($rows);
+        return $this->response->json($rows);
     }
 
     /**
@@ -97,7 +111,7 @@ class IndexController
      */
     public function plaintext()
     {
-        return 'Hello, World!';
+        return $this->response->raw('Hello, World!');
     }
 
     private function clamp($value): int

+ 2 - 0
frameworks/PHP/hyperf/app/Model/Fortune.php

@@ -12,6 +12,8 @@ declare(strict_types=1);
 
 namespace App\Model;
 
+use Hyperf\DbConnection\Model\Model;
+
 class Fortune extends Model
 {
     public $timestamps = false;

+ 0 - 19
frameworks/PHP/hyperf/app/Model/Model.php

@@ -1,19 +0,0 @@
-<?php
-
-declare(strict_types=1);
-/**
- * This file is part of Hyperf.
- *
- * @link     https://www.hyperf.io
- * @document https://doc.hyperf.io
- * @contact  [email protected]
- * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
- */
-
-namespace App\Model;
-
-use Hyperf\DbConnection\Model\Model as BaseModel;
-
-abstract class Model extends BaseModel
-{
-}

+ 2 - 0
frameworks/PHP/hyperf/app/Model/World.php

@@ -12,6 +12,8 @@ declare(strict_types=1);
 
 namespace App\Model;
 
+use Hyperf\DbConnection\Model\Model;
+
 class World extends Model
 {
     public $timestamps = false;

+ 2 - 0
frameworks/PHP/hyperf/app/Render.php

@@ -4,6 +4,8 @@ namespace App;
 
 
 use Hyperf\HttpMessage\Stream\SwooleStream;
+use Hyperf\Task\Task;
+use Hyperf\Task\TaskExecutor;
 use Hyperf\View\Engine\EngineInterface;
 use Hyperf\View\Mode;
 

+ 2 - 1
frameworks/PHP/hyperf/composer.json

@@ -30,7 +30,8 @@
         "hyperf/pool": "~1.0.0",
         "hyperf/utils": "~1.0.0",
         "hyperf/view": "~1.0.0",
-        "duncan3dc/blade": "^4.6"
+        "duncan3dc/blade": "^4.6",
+        "hyperf/task": "~1.0.0"
     },
     "require-dev": {
         "swoft/swoole-ide-helper": "^4.2",

+ 2 - 2
frameworks/PHP/hyperf/config/autoload/databases.php

@@ -22,8 +22,8 @@ return [
         'collation' => env('DB_COLLATION', 'utf8_unicode_ci'),
         'prefix' => env('DB_PREFIX', ''),
         'pool' => [
-            'min_connections' => 1,
-            'max_connections' => 100000,
+            'min_connections' => 100,
+            'max_connections' => 512,
             'connect_timeout' => 10.0,
             'wait_timeout' => 3.0,
             'heartbeat' => -1,

+ 9 - 5
frameworks/PHP/hyperf/config/autoload/server.php

@@ -29,17 +29,21 @@ return [
     ],
     'settings' => [
         'enable_coroutine' => true,
-        'worker_num' => swoole_cpu_num() * 2,
+        'reactor_num' => swoole_cpu_num() * 2,
+        'worker_num' => swoole_cpu_num(),
+        'task_worker_num' => swoole_cpu_num(),
         'pid_file' => BASE_PATH . '/runtime/hyperf.pid',
         'open_tcp_nodelay' => true,
-        'max_coroutine' => 1000000,
-        'open_http2_protocol' => false,
-        'max_request' => 1000000,
-        'socket_buffer_size' => 2 * 1024 * 1024,
+        'open_cpu_affinity' => true,
+        'max_connection' => 100000,
+        'log_level' => SWOOLE_LOG_NONE,
     ],
     'callbacks' => [
         SwooleEvent::ON_BEFORE_START => [Hyperf\Framework\Bootstrap\ServerStartCallback::class, 'beforeStart'],
         SwooleEvent::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
         SwooleEvent::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
+        // Task callbacks
+        SwooleEvent::ON_TASK => [Hyperf\Framework\Bootstrap\TaskCallback::class, 'onTask'],
+        SwooleEvent::ON_FINISH => [Hyperf\Framework\Bootstrap\FinishCallback::class, 'onFinish'],
     ],
 ];

+ 1 - 1
frameworks/PHP/hyperf/config/autoload/view.php

@@ -8,7 +8,7 @@ return [
     // 使用的渲染引擎
     'engine' => BladeEngine::class,
     // 不填写则默认为 Task 模式,推荐使用 Task 模式
-    'mode' => Mode::SYNC,
+    'mode' => Mode::TASK,
     'config' => [
         // 若下列文件夹不存在请自行创建
         'view_path' => BASE_PATH . '/storage/view/',