Download the PHP package itxiao6/laravel-s without Composer
On this page you can find all versions of the php package itxiao6/laravel-s. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download itxiao6/laravel-s
More information about itxiao6/laravel-s
Files in itxiao6/laravel-s
Package laravel-s
Short Description ? LaravelS: speed up Laravel/Lumen by Swoole, let's fly.
License MIT
Homepage https://github.com/hhxsv5/laravel-s
Informations about the package laravel-s
🚀 Speed up Laravel/Lumen by
Swoole
, let's fly.
Features
-
Built-in Http/WebSocket server
-
Memory resident
-
Gracefully reload
-
Automatically reload when code is modified
-
Support Laravel/Lumen both, good compatibility
- Simple & Out of the box
Requirements
Dependency | Requirement |
---|---|
PHP | >= 5.5.9 |
Swoole | >= 1.7.19 The Newer The Better No longer support PHP5 since 2.0.12 |
Laravel/Lumen | >= 5.1 |
Gzip[optional] | zlib, be used to compress the HTTP response, check by ldconfig -p|grep libz |
Inotify[optional] | inotify, be used to reload all worker processes when your code is modified, check by php --ri inotify |
Install
1.Require package via Composer(packagist).
2.Register service provider.
-
Laravel
: inconfig/app.php
file Lumen
: inbootstrap/app.php
file
3.Publish configuration.
Suggest that do publish after upgrade LaravelS every time
Special for Lumen
: you DO NOT
need to load this configuration manually in bootstrap/app.php
file, LaravelS will load it automatically.
4.Change config/laravels.php
: listen_ip, listen_port, refer Settings.
Run demo
php artisan laravels {start|stop|restart|reload|publish}
Command | Description |
---|---|
start |
Start LaravelS, list the processes by ps -ef|grep laravels |
stop |
Stop LaravelS |
restart |
Restart LaravelS |
reload |
Reload all worker processes(Contain your business & Laravel/Lumen codes), exclude master/manger process |
publish |
Publish configuration file laravels.php into folder config |
Cooperate with Nginx (Recommended)
Cooperate with Apache
Enable WebSocket server
The Listening address of WebSocket Sever is the same as Http Server.
1.Create WebSocket Handler class, and implement interface WebSocketHandlerInterface
.
2.Modify config/laravels.php
.
3.Use swoole_table
to bind FD & UserId, optional, Swoole Table Demo. Also you can use the other global storage services, like Redis/Memcached/MySQL, but be careful that FD will be possible conflicting between multiple Swoole Servers
.
4.Cooperate with Nginx (Recommended)
Refer WebSocket Proxy
Listen events
System events
Usually, you can reset/destroy some
global/static
variables, or change the currentRequest/Response
object.
-
laravels.received_request
After LaravelS parsedswoole_http_request
toIlluminate\Http\Request
, before Laravel's Kernel handles this request. laravels.generated_response
After Laravel's Kernel handled the request, before LaravelS parsesIlluminate\Http\Response
toswoole_http_response
.
Customized asynchronous events
This feature depends on
AsyncTask
ofSwoole
, your need to setswoole.task_worker_num
inconfig/laravels.php
firstly. The performance of asynchronous event processing is influenced by number of Swoole task process, you need to set task_worker_num appropriately.
1.Create event class.
2.Create listener class.
3.Bind event & listeners.
4.Fire event.
Asynchronous task queue
This feature depends on
AsyncTask
ofSwoole
, your need to setswoole.task_worker_num
inconfig/laravels.php
firstly. The performance of task processing is influenced by number of Swoole task process, you need to set task_worker_num appropriately.
1.Create task class.
2.Deliver task.
Millisecond cron job
Wrapper cron job base on Swoole's Millisecond Timer, replace
Linux
Crontab
.
1.Create cron job class.
2.Bind cron job.
3.Note: it will launch multiple timers when build the server cluster, so you need to make sure that launch one timer only to avoid running repetitive task.
Get the instance of swoole_server
in your project
Use swoole_table
1.Define swoole_table
, support multiple.
All defined tables will be created before Swoole starting.
2.Access swoole_table
: all table instances will be bound on swoole_server
, access by app('swoole')->xxxTable
.
Multi-port mixed protocol
For more information, please refer to Swoole Server AddListener
To make our main server support more protocols not just Http and WebSocket, we bring the feature multi-port mixed protocol
of Swoole in LaravelS and name it Socket
. Now, you can build TCP/UDP
applications easily on top of Laravel.
- Create socket handler class, and extend
Hhxsv5\LaravelS\Swoole\Socket\{TcpSocket|UdpSocket|Http|WebSocket}
.
These Socket
connections share the same worker processes with your HTTP
/WebSocket
connections. So it won't be a problem at all if you want to deliver tasks, use swoole_table
, even Laravel components such as DB, Eloquent and so on.
At the same time, you can access swoole_server_port
object directly by member property swoolePort
.
- Register Sockets.
For TCP socket, onConnect
and onClose
events will be blocked when dispatch_mode
of Swoole is 1/3
, so if you want to unblock these two events please set dispatch_mode
to 2/4/5
.
- Test.
-
TCP:
telnet 127.0.0.1 5291
- UDP: [Linux]
echo "Hello LaravelS" > /dev/udp/127.0.0.1/5292
- Register example of other protocols.
-
UDP
-
Http
- WebSocket
Coroutine MySQL
Support coroutine client for MySQL database.
1.Requirements: Swoole>=4.0
, Laravel>=5.1
(Lumen will be supported later).
2.Change the driver
of MySQL connection to sw-co-mysql
in file config/database.php
.
3.Replace Illuminate\Database\DatabaseServiceProvider::class
of providers
to \Hhxsv5\LaravelS\Illuminate\Database\DatabaseServiceProvider::class
in file config/app.php
.
4.Now, you just use QueryBuilder
and ORM
as usual. (Alpha stage, there should be some bugs, please give us your feedback).
Important notices
-
Singleton Issue
-
Under FPM mode, singleton instances will be instantiated and recycled in every request, request start=>instantiate instance=>request end=>recycled instance.
-
Under Swoole Server, All singleton instances will be held in memory, different lifetime from FPM, request start=>instantiate instance=>request end=>do not recycle singleton instance. So need developer to maintain status of singleton instances in ervery request.
-
Common solutions:
-
Reset
status of singleton instances byMiddleware
. - Re-register
ServiceProvider
, addXxxServiceProvider
intoregister_providers
of filelaravels.php
. So that reinitialize singleton instances in ervery request Refer.
-
-
-
Get all info of request from
Illuminate\Http\Request
Object, compatible with $_SERVER/$_ENV/$_GET/$_POST/$_FILES/$_COOKIE/$_REQUEST,CANNOT USE
$_SESSION. -
Respond by
Illuminate\Http\Response
Object, compatible with echo/vardump()/print_r(),CANNOT USE
functions like header()/setcookie()/http_response_code(). - The various
singleton connections
will bememory resident
, recommend to enablepersistent connection
.- Database connection, it
will
reconnect automaticallyimmediately
after disconnect.
- Database connection, it
- Redis connection, it
won't
reconnect automaticallyimmediately
after disconnect, and will throw an exception about lost connection, reconnect next time. You need to make sure thatSELECT DB
correctly before operating Redis every time.
-
global
,static
variables which you declared are need to destroy(reset) manually. - Infinitely appending element into
static
/global
variable will lead to memory leak.
Todo list
-
Connection pool for MySQL/Redis.
- Wrap coroutine clients for MySQL(alpha stage)/Redis/Http.