前言
PHP实现了持久连接(也叫长连接),能复用已有连接。听起来挺神的,但是用起来可能会有些误区,尤其是在不同的SAPI下,持久连接表现不一样。
这篇文章主要是从一个简单实验出发,以连接mysql为例,探讨PHP持久连接是怎么一回事。
实验材料
- nginx+fastcgi+php(fpm)5.4+mysql
- php-fpm.conf: pm.max_children = 4
- yum install strace
- 实验脚本
短连接connect.php
长连接 pconnect.php
步骤一:fastcgi下测试短连接脚本
- 在服务器开启strace
[root]#strace -f -e trace=network -e trace=connect $(pidof php-fpm | sed 's/\([0-9]*\)/\-p \1/g')
- 打开浏览器,F5多刷几次脚本:
http://xxx.com/connect.php
- 留意服务器strace的输出结果
[root]# strace -f -e trace=network -e trace=connect $(pidof php-fpm | sed 's/\([0-9]*\)/\-p \1/g')
Process 3828 attached
Process 3827 attached
Process 3826 attached
Process 3825 attached
Process 3824 attached
[pid 3828] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
[pid 3826] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
[pid 3827] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
[pid 3825] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
[pid 3828] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
[pid 3826] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
[pid 3827] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
[pid 3825] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
[pid 3828] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
留意观察:3824是fpm主进程,3825、3826、3827和3828是子进程。
刚刚我刷新了9次,可见子进程每次都会connect
一次mysql。
步骤二:fastcgi下测试持久连接脚本
- 重启fpm
- 在服务器敲好命令准备
strace -f -e trace=network -e trace=connect $(pidof php-fpm | sed 's/\([0-9]*\)/\-p \1/g')
- 打开浏览器,F5多刷几次脚本:
http://xxx.com/connect.php
- 留意服务器strace的输出结果
[root]# strace -f -e trace=network -e trace=connect $(pidof php-fpm | sed 's/\([0-9]*\)/\-p \1/g')
Process 7557 attached
Process 7556 attached
Process 7555 attached
Process 7554 attached
Process 7553 attached
[pid 7555] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
[pid 7557] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
[pid 7554] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
[pid 7556] connect(5, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
留意观察,7553是fpm主进程,54、55、56和57是子进程。
刚刚请求了N遍,当4个进程都连接了一次mysql后,同一子进程后续所有的请求都不需要再connect
mysql了,这就是php-mysql持久连接的作用。
步骤三:cli下测试持久连接脚本
执行命令并留意结果
[root]# strace -f -e trace=network -e trace=connect php pconnect.php
Process 11515 attached
[pid 11515] +++ exited with 0 +++
connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
connect(4, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
resource(4) of type (mysql link persistent)
+++ exited with 0 +++
[root]# strace -f -e trace=network -e trace=connect php pconnect.php
Process 11526 attached
[pid 11526] +++ exited with 0 +++
connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
connect(4, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
resource(4) of type (mysql link persistent)
+++ exited with 0 +++
[root]# strace -f -e trace=network -e trace=connect php pconnect.php
Process 11596 attached
[pid 11596] +++ exited with 0 +++
connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
connect(4, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
resource(4) of type (mysql link persistent)
+++ exited with 0 +++
.....
无论执行多少遍,就算是mysql_pconnect
,每次都是重新connect
mysql,原因是什么?因为每次执行新起了一个进程
结论
简单来说就是一句话:php-mysql的持久连接只在同一个进程里可“复用”,不同进程之间玩不了。
步骤二,fpm的子进程可以常驻,所以只要子进程还在,那么上一次请求创建的持久连接,下一次请求还可以复用。
步骤三,cli每次都新起一个进程,没法把连接复用,那么短连接和持久连接其实是一样的。
附.PHP底层原理
如果没有手动close
,短连接资源变量会在脚本执行完毕回收,但持久连接资源在脚本执行完并不回收,只有在进程结束,mshutdown
统一回收。
实现原理很简单:
短连接资源只放在一张哈希表里,叫regular_list
参考源码:https://github.com/php/php-src/blob/PHP-5.4.41/ext/mysql/php_mysql.c#L1040
持久连接资源多拷贝了一份,放在persistent_list
参考源码:https://github.com/php/php-src/blob/PHP-5.4.41/ext/mysql/php_mysql.c#L906
persistent_list
里面的资源不会在脚本结束时回收。当后续请求来了,先检查persistent_list
,若有可用连接,直接用,实现所谓的“持久连接”。
persistent_list
里面的东西大概这样子,包含key
和zend_rsrc_list_entry
结构体:
所以PHP的持久连接其实就是变量的玩法,不算连接池。
回到上面说的,持久连接只能在同一个进程里复用也是这个原因。
思考题
想一下,persistent_list
里的key
如何产生?
php在连接之前要先去检查有没有可用的资源,拿什么去检查?
翻一下文档或看源码就知道了:
https://github.com/php/php-src/blob/PHP-5.4.41/ext/mysql/php_mysql.c#L813
hashed_details_length = spprintf(&hashed_details, 0, "mysql_%s_%s_%s_%ld", SAFE_STRING(host_and_port), SAFE_STRING(user), SAFE_STRING(passwd), client_flags);
如果这次连接的host
、port
、user
、`passwd和
flag``跟已保存的连接资源一致,那么就可以复用。