Skip to content
scil edited this page Dec 1, 2018 · 6 revisions

With swoole workers, we can use php 7.4 Preloading on old versions of php.

1. Laravel application is created onWorkerStart

This means: There's an application in each worker process. When a new worker starts, a new application is made.

  • Good 1: Hot Reload On Code Change. You can reload LaravelFly server manually or automatically with files monitor.
  • Good 2: After a worker has handled 'max_request' requests, it will stop and a new worker starts.Maybe it helps set aside suspicions that php can't run long time.

BTW, in Mode FpmLike, all objects are loaded onRequest, like php-fpm. Mode FpmLike does nothing except converting swoole request to laravel request and laravel reponse to swoole response.It just provides a opportunity to use tinker() or similar shells online.

There is a server config 'early_laravel' which allows Laravel Appication created before onWorkerStart. The Good 1 is lost, but the Good 2 is not lost.

2. Load services onWorkerStart as many as possbile?

Before handling a request, laravel does much work.

Let's take a look at Illuminate\Foundation\Http\Kernel::$bootstrappers:

    protected $bootstrappers = [
        \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class,
        \Illuminate\Foundation\Bootstrap\LoadConfiguration::class,
        \Illuminate\Foundation\Bootstrap\HandleExceptions::class,
        \Illuminate\Foundation\Bootstrap\RegisterFacades::class,
        \Illuminate\Foundation\Bootstrap\RegisterProviders::class,
        \Illuminate\Foundation\Bootstrap\BootProviders::class,
    ];

The "$bootstrappers" is what Laravel do before handling a request, LaravelFly execute them before any request, except the last two items "RegisterProviders" and "BootProviders"

In Mode Backup, "RegisterProviders" is placed on "WorkerStart", while "BootProviders" is placed on "request". That means, all providers are registered before any requests and booted after each request.The only exception is, providers in "config('laravelfly.providers_in_request')" are registered and booted after each request.

In Mode Map, providers in "config('laravelfly.providers_on_worker')" are registered and booted before any request. Other providers are treated like Mode Backup.

And In Mode Map, you can define which singleton services to made before any request in "config('laravelfly.providers_in_worker')".