readme.md

Would you like php 7.4 Preloading? Would you like php coroutine? Today you can use them because of Swoole.

LaravelFly is a safe solution to speeds up new or old Laravel 5.5+ projects, with preloading and coroutine, while without data pollution or memory leak. And it makes Tinker available online (use tinker while Laravel is responding requests from browsers).

Features and Behaviors

Same codes can run on PHP-FPM or LaravelFly. LaravelFly can be installed on your existing projects without affecting nginx/apache server, that's to say, you can run LaravelFly server and nginx/apache server simultaneously to run the same laravel project. The nginx conf swoole_fallback_to_phpfpm.conf allow you use LaravelFlyServer as the primary server, and the phpfpm as a backup server which will be passed requests when the LaravelFlyServer is unavailable. Another nginx conf use_swoole_or_fpm_depending_on_clients allows us use query string ?useserver=<swoole|fpm|... to select the server between swoole or fpm. That's wonderful for test, such as to use eval(tinker()) as a online debugger for your fpm-supported projects. Apache? There is a example Cooperate with Apache from laravel-s.

Moderate strategy: by default, each Third Party service provider is registered on server worker process (before the first request arrived at server) , booted in request.

By default, all Laravel official services are COROUTINE-FRIENDLY, including mysql and redis. You can make a service or object before any requests. There are two ways:

let the service or object live in multiple requests (only one instance of the service). LaravelFly named it WORKER SERVICE, WORKER OBJECT or COROUTINE-FRIENDLY SERVICE/OBJECT.

cloned the service or object in each request (one instance in one request).LaravelFly named it CLONE SERVICE or CLONE OBJECT. This way is simple, but often has the problem Stale Reference. This type is used widely by laravel-swoole and laravel-s, while used rarely by LaravelFly.

Check server info at /laravel-fly/info. (This feture is under dev and more infomations will be available.)

No support for static files any more, so use it with other servers. Conf examples: nginx or Apache

swoole-job,A Laravel job or event listener can be delivered into a swoole task process and executed at once artisan queue:work needless any more.

exit() or die() in an route action would output content to console or swoole log, and not make server die or reload. If you would like to change that behavior, fork LaravalFly and catch \Swoole\ExitException in LaravelFly\Map\Kernel::handle.

functions fly() and fly2() which are like go() provided by golang or swoole, plus Laravel services can be used in fly() and fly2() without closure. The fly2() has the limited ability to change services in current request, e.g. registering a new event handler for current request. fly2() is not suggested.

A coroutine starting in a request, can still live when the request ends. What's the effect of following route?
It responds with 'coroutine1; outer1; coroutine2; outer2; outer3',
but it write log 'coroutine1; outer1; coroutine2; outer2; outer3; coroutine2.end; coroutine1.end'

Similar projects that mix swoole and laravel

It is alse a safe sollution. It has supported Lumen and websocket. Its doc is great and also useful for LaravelFly.

The main difference is that in laravel-swoole user's code will be processed by a new app cloned from SwooleTW\Http\Server\Application::$application and laravel-swoole updates related container bindings to the new app. However in LaravelFly, the sandbox is not a new app, but an item in the $corDict of the unique application container.
In LaravelFly, most other objects such as app, event.... always keep one object in a worker process, clone is not used at all by default. LaravelFly makes most of laravel objects keep safe on their own. It's about high cohesion & low coupling and the granularity is at the level of app container or services/objects. For users of laravel-swoole, it's a big challenge to handle the relations of multiple packages and objects which to be booted before any requests. Read Stale Reference.

.

technique

work to maintaining relations of cloned objects to avoid Stale Reference

laravel-swoole

clone app contaniner and objects to make them safe

more work (as app,event...are cloned)

LaravelFly Mode Map

refactor most official objects to make them safe on their own

few work ( nothing is cloned by default)

In LaravelFly, another benefit of non-cloned objects is allowing some improvements, such as event listeners cache, route middlewares cache.

db connection pool and redis connection pool. In fly() or fly2(), connections to be used would be fetched from pool, not inherit the same connections from request coroutine. code: $this->connections[$childId] = []; in ConnectionsTrait.php