Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize token Bucket current limit in PHP

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to achieve token bucket current limit PHP, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The method for PHP to realize token bucket current limit is as follows: 1. It is provided with a token bucket in which tokens are stored; 2. Each visit takes a token from the bucket; 3. According to the actual situation, several tokens are put in or directly fill up the token bucket at regular intervals.

This article operating environment: Windows7 system, PHP7.1, Dell G3 computer.

Php uses token bucket algorithm to realize flow control based on redis

Whenever the domestic long holidays or important festivals, the domestic scenic spots or subways will be crowded, resulting in excessive load, some will adopt flow restrictions to limit the number of people entering, when the number of people in the area is reduced to a certain value, and then allowed to enter.

For example:

The maximum number of people allowed in the area is M

The current number of people in the area is N

When N = M, you are not allowed to enter each person.

For each person who leaves, NMUI 1, when N < M, can be allowed to enter.

During the operation of the system, if some activities are encountered, the number of visitors will implode in an instant, resulting in an instant surge of pressure on the server and overload of the system.

Of course, we can increase the server to share the pressure, first of all, increase the server also needs a certain amount of time to configure, and because of a certain activity to increase the server, these server resources will be wasted after the end of the activity.

Therefore, according to the type of business, we can first use current restriction to reduce the pressure on the server.

Unlike the current limit in scenic spots, the time from access to the end of the system is very short, so we only need to know the average time of each visit and set the maximum number of simultaneous visitors.

Token bucket algorithm

1. First of all, there is a token bucket in which tokens are stored. At first, the tokens in the token bucket are full (the number of tokens in the bucket can be set according to the server).

two。 Each access takes a token from the bucket, and when the token in the bucket is 0, no more access is allowed.

3. Every once in a while, put in the token, at most, make the token in the bucket full. (according to the actual situation, several tokens can be put in at regular intervals, or token buckets can be filled directly)

We can use redis queues as token bucket containers, using lPush (queuing) and rPop (dequeuing) to achieve token addition and consumption.

TrafficShaper.class.php

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report