SWOOLE PHP
Ant
2018-03-30
2/39
Introduction
3/39
Swoole is a C extension for PHP
Apache-2.0
+ =
4/39
Ref: https://www.swoole.co.uk/
5/39
Ref: https://www.swoole.co.uk/
6/39
Architecture
7/39
Ref: https://www.yanshuo.me/p/11565
Apache / Nginx + PHP-FPM Swoole
8/39
Ref: https://www.spaceotechnologies.com/why-node-js-best-option-develop-chat-based-mobile-application/
Multi-threaded (Java) Single-thread / Event loop (Node)
9/39
Ref: http://slides.com/albertcht/swoole-redefine-php#/3/6
Multi-threaded (Java) Nginx with PHP-FPM
10/39
Ref: http://slides.com/albertcht/swoole-redefine-php#/3/6
Multi-threaded (Java) Nginx with PHP-FPM
PHP-FPM is PHP FastCGI Process Manager
11/39
Ref: http://ae.koroglu.org/nginx-with-php-fpm-on-centos-6/
PHP Files
Nginx with PHP-FPM
12/39
Ref: http://ae.koroglu.org/nginx-with-php-fpm-on-centos-6/
PHP Files
Nginx with PHP-FPM Swoole
PHP Files
13/39
Example
14/39
Swoole Installation
Ref: https://www.swoole.co.uk/
15/39
Swoole HTTP Server
Ref: https://www.swoole.co.uk/
16/39
Swoole WebSocket Server
Ref: https://www.swoole.co.uk/
17/39
Swoole TCP Server
Ref: https://www.swoole.co.uk/
18/39
Swoole TCP Client
Ref: https://www.swoole.co.uk/
19/39
Swoole Async
Ref: https://www.swoole.co.uk/
20/39
Ref: https://www.swoole.co.uk/
Swoole Task
21/39
Performance
22/39
Performance
Demo
23/39
Connection pool
24/39
Ref: https://docs.oracle.com/cd/B28359_01/appdev.111/b28395/oci09adv.htm#LNOCI87728
Connection pool
➊ Reduces the number of times new connection objects are created.
➋ Promotes connection object reuse.
➌ Quickens the process of getting a connection.
➍ Minimizes the number of stale connections.
25/39
Ref: http://ae.koroglu.org/nginx-with-php-fpm-on-centos-6/
Swoole
PHP Files
26/39
Ref: http://ae.koroglu.org/nginx-with-php-fpm-on-centos-6/
Swoole
PHP Files
DBconnDBconn
27/39
Ref: http://ae.koroglu.org/nginx-with-php-fpm-on-centos-6/
Swoole
PHP Files
DBconn TaskDBconn
28/39
Ref: http://ae.koroglu.org/nginx-with-php-fpm-on-centos-6/
Swoole
PHP Files
DBconn Task
vendor/autoload.php
DBconn
29/39
Async
30/39
Ref: https://wiki.swoole.com/wiki/page/517.html
Async MySQL Client
$db = new swoole_mysql;
$server = array(
'host' => '192.168.1.1',
'port' => 3306,
'user' => 'test',
'password' => 'test',
'database' => 'test',
'charset' => 'utf8',
);
$db->connect($server, function ($db, $r) {
if ($r === false) {
var_dump($db->connect_errno, $db->connect_error);
die;
}
$db->query('show tables', function(swoole_mysql $db, $r) {
if ($r === false)
{
var_dump($db->error, $db->errno);
}
elseif ($r === true )
{
var_dump($db->affected_rows, $db->insert_id);
}
var_dump($r);
$db->close();
});
});
31/39
32/39
Memory leaking !!
33/39
Memory leaking !!
34/39
Ref: https://github.com/guzzle/guzzle/issues/1407
35/39
Coroutine
36/39
Ref: https://wiki.swoole.com/wiki/page/604.html
Coroutine: HTTP Client
$httpclient = new SwooleCoroutineHttpClient('127.0.0.1', 80);
$httpclient->setHeaders(['Host' => "api.mp.qq.com"]);
$httpclient->set([ 'timeout' => 1]);
$httpclient->setDefer();
$httpclient->get('/');
// to do something
$http_res = $httpclient->recv();
37/39
Why not Go(lang)
38/39
Why not Go(lang)
➊ Not most developers in company are familiar with Go(lang).
➋ Not so many Go(lang) developers in Taiwan (future?).
➌ Training time.
➍ Swoole performance is close to Go(lang) in some cases.
39/39
yftzeng@gmail.com
https://www.facebook.com/yftzeng.tw
https://twitter.com/yftzeng

Swoole Love PHP