swoole入门abc

1. 入门abc

1.1 github帐号添加

  • 第一步依然是配置git用户名和邮箱
git config user.name "用户名"
git config user.email "邮箱"
  • 生成ssh key时同时指定保存的文件名
ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C "email"
  • 新增并配置config文件php

    touch ~/.ssh/confightml

在config文件里添加以下内容(User表示你的用户名)laravel

Host *.github.com
IdentityFile ~/.ssh/id_rsa.github
User test
  • 上传key到github
pbcopy < ~/.ssh/id_rsa.github.pub
  eval "$(ssh-agent -s)"
  ssh-add ~/.ssh/id_rsa.github
  • 测试ssh key是否配置成功git

    ssh -T git@github.comgithub

  • 添加文件算法

git remote add origin git@github.com:zhuanxuhit/laravel-doc.git
git push -u origin master

1.2 case:Rate Limit

以rate limit为例来使用swoole开发。json

1.2.1 最简单的隔离算法

思想很简单,计算相邻两次请求之间的间隔,当速率大于Rate的时候,就拒绝请求。服务器

技能分析:swoole

  1. swoole的swoole_http_server功能,监听端口,等待客户端请求
  2. 注册回调,当请求到来的时候,处理请求

代码示例:网络

<?php namespace Swoole\Rate;

class Simple {

    protected $http;

    protected $lastTime;

    protected $rate = 0.1;
    /**
     * Simple constructor.
     *
     * @param $port
     */
    public function __construct($port)
    {
        $this->http = new \swoole_http_server('0.0.0.0',$port);
        $this->http->on('request',array($this,'onRequest'));
    }

    public function onRequest( \swoole_http_request $request, \swoole_http_response $response )
    {
        $lastTime = $this->lastTime;
        $currentTime = microtime(true);

        if(($currentTime-$lastTime)<1/$this->rate){
           // deny
        }
        else {
            $this->lastTime = $currentTime;
            // access
        }
    }

    public function start()
    {
        $this->http->start();
    }
}

$simple = new Simple(9090);
$simple->start();

分析上面的代码,咱们发现会有什么问题?若是两个请求同时进来,都读到了lastTime,没有被拒绝,可是这两个请求自己是已经请求过快了。

这个疑问产生的缘由是对于swoole的网络处理模型不是很清晰,若是请求是串行处理的,那不会有什么问题?可是若是请求是并发处理,那多个请求可能读到的是同一个时间戳,致使瞬间并发很大,出现问题。

首先来解决第一个问题:swoole是什么

swoole 是一个网络通讯框架,首要解决的问题是什么?通讯问题,以后就是高性能这个话题了,高性能主要从3个方面考虑

1) I/O调度模型:同步阻塞I/O(BIO)仍是非阻塞I/O(NIO)。

2) 序列化框架的选择:文本协议、二进制协议或压缩二进制协议。

3) 线程调度模型:串行调度仍是并行调度,锁竞争仍是无锁化算法。

swoole在IO模型上是使用异步阻塞IO实现,调度模型则是采用Reactor,简单说就是有一个线程专门负责IO操做,当关心事件发生的时候,进行回调函数处理,具体分析见下一章。

经过修改代码,使用ab test工具,我可以简单的模拟上面的讨论到的并发问题:

public function onRequest( \swoole_http_request $request, \swoole_http_response $response )
    {
        $lastTime = $this->lastTime;
        $currentTime = microtime(true);
        $pid =($this->http->worker_pid);
        if(($currentTime-$lastTime)<1/$this->rate){
            
            echo "deny worker_pid: $pid lastTime:$lastTime currentTime:$currentTime\n";
        }
        else {
            $this->lastTime = $currentTime;
           
            echo "accept worker_pid: $pid lastTime:$lastTime currentTime:$currentTime\n";
        }
    }

测试脚本

ab -n10 -c5 http://0.0.0.0:9090/

测试结果:

accept worker_pid: 45674 lastTime: currentTime:1463470993.3306
accept worker_pid: 45675 lastTime: currentTime:1463470993.331
accept worker_pid: 45671 lastTime: currentTime:1463470993.3318
accept worker_pid: 45672 lastTime: currentTime:1463470993.3322
accept worker_pid: 45673 lastTime: currentTime:1463470993.333
deny worker_pid: 45674 lastTime:1463470993.3306 currentTime:1463470993.3344
deny worker_pid: 45673 lastTime:1463470993.333 currentTime:1463470993.3348
deny worker_pid: 45675 lastTime:1463470993.331 currentTime:1463470993.3351
deny worker_pid: 45671 lastTime:1463470993.3318 currentTime:1463470993.3352
deny worker_pid: 45672 lastTime:1463470993.3322 currentTime:1463470993.3354

能够很显然的看到,并发请求来的时候,读到的lastTime都是未设置过的

模拟出并发问题后,这个关于swoole中有进程模型也很好测试出来:

public function serverInfoDebug()
    {
        return json_encode(
            [
                'master_id' => $this->http->master_pid,//返回当前服务器主进程的PID。
                'manager_pid' => $this->http->manager_pid,//返回当前服务器管理进程的PID。
                'worker_id' => $this->http->worker_id,//获得当前Worker进程的编号,包括Task进程
                'worker_pid' => $this->http->worker_pid,//获得当前Worker进程的操做系统进程ID。与posix_getpid()的返回值相同。
            ]
        );
    }

启动成功后会建立worker_num+2个进程。主进程+Manager进程+worker_num个Worker进程。

完整地址:
https://github.com/zhuanxuhit/php-recipes/blob/master/app/SwooleAbc/Rate/Simple.php

那回到上面的问题,怎么解决并发问题呢?在C++等语言中,很好解决这个问题,使用锁,互斥访问。

写到这的时候,发现个问题,发如今回调中,每一个worker在处理onRequest函数的时候,this都是一个新的,为何呢?由于worker进程都是由Manager进程fork()出来的,天然数据是新的一份了。

如今要解决的问题变为:如何在swoole中实现多个进程的数据共享功能

能够看到https://github.com/swoole/swoole-src/issues/242
其中建议,能够经过使用swoole提供的swoole_table来作,代码以下:

public function onRequest( \swoole_http_request $request, \swoole_http_response $response )
    {
        $currentTime = microtime(true);
        $pid =($this->http->worker_pid);
        $this->table->lock();
        $lastTime = $this->table->get( 'lastTime' );
        $lastTime = $lastTime['lastTime'];
        if(($currentTime-$lastTime)<1/$this->rate){
            $this->table->unlock();
            //deny
        }
        else {
            $this->table->set( 'lastTime', [ 'lastTime' => $currentTime] );
            $this->table->unlock();
            // access
        }
    }

再次测试,可以发现很好的知足了要求。

1.2.2 最清晰的吊桶算法

隔离算法的问题很明显,使用ab -n2 -c2 http://0.0.0.0:9090/,同时并发2个请求就被拒绝了,所以只计算了相邻两次的间隔,而没有关注1s内的请求,所以一个改进思路就是以s为key,记录时间戳下来。下面是实现

public function onRequest( \swoole_http_request $request, \swoole_http_response $response )
    {
        $currentTime = time();
        $this->table->lock();
        $count = $this->table->get( (string)$currentTime );
//        (new Dumper)->dump($count);
        if($count){
            $count = $count['count'];
        }
        else {
            $count = 0;
        }

        if($count >$this->rate){
            $this->table->unlock();
            // deny
        }
        else {
            $this->table->set( (string)$currentTime, [ 'count' => $count + 1] );
            $this->table->unlock();
            //accept
        }
    }

因为每s的数据都记录了,没有过时,致使数据不断增加,有问题,并且因为1s是分割的,不是连续的,必然会形成最开始脑图中的bad case。

因而就有了下面的第三个方法:最精确的队列算法

1.2.3 最精确的队列算法

思路上就是将请求入队,记录请求的时间,这样就能够判断任意连续的多个请求,其是不是在1s以内了

首先看下这个算法思路:假设rate=5,当请求到来的时候,获得当前请求编号,而后减5获得index,而后判断两次请求之间的时间间隔,是否大于1s,若是大于则accept,不然deny

n-5 n-4 n-3 n-2 n-1 n n+1 n+2 n+3 n+4 n+5

如今来的请求是n,则去n-5,为何是减5,所以rate是5,则当qps为6的时候就deny,所以须要判断n-5到n这6个请求的间隔!

算法有了,下面就是在swoole中怎么实现队列的问题了,这个队列还须要在进程间共享。

咱们可使用swoole_table来实现,另外还须要一个计数器,给每一个请求编号,实现以下:

// 每一个请求过来后的是否判断经过,这个操做必需要单点,串行,因此也就是说必需要加速
        $this->table->lock();
        $count = $this->counter->add(1);
        $bool = true;
        $currentCount = $count + 1;
        $previousCount = $count - $this->rate;
        if($currentCount<=$this->rate){
            $this->table->set( $count, [ 'timeStamp' => $currentTime ] );
            $this->table->unlock();
        }
        else {
            $previousTime = $this->table->get( $previousCount );
            if ( $currentTime - $previousTime['timeStamp'] > 1 ) {
                $this->table->set( $currentCount, [ 'timeStamp' => $currentTime ] );
                $this->table->unlock();
            } else {
                // 去除 deny
                $bool = false;
                $this->counter->sub( 1 );
                $this->table->unlock();
            }
        }

上面有一个核心点,以前一直没有注意到的:对全部请求的处理都是须要互斥的,便是一个单点,处理完后才能转发给真正的业务逻辑进行处理。

所以能够将Rate的逻辑抽离出来,做为一个服务提供,这个之后讲服务化的时候再作的。

1.2.4 最传统的令牌算法

令牌算法相似小米抢购,放量出来必定的票,当人想进来抢的时候,必须有F码才能进行抢购,而票的放出是按必定速率产生的。

上面算法实现时,须要用到swoole的定时器功能,须要在OnWorkerStart的回调的时候使用

public function onWorkerStart(\swoole_server $server, $worker_id)
    {
        if($worker_id ==0){
            $server->tick( 1000/$this->rate, [$this,'addTicket'] );
        }
    }

而请求到来的时候,就是经过getTicket获取资格,没票的时候,直接返回false

完整的github地址:
https://github.com/zhuanxuhit/php-recipes/tree/master/app/SwooleAbc/Rate
Rate limit的介绍:http://www.bittiger.io/classpage/hfjPKuZaLxPLyL5iN
gitbook地址:
https://zhuanxuhit.gitbooks.io/swoole-abc/content/chapter1.html

原文地址:https://www.jianshu.com/p/7c16cb61b59d

相关文章
相关标签/搜索