内容:
背景:提交代码时出现了这个头指针分离自15735cd的问题,提示让:git branch <新分支名> 1c8c2b1,git branch dev 1c8c2b1,而默认创建分支是:git checkout -b dev,这个git branch dev 1c8c2b1,1c8c2b1前面还有c41273a,再前面就是分离的15735cd,于是,把分离后的两个版本,1c8c2b1 和c41273a 建立新的分支,再合并Merge回Master即可达到和提交一样的,找回这丢失的两次提交。


https://git.oschina.net/swoole/swoole/tree/v1.9.11  
git clone https://git.oschina.net/swoole/swoole.git  #git checkout 1.9 然后编译,因默认clone都是最新版。
#git checkout 1.9
分支 1.9 设置为跟踪来自 origin 的远程分支 1.9。
切换到一个新分支 '1.9'

#git branch -a
* 1.9
  master
  remotes/origin/1.9
  remotes/origin/2.0
  remotes/origin/2.0.1
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/swoole-1.7
  remotes/origin/swoole-1.8

#git status
位于分支 1.9
您的分支与上游分支 'origin/1.9' 一致。
无文件要提交,干净的工作区

rm -rf .git  #干掉没有用的.git,我只要这个版本的源代码并打包成tar.gz,然后作为rpmbuild -ba swoole-php71-1.9.11.el7.x86_64.spec


把 swoole-php71-1.9.11.el7.x86_64.spec 放进Git的Server端仓库出现问题了,如下:
#git status
头指针分离自 15735cd
无文件要提交,干净的工作区

[root@danke-php-memcached-reids-zookeeper_bj_sjs_10_51_77_34:/home/test/rpmbuild/SPECS/centos7/php71]
#git checkout master
警告:您正丢下 2 个提交,未和任何分支关联:

  1c8c2b1 rpm包Swoole升级到最新版本,以及PHP-CP一块升级提交SPEC文件。
  c41273a 删除掉的Swoole版本的Spec文件,添加新的Swoole版本的Spec文件。

如果您想要通过创建新分支保存它们,这可能是一个好时候。
如下操作:

git branch <新分支名> 1c8c2b1

切换到分支 'master'
您的分支与上游分支 'origin/master' 一致。


#git rev-list 15735cd     ##指针在这儿丢失了:头指针分离自 15735cd。
15735cda1005046d7d671a2f1692f0e4d6edb977
bfaa314f6a7a212a386b3117c59a7e8be0c077a0


#git rev-list 1c8c2b1     #这个是最新的但是没有被记录到Git的Master分支服务器仓库里面。
1c8c2b1968cb303ee103fff62272a31a1b43fb57   #没有记录到,Log:1c8c2b1 rpm包Swoole升级到最新版本,以及PHP-CP一块升级提交SPEC文件。
c41273ad0cb545db8b6f1d4eb81b006552c915c9   #没有记录到,Log:c41273a 删除掉的Swoole版本的Spec文件,添加新的Swoole版本的Spec文件。
15735cda1005046d7d671a2f1692f0e4d6edb977   #指针在这儿丢失了:头指针分离自 15735cd。
bfaa314f6a7a212a386b3117c59a7e8be0c077a0  

怎么办?
这时候可以执行git branch <新分支名> 1c8c2b1创建一个新的分支,这个分支是基于头指针分离下修改提交的952770d创建的。
然后,再合并回Master里面去即可。
操作步骤如下:
git branch dev 1c8c2b1

切换过Dev分支,看提交的Log在不?在就回到Master合并:
#git checkout dev
切换到分支 'dev'
果然在,如下:
#git log
commit 1c8c2b1968cb303ee103fff62272a31a1b43fb57
Author: 詹金斯 <13880156868@163.com>
Date:   Thu Jun 1 22:56:43 2017 +0800

    rpm包Swoole升级到最新版本,以及PHP-CP一块升级提交SPEC文件。

commit c41273ad0cb545db8b6f1d4eb81b006552c915c9
Author: 詹金斯 <13880156868@163.com>
Date:   Thu Jun 1 20:31:59 2017 +0800

    删除掉的Swoole版本的Spec文件,添加新的Swoole版本的Spec文件。

回到Master,直接合并到Master分支,如下:
Merge branch 'dev'
Master的头指针出现:头指针分离自 15735cd,新建立Dev分支,git branch dev 1c8c2b1,合并回Master来。 ----这一行是注释,后Vim保存即可。
# 请输入一个提交信息以解释此合并的必要性,尤其是将一个更新后的上游分支
# 合并到主题分支。
#
# 以 '#' 开头的行将被忽略,而且空提交说明将会终止提交。

保存后,跳到这儿了:
#git merge dev
自动合并 centos7/php71/swoole-php71-1.9.11.el7.x86_64.spec
Merge made by the 'recursive' strategy.
centos7/php71/php-cp-php71-1.5.0.el7.x86_64.spec                               |  2 +-
...swoole-php71-1.9.10.el7.x86_64.spec => swoole-php71-1.9.11.el7.x86_64.spec} | 10 +++++-----
centos7/tomcat-7.0.77.el6.x86_64.spec                                          |  1 -
3 files changed, 6 insertions(+), 7 deletions(-)
rename centos7/php71/{swoole-php71-1.9.10.el7.x86_64.spec => swoole-php71-1.9.11.el7.x86_64.spec} (89%)


再确定合并及注释也Ok了,如下:
#git log
commit 432fbac58b19eb6a799d356b82d087a5340a1411
Merge: 78dab85 1c8c2b1
Author: 詹金斯 <13880156868@163.com>
Date:   Thu Jun 1 23:24:33 2017 +0800

    Merge branch 'dev'
    Master的头指针出现:头指针分离自 15735cd,新建立Dev分支,git branch dev 1c8c2b1,合并回Master来。

成功!
git branch -D 大写的D 删除分支,git branch -D dev:
#git branch -D dev
已删除分支 dev(曾为 1c8c2b1)。
参考:http://blog.csdn.net/yuelengloulan/article/details/72823420
Q:我在frame里面用了add processs以后,就一直出现这个错误。不断的有新的进程,刷屏。
A:你创建的进程应该没有任何代码,所以立即退出了,主进程又会重新拉起。这样就造成死循环了。
http://group.swoole.com/question/105990

Me:
[2017-04-23 00:04:26 @10362.0]  ERROR   zm_deactivate_swoole (ERROR 9003): worker process is terminated by exit()/die().
[2017-04-23 00:04:26 @10363.0]  ERROR   zm_deactivate_swoole (ERROR 9003): worker process is terminated by exit()/die().

背景:研究一下这个Swoole2.X(V2.0.8)的协程,于是试了一下Tcp/Mysql/Redis/Http(在一个以Http形式下,协程了:TcpServer配置、Redis配置、Mysql配置、Http,最后的接口返回时间以里面最长的那个为准,也就是一般是Mysql耗时最长,它就是最长的接口返回时间了。)一起上,Swoole顾问觉得要想性能高还得加上RPC,再就是在一些数据的Pack和Unpack性能上予以加强,以争取每个请求都非常快非常高效,一请求发现出现了段错误,最新版本出现小小的问题属于正常性况,关键是如何反馈问题,看了一下Swoole的Wiki是如何提Bug的连接,于是就试着提一个Bug吧,给其它用Swoole万一出现Coredump的兄弟们作一个示范,进行提Bug的和捕获相关的Coredump的输出,参考Swoole的Wiki,地址:https://wiki.swoole.com/wiki/page/10.html。

一、假如在你的项目中出现段错误,怎么办?重新编译打包Swoole的源码并打成RPm包,打开debug,参数是: --enable-debug ,如下:
swoole-php71-2.0.7.el7.x86_64.spec 打包./configure部分:
#./configure --with-php-config=%{php_bin}/php-config --enable-coroutine --enable-async-redis
./configure --with-php-config=%{php_bin}/php-config --enable-coroutine --enable-async-redis --enable-debug

二、Mysql部分做测试,加上用户权限,那个Tcp就用Swoole的示例打开9501端口,官网上有很简单的TcpServer样例(略),Mysql部分如下:
Mysql服务器上@101.200.*.135:
#mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 93109
Server version: 8.0.0-dmr-log Source distribution

mysql> grant all privileges on test.* to swooleUser@10.51.*.34 identified by "test123";
Query OK, 0 rows affected, 1 warning (0.04 sec)

mysql> flush privileges;    
Query OK, 0 rows affected (0.01 sec)

@测试机器@123.57.*.183
#rpm -ql mysql-client-8.0.0-170523211353.el7.centos.x86_64
/usr/local/mysql/bin/mysql
#/usr/local/mysql/bin/mysql -h10.44.*.177 -uswooleUser -p
Enter password:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.00 sec)



三、根据TcpServer配置、Redis配置、Mysql配置、Http(试着访问百度的Http页面),一般来讲都是Mysql慢,所以,它才是重点,代码如下:
coroutinemysql.php

<?php
$server = new Swoole\Http\Server("123.57.*.183", 9507, SWOOLE_BASE);
$server->set([
    'worker_num' => 1,
]);

$server->on('Request', function ($request, $response) {
    $tcpclient = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
    $tcpclient->connect('127.0.0.1', 9501,0.5);
    $tcpclient->send("hello world\n");

    $redis = new Swoole\Coroutine\Redis();
    $redis->connect('123.57.*.183', 6379);
    $redis->setDefer();
    $redis->get('key');

    $mysql = new Swoole\Coroutine\MySQL();
    $ret = $mysql->connect([
    'host' => '10.44.*.177',
    'port' => 3306,
        'user' => 'swooleUser',
        'password' => 'test123',
        'database' => 'test',
    ]);
    if ($ret == false) {
        $response->end("MySQL connect fail!");
        return;
    }
    $mysql->setDefer();
    $mysql->query('select sleep(1)');

    $httpclient = new Swoole\Coroutine\Http\Client('123.125.114.144', 80);
    $httpclient->setHeaders(['Host' => "baidu.com"]);
    $httpclient->set([ 'timeout' => 1]);
    $httpclient->setDefer();
    $httpclient->get('/');

    $tcp_res  = $tcpclient->recv();
    $redis_res = $redis->recv();
    $mysql_res = $mysql->recv();
    $http_res  = $httpclient->recv();

    $response->end('Test End');
});
$server->start();




四、按Swoole官方的Wiki教程,设置一下吐核,From: https://wiki.swoole.com/wiki/page/10.html :
#ulimit -c unlimited

五、启动对外提供的Http协议的9507端口访问进来后的多种协议并发的协程服务,如下:
#php  coroutinemysql.php


六、访问Http时再触发一下协程:
触发一下:http://123.57.*.183:9507/

七、生成了段错误的Coredump文件,如下:
#php  coroutinemysql.php
段错误(吐核)

#ls *core*              
core.8494


八、用GDB去跟进其堆栈,BT显示调用层级定位出现问题所在位置:
#gdb php /tmp/core.8494
(gdb) bt
#0  0x00007f146b33e9a5 in http_client_coro_send_http_request (zobject=0x7f147248d190)
    at /home/test/rpmbuild/BUILD/swoole-src-2.0.7-stable/swoole_http_client_coro.c:501
#1  0x00007f146b383c93 in swClient_onWrite (reactor=<optimized out>, event=0x7ffd0c185a10)
    at /home/test/rpmbuild/BUILD/swoole-src-2.0.7-stable/src/network/Client.c:1109
#2  0x00007f146b37d63c in swReactorEpoll_wait (reactor=0x1dfc338, timeo=<optimized out>)
    at /home/test/rpmbuild/BUILD/swoole-src-2.0.7-stable/src/reactor/ReactorEpoll.c:270
#3  0x00007f146b389de9 in swReactorProcess_loop (pool=<optimized out>,
    worker=worker@entry=0x7ffd0c185ae0)
    at /home/test/rpmbuild/BUILD/swoole-src-2.0.7-stable/src/network/ReactorProcess.c:379
#4  0x00007f146b38a727 in swReactorProcess_start (serv=serv@entry=0x1dc7cf0)
    at /home/test/rpmbuild/BUILD/swoole-src-2.0.7-stable/src/network/ReactorProcess.c:112
#5  0x00007f146b381538 in swServer_start (serv=serv@entry=0x1dc7cf0)
    at /home/test/rpmbuild/BUILD/swoole-src-2.0.7-stable/src/network/Server.c:696
#6  0x00007f146b333730 in zim_swoole_http_server_start (execute_data=0x7f14724130a0,
    return_value=0x7ffd0c185cb0)
    at /home/test/rpmbuild/BUILD/swoole-src-2.0.7-stable/swoole_http_server.c:1567
#7  0x00000000008ad0c6 in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER ()
#8  0x000000000085bd3b in execute_ex ()
#9  0x00000000008aee24 in zend_execute ()
#10 0x0000000000816fc4 in zend_execute_scripts ()
#11 0x00000000007b8520 in php_execute_script ()
#12 0x00000000008b0fbf in do_cli ()
#13 0x000000000043d450 in main ()

(gdb)f 1
#1  0x00007f146b383c93 in swClient_onWrite (reactor=<optimized out>, event=0x7ffd0c185a10)
    at /home/test/rpmbuild/BUILD/swoole-src-2.0.7-stable/src/network/Client.c:1109
1109                cli->onConnect(cli);

(gdb)f 0
#0  0x00007f146b33e9a5 in http_client_coro_send_http_request (zobject=0x7f147248d190)
    at /home/test/rpmbuild/BUILD/swoole-src-2.0.7-stable/swoole_http_client_coro.c:501
501         if (!http->cli || !http->cli->socket )

九、用valgrind大体排查一下是否有内存溢出等:
[root@测试服务器:/tmp]
#USE_ZEND_ALLOC=0 valgrind php coroutinemysql.php
==12833== Memcheck, a memory error detector
==12833== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==12833== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==12833== Command: php coroutinemysql.php
==12833==

==12833== Invalid read of size 8
==12833==    at 0x121249A5: http_client_coro_send_http_request (swoole_http_client_coro.c:501)
==12833==    by 0x12169C92: swClient_onWrite (Client.c:1109)
==12833==    by 0x1216363B: swReactorEpoll_wait (ReactorEpoll.c:270)
==12833==    by 0x1216FDE8: swReactorProcess_loop (ReactorProcess.c:379)
==12833==    by 0x12170726: swReactorProcess_start (ReactorProcess.c:112)
==12833==    by 0x12167537: swServer_start (Server.c:696)
==12833==    by 0x1211972F: zim_swoole_http_server_start (swoole_http_server.c:1567)
==12833==    by 0x8AD0C5: ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER (in /usr/local/php/bin/php)
==12833==    by 0x85BD3A: execute_ex (in /usr/local/php/bin/php)
==12833==    by 0x8AEE23: zend_execute (in /usr/local/php/bin/php)
==12833==    by 0x816FC3: zend_execute_scripts (in /usr/local/php/bin/php)
==12833==    by 0x7B851F: php_execute_script (in /usr/local/php/bin/php)
==12833==  Address 0x100000188 is not stack'd, malloc'd or (recently) free'd
==12833==
==12833==
==12833== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==12833==  Access not within mapped region at address 0x100000188
==12833==    at 0x121249A5: http_client_coro_send_http_request (swoole_http_client_coro.c:501)
==12833==    by 0x12169C92: swClient_onWrite (Client.c:1109)
==12833==    by 0x1216363B: swReactorEpoll_wait (ReactorEpoll.c:270)
==12833==    by 0x1216FDE8: swReactorProcess_loop (ReactorProcess.c:379)
==12833==    by 0x12170726: swReactorProcess_start (ReactorProcess.c:112)
==12833==    by 0x12167537: swServer_start (Server.c:696)
==12833==    by 0x1211972F: zim_swoole_http_server_start (swoole_http_server.c:1567)
==12833==    by 0x8AD0C5: ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER (in /usr/local/php/bin/php)
==12833==    by 0x85BD3A: execute_ex (in /usr/local/php/bin/php)
==12833==    by 0x8AEE23: zend_execute (in /usr/local/php/bin/php)
==12833==    by 0x816FC3: zend_execute_scripts (in /usr/local/php/bin/php)
==12833==    by 0x7B851F: php_execute_script (in /usr/local/php/bin/php)
==12833==  If you believe this happened as a result of a stack
==12833==  overflow in your program's main thread (unlikely but
==12833==  possible), you can try to increase the size of the
==12833==  main thread stack using the --main-stacksize= flag.
==12833==  The main thread stack size used in this run was 8388608.
==12833==
==12833== HEAP SUMMARY:
==12833==     in use at exit: 22,357,752 bytes in 22,711 blocks
==12833==   total heap usage: 29,852 allocs, 7,141 frees, 23,257,973 bytes allocated
==12833==
==12833== LEAK SUMMARY:
==12833==    definitely lost: 64 bytes in 2 blocks
==12833==    indirectly lost: 4,096 bytes in 2 blocks
==12833==      possibly lost: 1,813,286 bytes in 18,399 blocks
==12833==    still reachable: 20,540,306 bytes in 4,308 blocks
==12833==         suppressed: 0 bytes in 0 blocks
==12833== Rerun with --leak-check=full to see details of leaked memory
==12833==
==12833== For counts of detected and suppressed errors, rerun with: -v
==12833== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
段错误


十、在PHP代码上加加减减,定位一下PHP这个协程是哪个协议出现问题,经定位发现是发起http这块的协程可能有问题,如下:
再细节一点:
#php coroutinemysql.php
段错误(吐核)
这一行引起的:$httpclient->setDefer();     #setDefer函数的Wiki地址:https://wiki.swoole.com/wiki/page/607.html

$httpclient = new Swoole\Coroutine\Http\Client('123.125.114.144', 80);
$httpclient->setHeaders(['Host' => "baidu.com"]);
$httpclient->set([ 'timeout' => 3]);
$httpclient->setDefer();            #注释这一行,就会报下面的警告,不会Coredump,如下:                                                                
$httpclient->get('/');

#php coroutinemysql.php   #浏览器访问一下:http://123.57.*.183:9507/
PHP Warning:  Swoole\Coroutine\Http\Client::recv(): you should not use recv without defer  in /tmp/coroutinemysql.php on line 40
PHP Warning:  Swoole\Coroutine\Http\Client::recv(): you should not use recv without defer  in /tmp/coroutinemysql.php on line 40

在服务器上CURL形式访问一下百度,没毛病:
curl -i -H"Host:baidu.com" http://123.125.114.144            
HTTP/1.1 200 OK  
<html>
<meta http-equiv=refresh content=0;url=http://www.baidu.com/>
</html>


十一、提交出现Coredump以及和环境相关的问题给Swoole的CoreTeam研发小组的兄弟:
请将上面的得到的信息,连同机器信息,包括php -v gcc -v uname -a 提交到Github Issues页面或者发送邮件到 team@swoole.com。
若确定是Swoole的问题,开发组会快速解决问题。
其它涉及到编译器和PHP版本、系统环境:
#php -v
PHP 7.1.5 (cli) (built: May 23 2017 10:35:57) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.5, Copyright (c) 1999-2017, by Zend Technologies

#gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
目标:x86_64-redhat-linux
配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
线程模型:posix
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)


#uname -a
Linux 测试服务器 4.11.2-1.el7.elrepo.x86_64 #1 SMP Sun May 21 19:31:34 EDT 2017 x86_64 x86_64 x86_64 GNU/Linux


#php --ri swoole

swoole

swoole support => enabled
Version => 2.0.8
Author => tianfeng.han[email: mikan.tenny@gmail.com]
epoll => enabled
eventfd => enabled
timerfd => enabled
signalfd => enabled
cpu affinity => enabled
spinlock => enabled
rwlock => enabled
async redis client => enabled
async http/websocket client => enabled
Linux Native AIO => enabled
pcre => enabled
zlib => enabled
mutex_timedlock => enabled
pthread_barrier => enabled

Directive => Local Value => Master Value
swoole.aio_thread_num => 2 => 2
swoole.display_errors => On => On
swoole.use_namespace => On => On
swoole.fast_serialize => 1 => 1
swoole.unixsock_buffer_size => 8388608 => 8388608


EOF
核心语句:
swoole群上海求加入-张振亚-swoole-2345  14:38:38
php class 有一些魔术函数你知道吗
回忆未来-向东-Jàck  14:39:43
知道
__set __get
swoole群上海求加入-张振亚-swoole-2345  14:43:20
on方法只是注册一些事件
比如close。
swoole群上海求加入-张振亚-swoole-2345  14:44:28
swoole 连接关闭的时候 执行类似于  call_user_func($callable, $this, $data);
第一个参数是方法名,我们上面的写法是匿名函数
第二个参数就是对象本身,也就是swoole_client 对象


========================================================

swoole_client_function.php

php swoole_client_function.php
试着连接Server.php里获取硬件温度的旁端口操作失败
试着连接Server.php里获取硬件温度的旁端口操作失败


回忆未来-向东-Jàck  14:30:29
感觉$swoole_client->on("Receive", function(swoole_client $client和$swoole_client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
感觉是传个一个结构体是:swoole_client的,变量不一样,其实是一个东西是吧?兄弟这块哪儿有说呢?
$swoole_client和
function(swoole_client $client ...是一个东西。
这块感觉有啥知识点,兄弟能讲下吗?
swoole群上海求加入-张振亚-swoole-2345  14:31:40
就是一个值传递
回忆未来-向东-Jàck  14:32:39
但变量不一样呀,一个是$swoole_client,而另一个变量是,swoole_client $client,它是$client呀,$swoole_client和$client怎么就一样了?
swoole群上海求加入-张振亚-swoole-2345  14:33:33
$a = 1;$b = $a;
$a 和 $b 是不是一样



swoole群上海求加入-张振亚-swoole-2345  14:38:38
php class 有一些魔术函数你知道吗
回忆未来-向东-Jàck  14:39:43
知道
__set __get
swoole群上海求加入-张振亚-swoole-2345  14:43:20
on方法只是注册一些事件
比如close。


swoole群上海求加入-张振亚-swoole-2345  14:44:28
swoole 连接关闭的时候 执行类似于  call_user_func($callable, $this, $data);
第一个参数是方法名,我们上面的写法是匿名函数
第二个参数就是对象本身,也就是swoole_client 对象
swoole群上海求加入-张振亚-swoole-2345  14:45:50
也就是在执行onClose 回调,把本身的client对象传递过来了
你知道他们是同一个对象就行了
背景:对于swoole这样的Server想灵活的实现不同端口间(tcp/websocket/httpd) 的通讯,用swoole自己的table可能更能灵活处理一些逻辑fd的发送,这块简单,不支持搜索,能基本上满足简单业务需求,当然更复杂可以用Redis/Mysql,显然这个是针对物联网的,因为这个表大小由自己定,多了就没法再写入了。

使用备注:只有在swoole_server->start()之前创建的table对象才能在子进程中使用
swoole_table构造方法中指定了最大容量,一旦超过此数据容量将无法分配内存导致set操作失败。所以使用swoole_table之前一定要规划好数据容量
第二:set/get/del 是自带行锁,所以不需要调用lock加锁,这点好处不言自明。


[root@a ~]# telnet 127.0.0.1 9501
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Connection closed by foreign host.
[root@a ~]# telnet 127.0.0.1 9501
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
jack


php swoole_table.php
php swoole_table.php  
Array
(
    [fd] => 1
    [from_id] => 0
    [data] => 0
)
Array
(
    [fd] => 56
    [from_id] => 777
    [data] => 8899
)

其它常用到的相关操作函数:
swoole_table->exist
检查table中是否存在某一个key。

bool swoole_table->exist(string $key);
存在返回true
不存在返回false

swoole_table->del
删除数据

bool swoole_table->del(string $key)
$key对应的数据不存在,将返回false
成功删除返回true

参考:
http://wiki.swoole.com/wiki/page/292.html
http://wiki.swoole.com/wiki/page/259.html
https://my.oschina.net/matyhtf/blog/806196

生活中的事情没有啥不能通过一吨饭解决问题的,
代码世界没有啥是不能通过测试显问题或惊喜的。 ---swoole顾问向东
Swoole之多端口及多端口复合协议侦听:
https://www.zybuluo.com/Lancelot2014/note/560093
https://www.zybuluo.com/Lancelot2014/note/560299


固定包头协议,EOF协议:
https://www.zybuluo.com/Lancelot2014/note/418670

https://www.zybuluo.com/Lancelot2014/note/555800
https://www.zybuluo.com/Lancelot2014/note/556018
基于SWOOLE的分布式SOCKET消息服务器架构
阅读全文
分页: 1/1 第一页 1 最后页 [ 显示模式: 摘要 | 列表 ]