preface

I’ve been working on Swoole recently, and it turns out that listening to people all the time say that Swoole can speed up, it’s just so confusing all the time. After studying Swoole, I have some understanding of my own.

The dark history of PHP-CGI

For PHP to handle web requests, people basically use CGI to do it. So, what is CGI.

CGI

CGI, the full name of Common Gateway Interface, is called “public Gateway Interface” in Chinese. Maybe a lot of people think CGI is a program, yes, I used to think so too. I didn’t know CGI was a protocol until I started studying HTTP in detail, starting with Illustrated HTTP. CGI can be implemented in any programming language, so any language can be used as a backend language for a website (digressing).

PHP-CGI

As mentioned above, CGI is a protocol, so PHP has its own implementation of CGI called php-CGI. However, as the technology evolved, people began to realize that phP-CGI performance was not as good as it should have been. As we know, PHP relies on the configuration file php.ini when it runs. So, every time php-CGI starts working, it’s a completely new process that needs to reload the configuration file and initialize, which is a huge waste of resources and time.

FastCGI

So, to avoid this waste, clever programmers have come up with another way: why don’t we preload the configuration and then, for each task executed, just copy the current process and avoid this waste. FastCGI was born.

FastCGI, short for Fast Common Gateway Interface, is a Fast public Network management Interface. Yeah, that’s another deal. Of course, this protocol doesn’t exist because of PHP.

Apache (httpd)

Almost all Web containers implement FastCGI functionality. The first is HTTPD. For PHP, HTTPD implements a FastCGI module by itself. It preloads the configuration in the php.ini file. When a request comes in that PHP needs to process, PHP does not need to reload php.ini. This is why the HTTPD service must be restarted every time php.ini is changed.

Nginx and PHP – FPM

Php-fpm is also an implementation of FastCGI. Usually we delegate the PHP processing part of Nginx to php-FPM ports and hand it over to php-FPM to handle. Php-fpm also manages the process by preloading the configuration and passing it to the child process.

Swoole

The problem is that phP-FPM implements FastCGI, but it still has to re-run a script to handle a request. A framework like Laravel has to load a lot of dependencies and files at the beginning, which is still an overhead. Let’s take a look at the Laravel public/index.php source code.

require __DIR__.'/.. /bootstrap/autoload.php'; $app = require_once __DIR__.'/.. /bootstrap/app.php'; $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class); $response = $kernel->handle( $request = Illuminate\Http\Request::capture() ); $response->send(); $kernel->terminate($request, $response);Copy the code

This is a lot of time and resources. Every request has to be loaded on one side. It is painful.

So why can’t we have a way of not loading so many dependencies without reloading the FastCGI configuration file as before?

Sure, that’s where Swoole comes in. $app->make; $app->make; So, we can generate $app before we receive the request, and that will be faster, right? We can make a simple modification of it.

require __DIR__.'/.. /bootstrap/autoload.php'; $app = require_once __DIR__.'/.. /bootstrap/app.php'; $serv = new \Swoole\Server\Http('127.0.0.1', 9501); $serv->on('request', function ($req, $res) use ($app) { $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class); $response = $kernel->handle( $request = Illuminate\Http\Request::capture() ); $res->end($response); $kernel->terminate($request, $response); }); $serv->start();Copy the code

Ok, we can now listen on port 9501 by executing this script. Then configure it just as Nginx does php-fpm. This way we can see that the dependencies are loaded clean before the request is received, and all that is left is to process the request.

Of course, my change is very simple, can not be used in the production environment, just to provide an example.

Afterword.

The above is just my own understanding and summary of my own understanding. I’m still working on Swoole, because it’s just a lot of stuff, and it builds up bit by bit. This article may have wrong place, welcome everybody big god to clap brick!