Home >Backend Development >PHP Tutorial >PHP uses token bucket algorithm to implement flow control based on redis

PHP uses token bucket algorithm to implement flow control based on redis

jacklove
jackloveOriginal
2018-06-15 17:22:533801browse

This article introduces PHP based on redis and uses the token bucket algorithm to control access traffic. It provides a complete algorithm description and demonstration examples for everyone to learn and use.
Whenever there are long domestic holidays or important festivals, domestic scenic spots or subways will be crowded with people, resulting in excessive load. In some cases, flow limiting measures will be adopted to limit the number of people entering. When the number of people in the area is reduced to a certain value, no one will be allowed to enter again. Enter.

For example:
The maximum number of people allowed in the area is M
The current number of people in the area is N
Every entry One person, N 1, when N = M, is not allowed to enter
Every time a person leaves, N-1, when When N 359e6060cbb9c4e03a89407cad574902 'localhost',    'port' => 6379,    'index' => 0,    'auth' => '',    'timeout' => 1,    'reserved' => NULL,    'retry_interval' => 100, );// 令牌桶容器$queue = 'mycontainer';// 最大令牌数$max = 10;// 每次时间间隔加入的令牌数$token_num = 3;// 时间间隔,最好是能被60整除的数,保证覆盖每一分钟内所有的时间$time_step = 1;// 执行次数$exec_num = (int)(60/$time_step);// 创建TrafficShaper对象$oTrafficShaper = new TrafficShaper($config, $queue, $max);for($i=0; $iac5d8bbec8fb1aae84ade36d7c132775add($token_num);    echo '['.date('Y-m-d H:i:s').'] add token num:'.$add_num.PHP_EOL;     sleep($time_step); }?>

The simulation consumption program is as follows, consuming 2-8 tokens per second.

b0aa8dbae4af6175c5b1872dc35aed72 'localhost',    'port' => 6379,    'index' => 0,    'auth' => '',    'timeout' => 1,    'reserved' => NULL,    'retry_interval' => 100,
);// 令牌桶容器$queue = 'mycontainer';// 最大令牌数$max = 10;// 每次时间间隔随机消耗的令牌数量范围$consume_token_range = array(2, 8);// 时间间隔$time_step = 1;// 创建TrafficShaper对象$oTrafficShaper = new TrafficShaper($config, $queue, $max);// 重设令牌桶,填满令牌$oTrafficShaper->reset();// 执行令牌消耗while(true){    $consume_num = mt_rand($consume_token_range[0], $consume_token_range[1]);    for($i=0; $idd4e44fbcd7068196de2d6c8cad7e133get();        echo '['.date('Y-m-d H:i:s').'] consume token:'.($status? 'true' : 'false').PHP_EOL;
    }
    sleep($time_step);
}?>

Demonstration

Set a scheduled task and execute it once every minute

* * * * * php /程序的路径/cron_add.php >> /tmp/cron_add.log

Execution simulation consumption

php consume_demo.php

Execution results :

[2018-02-23 11:42:57] consume token:true[2018-02-23 11:42:57] consume token:true[2018-02-23 11:42:57] consume token:true[2018-02-23 11:42:57] consume token:true[2018-02-23 11:42:57] consume token:true[2018-02-23 11:42:57] consume token:true[2018-02-23 11:42:57] consume token:true[2018-02-23 11:42:58] consume token:true[2018-02-23 11:42:58] consume token:true[2018-02-23 11:42:58] consume token:true[2018-02-23 11:42:58] consume token:true[2018-02-23 11:42:58] consume token:true[2018-02-23 11:42:58] consume token:true[2018-02-23 11:42:58] consume token:false[2018-02-23 11:42:59] consume token:true[2018-02-23 11:42:59] consume token:true[2018-02-23 11:42:59] consume token:true[2018-02-23 11:42:59] consume token:false[2018-02-23 11:42:59] consume token:false[2018-02-23 11:42:59] consume token:false[2018-02-23 11:42:59] consume token:false[2018-02-23 11:43:00] consume token:true[2018-02-23 11:43:00] consume token:true[2018-02-23 11:43:00] consume token:true[2018-02-23 11:43:00] consume token:false[2018-02-23 11:43:00] consume token:false

Because the token bucket is full at the beginning (the maximum number of tokens is 10), tokens can be obtained in the first 10 times. After 10 times, the tokens consumed will be greater than the joining token. When the number of cards is exceeded, access is restricted.


This article explains how PHP uses the token bucket algorithm to implement traffic control based on redis. For more related content, please pay attention to the PHP Chinese website.

Related recommendations:

Redis master-slave synchronization, Relevant operations for read-write separation settings


##Introducing the method of MySQL to rebuild table partitions and retain data


PHP generates relevant content of the unique RequestID class

The above is the detailed content of PHP uses token bucket algorithm to implement flow control based on redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn