Data flow limiting method and device, electronic equipment and storage medium
Through the combination of Lua scripts and Redis databases, the current limiting strategy is dynamically adjusted, which solves the problem that traditional QPS current limiting methods cannot adapt to requested load changes, improves system performance and stability, and achieves flexible response to system loads.
Patent Information
- Application Number
- CN202410948021.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-16
- Publication Date
- 2024-11-15
AI Technical Summary
The traditional QPS current limiting method cannot dynamically adapt to request load changes, resulting in performance bottlenecks, lack of flexibility and security risks, and cannot effectively protect the system from malicious traffic.
Through the combination of Lua script and Redis database, the added value and residual amount of the amount of business requests can be calculated, the current limiting strategy is dynamically adjusted, and the number of requests is counted using an ordered set to achieve flexible current limiting control, and the request is made when necessary Current limit or release processing volume.
It improves the overall performance and stability of the system, can flexibly respond to different business scenarios and requested loads, respond to system load changes in a timely manner, and lowers the threshold for technical implementation.
Smart Images

Figure CN118964325A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computers, and in particular to a data flow limiting method, device, electronic equipment and storage medium. Background Art
[0002] In today's digital age, the rapid development of computer science and network technology has brought about the popularity of large-scale distributed systems and high-concurrency network services. Various application scenarios such as Web services, API interfaces, and distributed databases have put forward higher requirements on system performance and stability. One of the common problems is how to ensure that the system can effectively control the number of queries per second (QPS) in the face of growing request loads.
[0003] Traditional QPS limiting methods are usually based on fixed rules or thresholds and cannot dynamically adapt to changing request loads. This leads to a series of problems, including but not limited to: (1) Performance bottlenecks: Traditional QPS limiting methods may encounter performance bottlenecks when processing large-scale, high-concurrency requests, affecting the system's response time and throughput; (2) Lack of flexibility: Fixed QPS limiting rules cannot flexibly respond to changes in different business scenarios and system loads, causing the system to over-restrict or relax query restrictions at certain times; (3) Security risks: When faced with malicious attacks or abnormal requests, traditional QPS limiting methods may not be able to adjust quickly to protect the system from malicious traffic.
[0004] Therefore, based on the above problems, it is necessary to develop a new data flow limiting technology to improve the elasticity, stability and performance of the system. Summary of the invention
[0005] In view of the above reasons, it is necessary to provide a data flow limiting method, device, electronic device and storage medium to improve the overall performance of the system.
[0006] To achieve the above object, the present invention provides a data flow limiting method, which comprises the following steps:
[0007] S1. Receive a service request through a service request interface, and obtain first service data and second service data through a preset Lua script of a current limiter;
[0008] S2. Calculate the increase in the processable amount of the current business request by using the first business data and the second business data, calculate the cumulative sum of the increase in the processable amount of the current business request and the remaining processable amount of the previous business request, compare the maximum processable amount of the business request in the first business data with the cumulative sum, take the minimum value of the two as the processable amount of the current business request, and then determine whether the minimum value is greater than or equal to 0;
[0009] S3. If not, limit the flow of the business request. After the business request sleeps for a preset time, return to steps S1 and S2 to determine whether to limit the flow of the business request through the flow limiter; if so, reduce the processable amount of the existing business request by 1, and request the relevant interface according to the business request to implement the business request.
[0010] Optionally, receiving a service request through a service request interface includes:
[0011] The current limiter is added through a preset AOP aspect, the business request is received through the current limiter, and the business request is stored in an ordered set in the current limiter.
[0012] Optionally, the flow limiter includes a Redis database and a Lua script, and uses the Lua script to check and logically control the first business data and the second business data on the Redis database server side to limit the flow of business requests.
[0013] Optionally, the rate limiter uses the ordered set of the Redis database to store the timestamp of each business request, and counts the number of business requests within a preset time period through the orderliness of the ordered set.
[0014] Optionally, counting the number of service requests within a preset time period based on the orderliness of the ordered set includes:
[0015] The ordered set stores the unique identifier in each business request and sorts them according to the Long value of the timestamp. The number of business requests within a preset time window is counted as the maximum processing capacity of the business requests through the range function in the ordered set, and the data flow limiting method is executed on the business requests in the ordered set in sequence based on the sorting.
[0016] Optionally, the method further comprises:
[0017] Presetting an expiration time for each service request in the ordered set, and judging whether the service request is expired by using the timestamp of the ordered set and the expiration time;
[0018] If the service request expires, it is deleted from the ordered set, and an expiration notification is sent to the client corresponding to the service request.
[0019] Optionally, the time window is a dynamic time window, and the time window sets the maximum processing volume of the current business request according to the limit processing volume of the business request and the number of business requests in the ordered set.
[0020] The present invention also provides a data current limiting device, the data current limiting device comprising:
[0021] An acquisition module, used to receive a service request through a service request interface, and acquire first service data and second service data through a preset Lua script of a current limiter;
[0022] a judgment module, configured to calculate an increase in the processable amount of the current business request through the first business data and the second business data, calculate a cumulative sum of the increase in the processable amount of the current business request and the remaining processable amount of the previous business request, compare the maximum processable amount of the business request in the first business data with the cumulative sum, take the minimum value of the two as the processable amount of the current business request, and then judge whether the minimum value is greater than or equal to 0;
[0023] An execution module is used to limit the flow of the business request if the minimum value is not greater than or equal to 0. After the business request sleeps for a preset time, it returns to steps S1 and S2 to determine whether to limit the flow of the business request through the limiter; if the minimum value is greater than or equal to 0, the processable amount of the existing business request is reduced by 1, and the relevant interface is requested according to the business request to implement the business request.
[0024] The present invention also provides an electronic device, which includes a memory and a processor, wherein the memory includes a data current limiting program, and when the data current limiting program is executed by the processor, any step in the data current limiting method described above is implemented.
[0025] In addition, to achieve the above-mentioned purpose, the present invention also provides a computer-readable storage medium, wherein the computer-readable storage medium includes a data flow limiting program, and when the data flow limiting program is executed by a processor, any step in the data flow limiting method as described above is implemented.
[0026] The present invention relates to the field of computers, and provides a data flow limiting method, which includes: obtaining the existing business request processable amount based on the maximum processing amount of business requests obtained by a flow limiter, the time of this query of the number of business requests, the time of the last query of the number of business requests, the increase speed of the business request processable amount, and the remaining processable amount of the last business request, and judging whether the existing business request processable amount is greater than or equal to 0; if not, limiting the flow of the business request, after the business request sleeps for a preset time, returning to steps S1 and S2 to judge whether to limit the flow of the business request through the flow limiter; if so, reducing the existing business request processable amount by 1, and then requesting related interfaces according to the business request to realize the current business request. The present invention makes flow limiting judgments through multiple data, implements different strategies according to the judgment situation, and improves the overall performance of the system. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] Figure 1 A schematic diagram of the internal structure of an electronic device implementing a data current limiting method provided by an embodiment of the present invention;
[0028] Figure 2 A schematic diagram of the functional modules of a data current limiting device provided by an embodiment of the present invention;
[0029] Figure 3 A schematic diagram of a flow chart of a data flow limiting method provided by an embodiment of the present invention;
[0030] The realization of the purpose, functional features and advantages of the present invention will be further explained in conjunction with embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0031] The principle and spirit of the present invention will be described below with reference to several specific embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.
[0032] See also Figure 1 FIG. 1 is a schematic diagram of an optional hardware architecture of an electronic device proposed in the present invention. In this embodiment, the electronic device 10 may include, but is not limited to, a memory 11, a processor 12, and a network interface 13 that can be interconnected via a communication bus 14. It should be noted that Figure 1 The component structure of the electronic device of one embodiment of the present invention is only exemplified, and those skilled in the art can understand that: Figure 1 The component structure shown does not constitute a structural limitation on the electronic device 10. In other embodiments of the present invention, the electronic device 10 may include fewer or more components than shown in the figure, or fewer or more combinations of specific components than shown in the figure, or the arrangement structure of the components of the electronic device 10 may be different from the arrangement structure shown in the figure, which will not be elaborated here.
[0033] Among them, the memory 11 includes at least one type of computer-readable storage medium, and the computer-readable storage medium includes flash memory, hard disk, multimedia card, card-type memory (for example, SD or DX memory, etc.), random access memory (RAM), static random access memory (SRAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), programmable read-only memory (PROM), magnetic memory, disk, optical disk, etc. In some embodiments, the memory 11 can be an internal storage unit of the electronic device 10, such as a hard disk or memory of the electronic device 10. In other embodiments, the memory 11 can also be an outsourced storage device of the electronic device 10, such as a plug-in hard disk equipped on the electronic device 10, a smart memory card (SmartMedia Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), etc. Of course, the memory 11 can also include both the internal storage unit of the electronic device 10 and its outsourced storage device. In this embodiment, the memory 11 is generally used to store the operating system and various application software installed on the electronic device 10, such as a data flow limiting program, etc. In addition, the memory 11 can also be used to temporarily store various types of data that have been output or are to be output.
[0034] In some embodiments, the processor 12 may be a central processing unit (CPU), a controller, a microcontroller, a microprocessor, or other data processing chips. The processor 12 is generally used to control the overall operation of the electronic device 10. In this embodiment, the processor 12 is used to run the program code stored in the memory 11 or process data, such as running a data current limiting program.
[0035] The network interface 13 may include a wireless network interface or a wired network interface. The network interface 13 is generally used to establish a communication connection between the electronic device 10 and other electronic devices.
[0036] The communication bus 14 is used to realize the communication connection between the components 11-13.
[0037] Optionally, the electronic device 10 may further include a user interface ( Figure 1 (not shown in the figure), the user interface may include a display, an input unit such as a keyboard, wherein the user interface may also include a standard wired interface, a wireless interface, etc.
[0038] Optionally, in some embodiments, the display may be an LED display, a liquid crystal display, a touch-sensitive liquid crystal display, an OLED touch device, etc. Further, the display may also be referred to as a display screen or a display unit, which is used to display information processed in the electronic device 10 and to display a visual user interface.
[0039] Optionally, in some embodiments, the electronic device 10 may further include an audio unit (audio unit Figure 1 (not shown in the figure), the audio unit can convert the received or stored audio data into an audio signal when the electronic device 10 is in a call signal receiving mode, a talk mode, a recording mode, a voice recognition mode, a broadcast receiving mode, etc.; further, the electronic device 10 may also include an audio output unit, which outputs the audio signal converted by the audio unit, and the audio output unit can also provide audio output related to a specific function performed by the electronic device 10 (such as a call signal reception sound, a message reception sound, etc.), and the audio output unit may include a speaker, a buzzer, etc.
[0040] Optionally, in some embodiments, the electronic device 10 may further include an alarm unit (not shown in the figure), which may provide an output to notify the electronic device 10 of the occurrence of an event. Typical events may include call reception, message reception, key signal input, touch input, and the like. In addition to audio or video output, the alarm unit may provide output in different ways to notify the occurrence of an event. For example, the alarm unit may provide an output in the form of a vibration, and when a call, message, or some other event that may cause the electronic device 10 to enter a communication mode is received, the alarm unit may provide a tactile output (i.e., vibration) to notify the user.
[0041] In the above embodiment, the processor 12 implements the following steps when executing the data current limiting program stored in the memory 11:
[0042] S1. Receive a service request through a service request interface, and obtain first service data and second service data through a preset Lua script of a current limiter;
[0043] S2. Calculate the increase in the processable amount of the current business request by using the first business data and the second business data, calculate the cumulative sum of the increase in the processable amount of the current business request and the remaining processable amount of the previous business request, compare the maximum processable amount of the business request in the first business data with the cumulative sum, take the minimum value of the two as the processable amount of the current business request, and then determine whether the minimum value is greater than or equal to 0;
[0044] S3. If not, limit the flow of the business request. After the business request sleeps for a preset time, return to steps S1 and S2 to determine whether to limit the flow of the business request through the flow limiter; if so, reduce the processable amount of the existing business request by 1, and request the relevant interface according to the business request to implement the business request.
[0045] In one embodiment of the present invention, the flow limiter adopts a token algorithm. Each business request needs to be configured with a token to determine whether it needs flow limiting. After each business request is judged, the token corresponding to the business request will be consumed, and the total number of tokens will decrease. As the number of judgments increases, if the tokens are not replenished, the tokens will be consumed, and subsequent business requests cannot be judged normally. Therefore, it is necessary to introduce the increase rate of the processable amount of business requests, that is, the increase rate of the corresponding number of tokens, to increase the corresponding number of tokens.
[0046] In one embodiment of the present invention, the maximum processing capacity of the business request is calculated corresponding to the number of business requests in the current ordered set in the preset limiter. For example, the current limit processing capacity of business requests processed by Redis is 80,000. If the number of business requests in the current ordered set is 20,000, the time window can be set to 0.3 seconds, and the number of business requests that can be processed is 24,000 (maximum processing capacity). If the number of business requests in the current ordered set is 40,000, the time window can be set to 0.55 seconds, and the number of business requests that can be processed is 44,000, and so on, until the number of business requests in the time window reaches the limit processing capacity of the business request. This embodiment allows the time window to be adjusted dynamically, so that the system can flexibly adapt to different business scenarios and request loads, and improves the adaptability of the system to changes.
[0047] For a detailed description of the above steps, please refer to the following Figure 2 Functional module diagram of data flow limiting method and Figure 3 A description of a flow chart of a data limiting method.
[0048] Reference Figure 2 1 is a functional module diagram of an embodiment of a data current limiting device of the present invention. In this embodiment, multiple modules are stored in a memory 11 and executed by a processor 12 to complete the present invention. The module referred to in the present invention refers to a series of computer program instruction segments that can complete a specific function.
[0049] The data flow limiting device may include: an acquisition module 201 , a determination module 202 , and an execution module 203 .
[0050] The acquisition module 201 is used to receive a service request through a service request interface, and acquire first service data and second service data through a preset Lua script of a current limiter.
[0051] Specifically, the first business data includes the maximum processing volume of business requests, the time of this query on the number of business requests, and the rate of increase of the processable volume of business requests; the second business data includes the time of the last query on the number of business requests, and the remaining processable volume of the last business request.
[0052] Specifically, receiving a service request through a service request interface includes:
[0053] The current limiter is added through a preset AOP aspect, the business request is received through the current limiter, and the business request is stored in an ordered set in the current limiter.
[0054] Specifically, the current limiter includes a Redis database and a Lua script, and uses the Lua script to check and logically control the first business data and the second business data on the Redis database server side to limit the flow of business requests.
[0055] Specifically, the current limiter uses the ordered set of the Redis database to store the timestamp of each business request, and counts the number of business requests within a preset time period through the orderliness of the ordered set.
[0056] Specifically, the method further includes:
[0057] Presetting an expiration time for each service request in the ordered set, and judging whether the service request is expired by using the timestamp of the ordered set and the expiration time;
[0058] If the service request expires, it is deleted from the ordered set, and an expiration notification is sent to the client corresponding to the service request.
[0059] Specifically, the time window is a dynamic time window, and the time window sets the maximum processing volume of the current business request according to the limit processing volume of the business request and the number of business requests in the ordered set.
[0060] AOP (Aspect oriented programming) is aspect-oriented programming. AOP is a continuation of OOP (object-oriented programming). AOP is used to solve the problems of code duplication in cross-cutting logic codes, bloated code caused by mixing cross-cutting logic codes and business codes, and inconvenient maintenance. AOP proposes a horizontal extraction mechanism to separate cross-cutting logic codes from business logic codes. Without changing the original business logic, the cross-cutting logic code is enhanced, and the coupling is fundamentally decoupled to avoid duplication of cross-cutting logic codes. Cut: refers to cross-cutting logic. The original business logic code remains unchanged, and only the cross-cutting logic code can be operated, so it is oriented to cross-cutting logic. Surface: Cross-cutting logic code often affects many methods. Each method is like a point, and multiple points constitute a surface. By triggering the AOP aspect, the present invention can efficiently add a current limiter without affecting the function of the original interface, so as to achieve the effect of limiting the flow of business requests.
[0061] Redis (Remote Dictionary Server) is a remote dictionary service. It is an open source log-type, Key-Value database written in ANSI C language, supports the network, can be based on memory or persistence, and provides APIs in multiple languages. Unlike the MySQL database, Redis data is stored in memory. Its read and write speed is very fast, and it can handle more than 100,000 read and write operations per second. Therefore, Redis is widely used in caching. In addition, Redis is also often used for distributed locks. In addition, Redis supports transactions, persistence, Lua scripts, LRU driven events, and multiple cluster solutions.
[0062] Lua is a scripting language developed based on C language. Using Lua script can reduce network communication overhead, put multiple commands in the same script to run, and form atomic operations. Redis will execute the entire script as a whole, and other commands will not be inserted in the middle. The script can be stored in Redis and reused by multiple terminals. The present invention realizes efficient data inspection and logic control by executing Lua script on the Redis server side, reduces network communication overhead, improves the overall performance of the system, and uses Redis's ordered set and Lua script to realize real-time monitoring and control, ensures immediate response to system load, effectively avoids the delay problem of traditional methods, and reduces the threshold of technical implementation, so that more developers can easily integrate and apply the technology in their systems and services. Due to the use of general Redis and Lua technology stacks, the present invention has strong general applicability and can be widely used in various distributed systems and network services.
[0063] In one embodiment of the present invention, the current limit processing capacity of business requests processed by Redis is 80,000. If the number of business requests in the current ordered set is 20,000, the time window can be set to 0.3 seconds, and the number of business requests that can be processed is 24,000 (maximum processing capacity). If the number of business requests in the current ordered set is 40,000, the time window can be set to 0.55 seconds, and the number of business requests that can be processed is 44,000, and so on, until the number of business requests in the time window reaches the limit processing capacity of business requests. This embodiment allows the time window to be adjusted dynamically, so that the system can flexibly adapt to different business scenarios and request loads, and improves the adaptability of the system to changes.
[0064] The judgment module 202 is used to calculate the increase in the processable amount of the current business request through the first business data and the second business data, calculate the increase in the processable amount of the current business request and the cumulative sum of the remaining processable amount of the previous business request, compare the maximum processable amount of the business request in the first business data with the cumulative sum, take the minimum value of the two as the processable amount of the existing business request, and then judge whether the minimum value is greater than or equal to 0.
[0065] Specifically, the calculating the increased value of the processable amount of the current service request by using the first service data and the second service data includes:
[0066] The query time span value is obtained by the time of the current query of the number of business requests and the time of the last query of the number of business requests, and the increase value of the processable amount of business requests this time is calculated by the query time span value and the increase speed of the processable amount of business requests.
[0067] In one embodiment of the present invention, the calculation formula of the query time span value is: T=T1-T0, wherein T is the query time span value, T1 is the time of this query of the number of service requests, and T0 is the time of the last query of the number of service requests.
[0068] In one embodiment of the present invention, the calculation formula for the increase in the processable amount of the business requests is: H=T*V1, wherein H is the increase in the processable amount of the business requests, and V1 is the increase rate of the processable amount of the business requests.
[0069] In one embodiment of the present invention, the calculation formula of the cumulative sum is: K=H+K0, where K is the cumulative sum and K0 is the remaining processable amount of the last service request. An acquisition module is used to receive a service request through a service request interface and acquire the first service data and the second service data through a preset Lua script of a current limiter;
[0070] a judgment module, configured to calculate an increase in the processable amount of the current business request through the first business data and the second business data, calculate a cumulative sum of the increase in the processable amount of the current business request and the remaining processable amount of the previous business request, compare the maximum processable amount of the business request in the first business data with the cumulative sum, take the minimum value of the two as the processable amount of the current business request, and then judge whether the minimum value is greater than or equal to 0;
[0071] An execution module is used to limit the flow of the business request if the minimum value is not greater than or equal to 0. After the business request sleeps for a preset time, it returns to steps S1 and S2 to determine whether to limit the flow of the business request through the flow limiter; if the minimum value is greater than or equal to 0, the processable amount of the existing business request is reduced by 1, and the relevant interface of the business request is requested according to the business request. In one embodiment of the present invention, for a business request, the maximum processable amount of business requests (80,000) is obtained through a Lua script, the time for querying the number of business requests this time (2 seconds) and the time for the last query of the number of business requests (1 second), and the increase in the processable amount of business requests The query time span value is 1 second, and the query time span is multiplied by the increase speed of the business request processable volume (80,000). The maximum business request processable volume is compared with the number obtained by adding the increase value of the business request processable volume and the remaining processable volume of the last business request. If the two are equal, a value is randomly selected as the current business request processable volume. If the value (80,000) is greater than or equal to 0, the current business request is not limited, and the current business request processable volume is 79,999. In layman's terms, the number of business requests that can be processed in one second is limited (the maximum business request processable volume is 80,000). If 32,000 business requests are processed in 0.4 seconds of this second, the remaining 0.6 seconds of business request processable volume is 48,000. When there are 81,000 business requests in 1 second, 1,000 business requests are limited. In the next second, the existing processable volume of business requests may change according to the increasing speed of the processable volume of business requests.
[0072] In one embodiment of the present invention, the maximum processing volume of business requests and the speed at which business requests increase can be set through Lua scripts.
[0073] Execution module 203 is used to limit the flow of the business request if the minimum value is not greater than or equal to 0. After the business request sleeps for a preset time, it returns to steps S1 and S2 to determine whether to limit the flow of the business request through the limiter; if the minimum value is greater than or equal to 0, the processable amount of the existing business request is reduced by 1, and the relevant interface is requested according to the business request to implement the business request.
[0074] Specifically, counting the number of service requests within a preset time period by using the orderliness of the ordered set includes:
[0075] The ordered set stores the unique identifier in each business request and sorts them according to the Long value of the timestamp. The number of business requests within a preset time window is counted as the maximum processing capacity of the business requests through the range function in the ordered set, and the data flow limiting method is executed on the business requests in the ordered set in sequence based on the sorting.
[0076] Specifically, when the sleep times of the service request are greater than a preset number, a preset rule is executed on the service request.
[0077] In one embodiment of the present invention, a Redis ordered set (ZSET) is used to store the timestamp of each business request. The orderliness of ZSET can be used to conveniently count the number of queries within a time window. ZSET stores the request serial number (a unique identifier for each business request) and the timestamp. Each request stores both of these contents. The ZSET data structure can support sorting by the Long value of the timestamp, and the range function in ZSET can be used to count the number of business requests within the time window. Redis Zrange can return members (business requests) within a specified interval in ZSET.
[0078] In one embodiment of the present invention, the current limiter is set as follows: when a service request sleeps for 3 times, the service request is considered expired, and an expiration notification "Your request has expired, please submit again!" is sent to the client.
[0079] In one embodiment of the present invention, the preset sleep time of the service request is 1 millisecond.
[0080] In one embodiment of the present invention, the current limiter has a preset rule: when a service request sleeps for 5 times, the relevant interface will be requested according to the service request to implement the service request.
[0081] The present invention relates to the field of computers, and provides a data flow limiting method, which includes: obtaining the existing business request processable amount based on the maximum processing amount of business requests obtained by a flow limiter, the time of this query of the number of business requests, the time of the last query of the number of business requests, the increase speed of the business request processable amount, and the remaining processable amount of the last business request, and judging whether the existing business request processable amount is greater than or equal to 0; if not, limiting the flow of the business request, after the business request sleeps for a preset time, returning to steps S1 and S2 to judge whether to limit the flow of the business request through the flow limiter; if so, reducing the existing business request processable amount by 1, and then requesting related interfaces according to the business request to realize the current business request. The present invention makes flow limiting judgments through multiple data, implements different strategies according to the judgment situation, and improves the overall performance of the system.
[0082] Reference Figure 3 FIG. 1 is a flow chart of an embodiment of a data flow limiting method of the present invention. In this embodiment, the processor 12 of the electronic device 10 executes the data flow limiting program in the memory 11 to implement the following steps of the data flow limiting method:
[0083] Step S301: Receive a service request through a service request interface, and obtain first service data and second service data through a preset Lua script of a current limiter.
[0084] Specifically, the first business data includes the maximum processing volume of business requests, the time of this query on the number of business requests, and the rate of increase of the processable volume of business requests; the second business data includes the time of the last query on the number of business requests, and the remaining processable volume of the last business request.
[0085] Specifically, receiving a service request through a service request interface includes:
[0086] The current limiter is added through a preset AOP aspect, the business request is received through the current limiter, and the business request is stored in an ordered set in the current limiter.
[0087] Specifically, the current limiter includes a Redis database and a Lua script, and uses the Lua script to check and logically control the first business data and the second business data on the Redis database server side to limit the flow of business requests.
[0088] Specifically, the current limiter uses the ordered set of the Redis database to store the timestamp of each business request, and counts the number of business requests within a preset time period through the orderliness of the ordered set.
[0089] Specifically, the method further includes:
[0090] Presetting an expiration time for each service request in the ordered set, and judging whether the service request is expired by using the timestamp of the ordered set and the expiration time;
[0091] If the service request expires, it is deleted from the ordered set, and an expiration notification is sent to the client corresponding to the service request.
[0092] Specifically, the time window is a dynamic time window, and the time window sets the maximum processing volume of the current business request according to the limit processing volume of the business request and the number of business requests in the ordered set.
[0093] AOP (Aspect oriented programming) is aspect-oriented programming. AOP is a continuation of OOP (object-oriented programming). AOP is used to solve the problems of code duplication in cross-cutting logic codes, bloated code caused by mixing cross-cutting logic codes and business codes, and inconvenient maintenance. AOP proposes a horizontal extraction mechanism to separate cross-cutting logic codes from business logic codes. Without changing the original business logic, the cross-cutting logic code is enhanced, and the coupling is fundamentally decoupled to avoid duplication of cross-cutting logic codes. Cut: refers to cross-cutting logic. The original business logic code remains unchanged, and only the cross-cutting logic code can be operated, so it is oriented to cross-cutting logic. Surface: Cross-cutting logic code often affects many methods. Each method is like a point, and multiple points constitute a surface. By triggering the AOP aspect, the present invention can efficiently add a current limiter without affecting the function of the original interface, so as to achieve the effect of limiting the flow of business requests.
[0094] Redis (Remote Dictionary Server) is a remote dictionary service. It is an open source log-type, Key-Value database written in ANSI C language, supports the network, can be based on memory or persistence, and provides APIs in multiple languages. Unlike the MySQL database, Redis data is stored in memory. Its read and write speed is very fast, and it can handle more than 100,000 read and write operations per second. Therefore, Redis is widely used in caching. In addition, Redis is also often used for distributed locks. In addition, Redis supports transactions, persistence, Lua scripts, LRU driven events, and multiple cluster solutions.
[0095] Lua is a scripting language developed based on C language. Using Lua script can reduce network communication overhead, put multiple commands in the same script to run, and form atomic operations. Redis will execute the entire script as a whole, and other commands will not be inserted in the middle. The script can be stored in Redis and reused by multiple terminals. The present invention realizes efficient data inspection and logic control by executing Lua script on the Redis server side, reduces network communication overhead, improves the overall performance of the system, and uses Redis's ordered set and Lua script to realize real-time monitoring and control, ensures immediate response to system load, effectively avoids the delay problem of traditional methods, and reduces the threshold of technical implementation, so that more developers can easily integrate and apply the technology in their systems and services. Due to the use of general Redis and Lua technology stacks, the present invention has strong general applicability and can be widely used in various distributed systems and network services.
[0096] In one embodiment of the present invention, the current limit processing capacity of business requests processed by Redis is 80,000. If the number of business requests in the current ordered set is 20,000, the time window can be set to 0.3 seconds, and the number of business requests that can be processed is 24,000 (maximum processing capacity). If the number of business requests in the current ordered set is 40,000, the time window can be set to 0.55 seconds, and the number of business requests that can be processed is 44,000, and so on, until the number of business requests in the time window reaches the limit processing capacity of business requests. This embodiment allows the time window to be adjusted dynamically, so that the system can flexibly adapt to different business scenarios and request loads, and improves the adaptability of the system to changes.
[0097] Step S302: calculate the increase in the processable amount of this business request through the first business data and the second business data, calculate the cumulative sum of the increase in the processable amount of this business request and the remaining processable amount of the previous business request, compare the maximum processable amount of the business request in the first business data with the cumulative sum, take the minimum value of the two as the processable amount of the existing business request, and then determine whether the minimum value is greater than or equal to 0.
[0098] Specifically, the calculating the increased value of the processable amount of the current service request by using the first service data and the second service data includes:
[0099] The query time span value is obtained by the time of the current query of the number of business requests and the time of the last query of the number of business requests, and the increase value of the processable amount of business requests this time is calculated by the query time span value and the increase speed of the processable amount of business requests.
[0100] In one embodiment of the present invention, the calculation formula of the query time span value is: T=T1-T0, wherein T is the query time span value, T1 is the time of this query of the number of service requests, and T0 is the time of the last query of the number of service requests.
[0101] In one embodiment of the present invention, the calculation formula for the increase in the processable amount of the business requests is: H=T*V1, wherein H is the increase in the processable amount of the business requests, and V1 is the increase rate of the processable amount of the business requests.
[0102] In one embodiment of the present invention, the calculation formula of the cumulative sum is: K=H+K0, wherein K is the cumulative sum, and K0 is the remaining processable amount of the last service request.
[0103] In one embodiment of the present invention, for a business request, the maximum processing capacity of business requests (80,000) is obtained through a Lua script, the time for this query of the number of business requests (2 seconds) and the time for the last query of the number of business requests (1 second), the increase rate of the processable capacity of business requests (80,000 / 1 second) and the remaining processable capacity of the last business request (0) are obtained, the two times are subtracted to obtain a query time span value of 1 second, the query time span is multiplied by the increase rate of the processable capacity of business requests to obtain the increase value of the processable capacity of this business request (80,000), the maximum processing capacity of business requests is compared with the number obtained by adding the increase value of the processable capacity of this business request and the remaining processable capacity of the last business request, if the two are equal, then a value is arbitrarily selected as the processable capacity of the existing business request, and the value (80,000) is greater than or equal to 0, then there is no flow limit on the current business request, and the processable capacity of the existing business request is 79,999. Generally speaking, the number of business requests that can be processed in one second is limited (the maximum number of business requests that can be processed is 80,000). For example, if 32,000 business requests are processed in 0.4 seconds of this second, the number of business requests that can be processed in the remaining 0.6 seconds is 48,000. If there are 81,000 business requests in one second, 1,000 business requests will be limited. In the next second, the number of existing business requests that can be processed can change according to the speed at which the number of business requests that can be processed increases.
[0104] In one embodiment of the present invention, the maximum processing volume of business requests and the speed at which business requests increase can be set through Lua scripts.
[0105] Step S303: If not, limit the flow of the business request. After the business request sleeps for a preset time, return to steps S1 and S2 to determine whether to limit the flow of the business request through the flow limiter; if so, reduce the processable amount of the existing business request by 1, and request the relevant interface according to the business request to implement the business request.
[0106] Specifically, counting the number of service requests within a preset time period by using the orderliness of the ordered set includes:
[0107] The ordered set stores the unique identifier in each business request and sorts them according to the Long value of the timestamp. The number of business requests within a preset time window is counted as the maximum processing capacity of the business requests through the range function in the ordered set, and the data flow limiting method is executed on the business requests in the ordered set in sequence based on the sorting.
[0108] Specifically, when the sleep times of the service request are greater than a preset number, a preset rule is executed on the service request.
[0109] In one embodiment of the present invention, a Redis ordered set (ZSET) is used to store the timestamp of each business request. The orderliness of ZSET can be used to conveniently count the number of queries within a time window. ZSET stores the request serial number (a unique identifier for each business request) and the timestamp. Each request stores both of these contents. The ZSET data structure can support sorting by the Long value of the timestamp, and the range function in ZSET can be used to count the number of business requests within the time window. Redis Zrange can return members (business requests) within a specified interval in ZSET.
[0110] In one embodiment of the present invention, the current limiter is set as follows: when a service request sleeps for 3 times, the service request is considered expired, and an expiration notification "Your request has expired, please submit again!" is sent to the client.
[0111] In one embodiment of the present invention, the preset sleep time of the service request is 1 millisecond.
[0112] In one embodiment of the present invention, the current limiter has a preset rule: when a service request sleeps for 5 times, the relevant interface will be requested according to the service request to implement the service request.
[0113] The present invention relates to the field of computers, and provides a data flow limiting method, which includes: obtaining the existing business request processable amount based on the maximum processing amount of business requests obtained by a flow limiter, the time of this query of the number of business requests, the time of the last query of the number of business requests, the increase speed of the business request processable amount, and the remaining processable amount of the last business request, and judging whether the existing business request processable amount is greater than or equal to 0; if not, limiting the flow of the business request, after the business request sleeps for a preset time, returning to steps S1 and S2 to judge whether to limit the flow of the business request through the flow limiter; if so, reducing the existing business request processable amount by 1, and then requesting related interfaces according to the business request to realize the current business request. The present invention makes flow limiting judgments through multiple data, implements different strategies according to the judgment situation, and improves the overall performance of the system.
[0114] In addition, the embodiment of the present invention further proposes a computer-readable storage medium, which may be any one or any combination of a hard disk, a multimedia card, an SD card, a flash memory card, an SMC, a read-only memory (ROM), an erasable programmable read-only memory (EPROM), a portable compact disk read-only memory (CD-ROM), a USB memory, etc. The computer-readable storage medium includes a data current limiting program, and when the data current limiting program is executed by a processor, the following operations are implemented:
[0115] S1. Receive a service request through a service request interface, and obtain first service data and second service data through a preset Lua script of a current limiter;
[0116] S2. Obtain a query time span value through the time of the current query of the number of business requests and the time of the last query of the number of business requests, calculate the increase in the processable amount of the current business request through the query time span value and the increase rate of the processable amount of the business request, calculate the cumulative sum of the increase in the processable amount of the current business request and the remaining processable amount of the last business request, compare the maximum processable amount of the business request with the cumulative sum, take the minimum value of the two as the processable amount of the current business request, and then determine whether the minimum value is greater than or equal to 0;
[0117] S3. If not, limit the flow of the business request. After the business request sleeps for a preset time, return to steps S1 and S2 to determine whether to limit the flow of the business request through the flow limiter; if so, reduce the processable amount of the existing business request by 1, and request the relevant interface according to the business request to implement the business request.
[0118] The specific implementation of the computer-readable storage medium of the present invention is substantially the same as the specific implementation of the above-mentioned data current limiting method and electronic device 10, and will not be described in detail here.
[0119] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, device, article or method including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, device, article or method. In the absence of further restrictions, an element defined by the sentence "includes a ..." does not exclude the existence of other identical elements in the process, device, article or method including the element.
[0120] The serial numbers of the above embodiments of the present invention are only for description and do not represent the advantages and disadvantages of the embodiments. Through the description of the above implementation modes, those skilled in the art can clearly understand that the above embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course, by hardware, but in many cases the former is a better implementation mode. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) as described above, and includes a number of instructions for enabling a terminal device (which can be a mobile phone, a computer, a server, or a network device, etc.) to execute the methods described in each embodiment of the present invention.
[0121] The above are only preferred embodiments of the present invention, and are not intended to limit the patent scope of the present invention. Any equivalent structure or equivalent process transformation made using the contents of the present invention specification and drawings, or directly or indirectly applied in other related technical fields, are also included in the patent protection scope of the present invention.
Claims
1. A data flow limiting method, characterized in that: The method includes: S1. Receive a service request through a service request interface, and obtain first service data and second service data through a preset Lua script of a current limiter; S2. Calculate the increase in the processable amount of the current business request by using the first business data and the second business data, calculate the cumulative sum of the increase in the processable amount of the current business request and the remaining processable amount of the previous business request, compare the maximum processable amount of the business request in the first business data with the cumulative sum, take the minimum value of the two as the processable amount of the current business request, and then determine whether the minimum value is greater than or equal to 0; S3. If not, limit the flow of the business request. After the business request sleeps for a preset time, return to steps S1 and S2 to determine whether to limit the flow of the business request through the flow limiter; if so, reduce the processable amount of the existing business request by 1, and request the relevant interface according to the business request to implement the business request.
2. The data flow limiting method according to claim 1, characterized in that: The receiving a service request through a service request interface includes: The current limiter is added through a preset AOP aspect, the business request is received through the current limiter, and the business request is stored in an ordered set in the current limiter.
3. The data flow limiting method according to claim 1, characterized in that: The current limiter includes a Redis database and a Lua script, and uses the Lua script to check and logically control the first business data and the second business data on the Redis database server side to limit the current of business requests.
4. The data flow limiting method according to claim 3, characterized in that: The current limiter uses the ordered set of the Redis database to store the timestamp of each business request, and counts the number of business requests within a preset time period through the orderliness of the ordered set.
5. The data flow limiting method according to claim 4, characterized in that: The counting of the number of service requests within a preset time period by the orderliness of the ordered set includes: The ordered set stores the unique identifier in each business request and sorts them according to the Long value of the timestamp. The number of business requests within a preset time window is counted as the maximum processing capacity of the business requests through the range function in the ordered set, and the data flow limiting method is executed on the business requests in the ordered set in sequence based on the sorting.
6. The data flow limiting method according to claim 4, characterized in that: The method further includes: Presetting an expiration time for each service request in the ordered set, and judging whether the service request is expired by using the timestamp of the ordered set and the expiration time; If the service request expires, it is deleted from the ordered set, and an expiration notification is sent to the client corresponding to the service request.
7. The data flow limiting method according to claim 5, characterized in that: The time window is a dynamic time window, and the time window sets the maximum processing volume of the current business request according to the limit processing volume of the business request and the number of business requests in the ordered set.
8. A data current limiting device, characterized in that: The device comprises: An acquisition module, used to receive a service request through a service request interface, and acquire first service data and second service data through a preset Lua script of a current limiter; a judgment module, configured to calculate an increase in the processable amount of the current business request through the first business data and the second business data, calculate a cumulative sum of the increase in the processable amount of the current business request and the remaining processable amount of the previous business request, compare the maximum processable amount of the business request in the first business data with the cumulative sum, take the minimum value of the two as the processable amount of the current business request, and then judge whether the minimum value is greater than or equal to 0; An execution module is used to limit the flow of the business request if the minimum value is not greater than or equal to 0. After the business request sleeps for a preset time, it returns to steps S1 and S2 to determine whether to limit the flow of the business request through the limiter; if the minimum value is greater than or equal to 0, the processable amount of the existing business request is reduced by 1, and the relevant interface is requested according to the business request to implement the business request.
9. An electronic device, characterized in that: The electronic device includes a memory and a processor, wherein a data current limiting program is stored in the memory, and when the data current limiting program is executed by the processor, the data current limiting method according to any one of claims 1 to 7 is implemented.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a data flow limiting program, and when the data flow limiting program is executed by a processor, the data flow limiting method according to any one of claims 1 to 7 is implemented.
