0

I am developing a PHP laravel application. I noticed that it is too slow and after careful debugging I realized that the processing is not slow, but the problem is that the connection takes too long to be terminated and so in this time it does not accept a new request. The following figure shows the performance analysis of a request. Regardless of the type of request, it takes 20-30 seconds to close a connection.

performance

The following figure shows the logs of the local run. The first request was accepted and the second only after the first one was closed, which took more than 20 seconds, although the response comes back in a few milliseconds.

enter image description here

Does anyone know how to fix this?

  • You can close connection when you don't need it anymore – Lk77 Oct 20 '22 at 16:12
  • How? Laravel itself handles the requests. – Milad Barazandeh Oct 20 '22 at 16:13
  • It does, but it will wait for the end of the request to close the connection, if you have some endpoints that you know will take some time, you can close the connection before, ex: close the connection before doing a file download, look for the DB:disconnect function – Lk77 Oct 20 '22 at 16:14
  • You mean I need to close the connection in every single controller? Imagine There is a simple action like this ``` Route::get('/greeting', function () { return 'Hello World'; }); ``` How can I close the connection? – Milad Barazandeh Oct 20 '22 at 16:19
  • No, you don't need to do it everywhere, only when you need it, when you know you don't need the database anymore and you will do something that takes time next, like file download / file upload. Imagine a slow 3g connection downloading a file that you provide with laravel (so not a static file provided with apache/nginx but a dynamic file you generate) the connection can stay open for hours – Lk77 Oct 20 '22 at 16:20
  • This problem occurs even with the simplest routes that return only a text without requiring a DB query or a file read. Even in these cases, the connection remains open for a long time after the response is sent. – Milad Barazandeh Oct 20 '22 at 16:25
  • @Lk77 never heard of this, why would you need to manually close connections? – Flame Oct 20 '22 at 16:51
  • @Flame Let's say for example that your mysql server can handle 10000 connections at the same time, if you don't close your connections before doing long running tasks like file download, you can fill it pretty quickly, 100 users downloading 10 files each and your mysql server is gone, you will get `SQLSTATE[08004] [1040] Too many connections` errors – Lk77 Oct 21 '22 at 06:56
  • It mostly depends on what you are building and the traffic you will be receiving, in our case, we receive around 350 requests per minute, and around 500,000 requests per 24h, it's not that much but we do have spikes and issues regarging 'Too many connections' errors – Lk77 Oct 21 '22 at 07:04
  • @Lk77 you store files in a database? – Flame Oct 21 '22 at 09:34
  • @Flame no we don't store files in database, but we have scenarios where files are generated by php like csv exports – Lk77 Oct 21 '22 at 09:37
  • Are you using php-fpm? are you using nginx? is it not a server config related issue rather than code? Does your code execute without long load times locally vs. production? – Grant Oct 24 '22 at 15:55
  • 1
    Try to run php -S 127.0.0.1:8001 and check your config and .env that you're using 127.0.0.1 instead of localhost – Khaled Hassan Oct 24 '22 at 20:39
  • which package you're using for monitoring time? this is strange because in my laravel 9 request closes right after execution. – Sumit kumar Oct 28 '22 at 21:17
  • @MiladBarazandeh were you able to try any of the options I suggested, to verify if the problem is with the built-in server? – Don't Panic Oct 30 '22 at 23:52

1 Answers1

5

There is not a lot to go on in your question, however one thing we can see from the log messages is that you are using the PHP built-in server (or php artisan serve which uses it). This is well known to be very slow, single-threaded, and can cause PHP applications to stall:

Presumably this is your local dev environment - the PHP docs warn not to use the built-in server on the public internet:

Warning This web server is designed to aid application development. It may also be useful for testing purposes or for application demonstrations that are run in controlled environments. It is not intended to be a full-featured web server. It should not be used on a public network.

Using nginx or Apache and PHP on your production server you should see much better performance. For local development you might be better off setting up Docker, or WAMP, or XAMPP, or the servers included with your distro if you're on some flavour of *nix.

Don't Panic
  • 13,965
  • 5
  • 32
  • 51