Home

Awesome

Would you like php 7.4 Preloading? Would you like php coroutine? Today you can use them with Laravel because of Swoole. With LaravalFly, Laravel will begin like Django 3.0 to be fully async-capable.

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).

Thanks to Laravel, Swoole and PsySh.

Notice: Great News! Laravel is supporting Swoole offcially with laravel/octane!

A simple ab test

ab -k -n 1000 -c 10 http://zc.test (21 sql statements were executed in single request)

.fpmFly
Time taken ≈43.5 s12.3 s
Requests per second2381.5
50%303 ms117 ms
80%360 ms153 ms
99%1341 ms239 ms
<details> <summary>Test Env</summary> <div> </div> </details>

Version Compatibility

Quick Start

1.pecl install swoole
Make sure extension=swoole.so in config file for php cli, not for fpm.
Suggest: pecl install inotify

2.composer require "scil/laravel-fly":"dev-master"

3.php vendor/scil/laravel-fly/bin/fly start
If you enable eval(tinker()) and see an error about mkdir, you can start LaravelFly with sudo.

Now, your project is flying and listening to port 9501. Enjoy yourself.

Docker-compose

composer require "scil/laravel-fly":"dev-master"

php artisan vendor:publish --tag=fly-server

# 127.0.0.1:8080
# you can edit this docker-compos file and use your own nginx conf
docker-compose -f vendor/scil/laravel-fly-local/docker/docker-compose.yml up -d

Doc

Configuration

Commands: Start, Reload & Debug

Coding Guideline

Events about LaravelFly

Using tinker when Laravel Working

For Dev

Recommended Packages

Features and Behaviors

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'

// ensure ` const LARAVELFLY_COROUTINE = true;` in fly.conf.php

Route::get('/fly', function () {

    $a = [];
    
    fly(function () use (&$a) {
    
        $a[] = 'coroutine1';
        \co::sleep(2);
        $a[] = 'coroutine1.end';
        \Log::info(implode('; ', $a));
        
        // Eloquent can be used even if current request has ended.
        // $user = new User();
        // $user->name = implode('; ',$a);
        // $user->save();
        
    });

    $a[] = 'outer1';
    

    // go() can use laravel service  with closure
    $log = app('log');
    go(function () use (&$a, $log) {
        $a[] = 'coroutine2';
        \co::sleep(1.2);
        $a[] = 'coroutine2.end';
    });

    $a[] = 'outer2';

    \co::sleep(1);

    $a[] = 'outer3';

    return implode(';', $a);

});

Similar projects that mix swoole and laravel

The main distinguishing feature of LaravalFly is refactoring Laravel, like what Django 3.0 does.

1. laravel-swoole

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.

.techniquework to maintaining relations of cloned objects to avoid Stale Reference
laravel-swooleclone app contaniner and objects to make them safemore work (as app,event...are cloned)
LaravelFly Mode Maprefactor most official objects to make them safe on their ownfew 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.

2. laravel-s

Many great features!

About data pollution? Same technique and problems as laravel-swoole. And neither support coroutine jumping (from one request to another request).

Todo About Improvement

Other Todo