Record a memory leak caused by improper use of Larave Http Client Author: Chuwen Time: 2023-12-06 Classification: PHP comment ##Background Larave's [HTTP Client]( https://laravel.com/docs/10.x/http-client "HTTP Client") is based on the secondary encapsulation of Guzzle, which is very convenient for operation, so I wrote similar codes in the business code ```php $url = ' https://xxxx/xxx.json '; $json = Illuminate\Support\Facades\Http::get($url)->json(); //TODO business logic processing ``` The project uses [laravels]( https://github.com/hhxsv5/laravel-s "Laravels"), so the project is resident in memory, but it is observed that the worker memory will increase slowly with the number of requests, and will not automatically reclaim the memory, resulting in an abnormal termination of the program due to insufficient memory: ``` PHP Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 360448 bytes) in /Users/shine/work/xxxxxx-project/vendor/guzzlehttp/psr7/src/Utils.php on line 414 Symfony\Component\ErrorHandler\Error\FatalError Allowed memory size of 67108864 bytes exhausted (tried to allocate 360448 bytes) at vendor/guzzlehttp/psr7/src/Utils.php:414 410▕ }); 411▕ 412▕ try { 413▕ /** @var string|false $contents */ ➜ 414▕ $contents = stream_get_contents($stream); 415▕ 416▕ if ($contents === false) { 417▕ $ex = new \RuntimeException('Unable to read stream contents'); 418▕ } Whoops\Exception\ErrorException Allowed memory size of 67108864 bytes exhausted (tried to allocate 360448 bytes) at vendor/guzzlehttp/psr7/src/Utils.php:414 410▕ }); 411▕ 412▕ try { 413▕ /** @var string|false $contents */ ➜ 414▕ $contents = stream_get_contents($stream); 415▕ 416▕ if ($contents === false) { 417▕ $ex = new \RuntimeException('Unable to read stream contents'); 418▕ } +1 vendor frames 2 [internal]:0 Whoops\Run::handleShutdown() ``` ##Solution By studying the framework code, it is found that Guzzle stream is not closed normally, which causes the memory to accumulate and cannot be recycled Then, in [vendor/larravel/framework/src/Illuminate/Http/Client/Response. php: 247]( https://github.com/laravel/framework/blob/10.x/src/Illuminate/Http/Client/Response.php#L247 "Vendor/larravel/framework/src/Illuminate/Http/Client/Response. php: 247") found a method of 'close()', but this method is not used anywhere. It should be called manually to close stream So change the code as follows ```php $url = ' https://xxxx/xxx.json '; $response = Illuminate\Support\Facades\Http::get($url); $json = $response->json(); //Close stream $response->close(); //TODO business logic processing ``` ##Attached with test memory leak code ```php <? php $i = 0; while (true) { $url = ' http://127.0.0.1:7788/composer.lock '; $json = Illuminate\Support\Facades\Http::get($url)->json(); printf("Count: %d\tPHP Memory: %.1fMB\n", $i++, memory_get_usage(true) / 1024.00 / 1024.00); } ``` ! []( https://cdn.nowtime.cc/2023/12/06/138987942.png ) ##Attached is the code after the test fixes the memory leak ```php <? php $i = 0; while (true) { $url = ' http://127.0.0.1:7788/composer.lock '; $response = Illuminate\Support\Facades\Http::get($url); printf("Count: %d\tPHP Memory: %.1fMB\tResponse size: %.2fKB\n", $i++, memory_get_usage(true) / 1024.00 / 1024.00, strlen($response->body()) / 1024); $response->close(); } ``` ! []( https://cdn.nowtime.cc/2023/12/06/3782614807.png )
Problems with using Laravels+DcatAdmin Author: Chuwen Time: 2023-12-04 Classification: PHP comment According to the Laravels document, add: ```php 'cleaners' => [ Hhxsv5\LaravelS\Illuminate\Cleaners\SessionCleaner::class, Hhxsv5\LaravelS\Illuminate\Cleaners\AuthCleaner::class, Hhxsv5\LaravelS\Illuminate\Cleaners\DcatAdminCleaner::class ], ``` In fact, there will still be problems. For example, the menu bar won't render If it is easy to solve this problem, please refer to https://github.com/hhxsv5/laravel-s/issues/251#issuecomment -971224092 Write a cleanup to clean up. The following are the operation steps I extracted according to this issue ##Step 1. Create a folder such as' app/Cleaners' 2. Create a new file 'app/Cleaners/DcatAdminCleaner. php' whose content is ```php <? php namespace App\Cleaners; use Dcat\Admin\Octane\Listeners\FlushAdminState; class DcatAdminCleaner extends \Hhxsv5\LaravelS\Illuminate\Cleaners\DcatAdminCleaner { public function clean() { $flushAdminState = new FlushAdminState($this->currentApp); $flushAdminState->handle(null); } } ``` 3. Replace the content 'Hhxsv5 LaravelS Illuminate Cleaners DcatAdminCleaner:: class' of' config/laravels. php 'with' App Cleaners DcatAdminCleaner:: class' ! []( https://cdn.nowtime.cc/2023/12/04/1914189827.png )
Record the pit where Node.js failed to build (/bin/sh: vite: no command found error Command failed with exit code 127.) Author: Chuwen Time: 2023-10-13 Classification: WEB development comment ##Troubleshooting After checking, it is found that the 'vite' package is not installed. Then I think that the 'vite' package is written in the 'devDependencies' list of the' package. json 'file Final view [yarn document]( https://classic.yarnpkg.com/lang/en/docs/cli/install/#toc -Yarn install production true false) found a configuration item: If the environment variable 'NODE_ENV' is set to 'production', the package in the 'devDependencies' list will be ignored Then I took a look at the pipeline environment variable configuration, and the environment variable 'NODE_ENV=production' was set as expected, which also confirmed why the prompt 'vite' command could not be found during the construction ##Solution Choose one of the following options ### 1. Remove NODE_ENV environment variable ### 2. When installing dependencies, append the '-- production=false' parameter Complete example: ` yarn install -- production=false`
Tailscale builds a derper and uses the Nginx reverse proxy to encounter the following pitfalls: http: TLS handshake error from 172.40.0.1:44442: cert mismatch with hostname: "" Author: Chuwen Time: 2023-10-10 Classification: talk of everything under the sun comment ##Preamble According to the online tutorials, most of them let you configure Nginx as follows: ```nginx location / { proxy_pass https://127.0.0.1:3443 ; proxy_http_version 1.1; proxy_set_header Host $host; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } ``` However, if you do this, you will find that there is no access at all, and derper will also report the following error: ! []( https://cdn.nowtime.cc/2023/10/10/1996389015.png ) Cert mismatch with hostname **The reason for the abnormality (this is my personal guess, please correct it by Dalao):** It may be that during Nginx's handshake with the backend TLS, deper performs a strong SSL verification. If the hostname and the certificate hostname do not match, the connection is rejected ##Solution ```nginx location / { proxy_pass https://127.0.0.1:3443 ; proxy_http_version 1.1; proxy_set_header Host $host; #Add these two lines of configuration to solve the problem proxy_ssl_server_name on; proxy_ssl_name $host; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } ``` If you access your derper address, the following interface appears, indicating that the configuration is successful ! []( https://cdn.nowtime.cc/2023/10/10/4151549465.png )
Solution to Tailscale Hole Drilling Failure Author: Chuwen Time: 2023-10-09 Classification: Chatter comment Add a line to the configuration 'randomizeClientPort': true ' ! []( https://cdn.nowtime.cc/2023/10/09/1366708902.png )