Add Memory Leak Detection Middleware to Swoole Tracker (#2747)

This commit is contained in:
沈唁 2020-11-01 18:16:44 +08:00 committed by GitHub
parent 8f33ddbdf9
commit 7cf0d5894c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 3 deletions

View File

@ -233,14 +233,16 @@ php -r "trackerAnalyzeLeak();"
通过调用 `trackerCleanLeak()` 可以清除泄漏日志,重新开始。[了解更多内存检测工具使用细节](https://www.kancloud.cn/swoole-inc/ee-help-wiki/1941569)
在 Hyperf 中如果需要检测 HTTP Server 中的内存泄漏,可以在 `config/autoload/aspects.php` 配置以下 `Aspect`
如果需要在 Hyperf 中检测 HTTP Server 中的内存泄漏,可以在 `config/autoload/middlewares.php` 添加一个全局中间件
```php
<?php
return [
Hyperf\SwooleTracker\Aspect\OnRequestAspect::class,
'http' => [
Hyperf\SwooleTracker\Middleware\HookMallocMiddleware::class,
],
];
```
其他 Server 可以参照此 `Aspect` 进行重写使用
其他类型 Server 同理

View File

@ -17,6 +17,9 @@ use Hyperf\HttpServer\Server;
use Psr\Container\ContainerInterface;
use function trackerHookMalloc;
/**
* @deprecated v2.1 use HookMallocMiddleware instead.
*/
class OnRequestAspect extends AbstractAspect
{
public $classes = [

View File

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\SwooleTracker\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use function trackerHookMalloc;
class HookMallocMiddleware implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if (function_exists('trackerHookMalloc')) {
trackerHookMalloc();
}
return $handler->handle($request);
}
}