A method, device, computing equipment and storage medium for limiting current of cluster services
By executing Lua scripts in the Redis database, the data error problem in the IP current limiting scheme in the cluster in a high concurrency environment is solved, and the accuracy and consistency of current limiting processing is achieved, avoiding the resource consumption and deadlock risks brought about by distributed locks.
Patent Information
- Application Number
- CN202110857201.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-07-28
- Publication Date
- 2025-04-29
- Estimated Expiration
- 2041-07-28
AI Technical Summary
In a cluster high concurrency environment, the prior art IP current limiting scheme is prone to data error problems. For example, when multiple threads are updated at the same time, it may lead to overwrite, and the distributed lock consumes time and resources. Once the lock cannot be released in time, it will lead to deadlocks, affecting services.
By setting and executing Lua scripts in the Redis database, the operation operations are performed in the Lua scripts, using the atomicity of the Lua scripts and the single-threaded characteristics of the Redis database, concurrent operations under multiple threads are avoided, serial execution of user requests is ensured, and distributed locks are not required.
It effectively avoids data confusion problems under high concurrency, ensures the accuracy and consistency of current limit processing, reduces the consumption of system resources, and avoids the risk of deadlock.
Smart Images

Figure CN113467896B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of communication technology, and in particular to a current limiting method, device, computing equipment and storage medium for cluster services. Background Art
[0002] IP current limiting under high concurrency of cluster services means limiting the number of requests within a certain period of time for different IP addresses in a cluster deployment environment with high concurrent requests to prevent excessive pressure on the server and database crashes, thereby ensuring the normal operation of the service at the cost of losing some requests.
[0003] The existing technical center current limiting algorithms include: counter algorithm, leaky bucket algorithm and token bucket algorithm, etc.; among them, the counter algorithm usually records the request time and number of requests of each IP in a cache framework such as Redis. The principle is to record the request timestamp and counter, that is, within a certain period of time, each time a request is made, the counter is increased by 1 to determine whether the maximum (Max) access volume is exceeded. If the Max access volume is exceeded, the request is rejected. If it is not exceeded, the business is entered and the request continues.
[0004] However, the above scheme of the prior art usually has data error problems in a high-concurrency cluster environment. For example, if multiple threads enter the interface at the same time and read the number of requests in Redis at the same time, the multiple threads read the same number of counters at this time, and then judge whether the number of requests exceeds the maximum number of visits. If none of them exceeds the maximum number of visits, the values of the counters in the Redis database are updated respectively, but the last executed update thread will overwrite the execution result of the previous update thread. To solve the above problem, the prior art can use distributed locks to control only a single thread to enter the operation in a high-concurrency situation, that is, before entering the request interface, a distributed lock is obtained first. If the distributed lock can be obtained, the request interface is entered for request judgment. If the distributed lock cannot be obtained, it waits. However, distributed locks consume a lot of time and resources, and once a lock cannot be released in time and causes a deadlock, it will have a significant impact on the business. Summary of the invention
[0005] In view of the above problems, the present invention is proposed to provide a method, apparatus, computing device and storage medium for limiting the flow of cluster services that overcome the above problems or at least partially solve the above problems.
[0006] According to one aspect of the present invention, a method for limiting current of a cluster service is provided, comprising:
[0007] Obtain the IP address corresponding to the user request and the timestamp corresponding to the time when the user request was initiated;
[0008] Query the preset maximum number of visits corresponding to the IP address;
[0009] Reading a Lua script, and setting parameters of the Lua script according to the IP address, the timestamp, and the maximum number of visits;
[0010] Execute the Lua script and perform current limiting processing on the IP address according to the execution result.
[0011] According to another aspect of the present invention, a current limiting device for a cluster service is provided, comprising:
[0012] An information query module is used to obtain the IP address corresponding to the user request and the timestamp corresponding to the time when the user request is initiated; query the preset maximum number of visits corresponding to the IP address;
[0013] A parameter setting module, used for reading a Lua script and setting parameters of the Lua script according to the IP address, the timestamp and the maximum number of visits;
[0014] The execution module is used to execute the Lua script and perform current limiting processing on the IP address according to the execution result.
[0015] According to another aspect of the present invention, there is provided a computing device, comprising: a processor, a memory, a communication interface and a communication bus, wherein the processor, the memory and the communication interface communicate with each other via the communication bus;
[0016] The memory is used to store at least one executable instruction, and the executable instruction enables the processor to execute an operation corresponding to the current limiting method of the above-mentioned cluster service.
[0017] According to another aspect of the present invention, a computer storage medium is provided, wherein the storage medium stores at least one executable instruction, and the executable instruction enables a processor to perform operations corresponding to the current limiting method for a cluster service as described above.
[0018] According to a flow limiting method, device, computing device and storage medium of a cluster service of the present invention, the IP address corresponding to the user request and the timestamp corresponding to the time when the user request is initiated are obtained; the maximum access corresponding to the preset IP address is queried; the Lua script is read, and the parameters of the Lua script are set according to the IP address, timestamp and maximum access; the Lua script is executed, and the IP address is subjected to flow limiting processing according to the execution result. The present invention sets and executes the Lua script in the Redis database, so that the operation operation is performed in the Lua script. Based on the strong atomicity of the Lua script and the single-threaded characteristics of the Redis database, concurrent operations under multi-threading are avoided, and the serial execution of user requests is guaranteed, and the distributed lock in the prior art is not required, thereby avoiding the waste of time and resources.
[0019] The above description is only an overview of the technical solution of the present invention. In order to more clearly understand the technical means of the present invention, it can be implemented according to the contents of the specification. In order to make the above and other purposes, features and advantages of the present invention more obvious and easy to understand, the specific implementation methods of the present invention are listed below. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] Various other advantages and benefits will become apparent to those of ordinary skill in the art by reading the detailed description of the preferred embodiments below. The accompanying drawings are only for the purpose of illustrating the preferred embodiments and are not to be considered as limiting the present invention. Moreover, the same reference symbols are used throughout the accompanying drawings to represent the same components. In the accompanying drawings:
[0021] Figure 1 A flow chart of a cluster service current limiting method provided by an embodiment of the present invention is shown;
[0022] Figure 2 A schematic diagram of the implementation process of a current limiting method for a cluster service provided by an embodiment of the present invention is shown;
[0023] Figure 3 A schematic structural diagram of a current limiting device for a cluster service provided by an embodiment of the present invention is shown;
[0024] Figure 4 A schematic diagram of the structure of a computing device provided by an embodiment of the present invention is shown. DETAILED DESCRIPTION
[0025] The exemplary embodiments of the present invention will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present invention are shown in the accompanying drawings, it should be understood that the present invention can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided in order to enable a more thorough understanding of the present invention and to enable the scope of the present invention to be fully communicated to those skilled in the art.
[0026] Figure 1 A flow chart of an embodiment of a method for limiting current of a cluster service according to the present invention is shown as follows: Figure 1 As shown, the method comprises the following steps:
[0027] Step S110: Obtain the IP address corresponding to the user request and the timestamp corresponding to the time when the user request is initiated.
[0028] In an optional manner, step S110 further includes: in a cluster node of the cluster server, extracting an IP address corresponding to the user request from a request header of the user request; and obtaining a timestamp corresponding to the time when the user request is initiated through a system function.
[0029] Figure 2A schematic diagram of the implementation process of a current limiting method for a cluster service provided in this embodiment, such as Figure 2 As shown, in this step, the cluster server is configured with multiple cluster nodes (cluster nodes 1-6). When a user sends multiple requests through http, multiple user requests enter the load balancing server, such as the nginx server. The load balancing server forwards the multiple user requests to multiple cluster nodes of the cluster server, extracts the IP address corresponding to the user request from the request header of the user request, and obtains the timestamp [argv1] corresponding to the time when the user request is initiated through the system function provided by System, where the timestamp is a millisecond timestamp.
[0030] In this embodiment, the current limiting cache data can be used to record and manage the number of requests for the IP address within a unit time, wherein the IP address corresponding to the user request is used as the data key [key1] of the current limiting cache data, and the data value (value) of the current limiting cache data is the hash request data.
[0031] Step S120: Query the maximum number of visits corresponding to the preset IP address.
[0032] In an optional manner, step S120 further includes: preferentially querying the maximum number of visits corresponding to the preset IP address from the cache; if the maximum number of visits corresponding to the IP address is not queried from the cache, querying the maximum number of visits corresponding to the IP address from the database, and storing the queried maximum number of visits in the cache for the next query from the cache. In this embodiment, the database may be a Redis database.
[0033] In this step, the maximum number of visits per unit time [argv2] preset for the IP address as the accessed object is queried in the cache or Redis database based on the IP address corresponding to the user request obtained in step S110, wherein those skilled in the art can set the unit time according to actual needs, and the unit time may be one second, etc.
[0034] Since it is a user request under high concurrency, in order to reduce database IO operations, the maximum access volume can be obtained from the cache first. If it is not obtained in the cache, the maximum access volume [argv2] per unit time pre-set by the IP address user is queried from the database, and the maximum access volume [argv2] is placed in the cache for the next query from the cache. In order to further reduce the database pressure, the cache expiration time can be set to one minute, etc. When it is found that the value of the maximum access volume per unit time pre-set by a user is too large or too small, the user can be notified in time to modify or automatically modify the maximum access volume per unit time in the database to adjust it. After the cache expires, the maximum access volume per unit time after the modification is queried from the database as the latest data.
[0035] Step S130: Read the Lua script and set the parameters of the Lua script according to the IP address, timestamp and maximum access volume.
[0036] In this step, the specific content "Luas" in the Lua script is read by obtaining the absolute path of the Lua script file, and the parameters of the Lua script are set according to the user IP address [key1], the timestamp corresponding to the time when the user request is initiated [argv1], and the maximum number of visits per unit time [argv2] obtained in the above steps S110 and S120, and the user IP address [key1], the timestamp corresponding to the time when the user request is initiated [argv1], and the maximum number of visits per unit time [argv2] are respectively added to the keys set and the argvs set in the input parameter set (List set) of the Lua script.
[0037] It should be noted that in order to further reduce database IO operations, the Lua script file can be saved in the cache. For the cache expiration time of the Lua script file, technical personnel in this field can set it according to actual needs. For example, the cache expiration time of the Lua script file can be set to 24 hours.
[0038] Step S140: Execute the Lua script and perform current limiting processing on the IP address according to the execution result.
[0039] In this step, the Jedis client of the Redis database executes the Lua script through the eval function. The Lua script is very small and runs very fast. Each execution is performed as an atomic transaction. In this embodiment, the Lua script is embedded in the eval function for execution to achieve the function of rejecting or passing the access request corresponding to the IP address. The execution process is: the script content Luas, keys set and argvs set of the Lua script are used as the three parameters of the eval function to set and execute the eval function. The Lua script can obtain parameters successively through the above steps S110 and S120. The eval function executes the Lua script as a Redis command. Based on the single-threaded characteristics of the Redis database, the atomic operation of the Lua script is guaranteed. Figure 2 As shown, each cluster node executes a Lua script through the eval function, namely thread_Lua1, thread_Lua2, thread_Lua3, thread_Lua4, thread_Lua5 and thread_Lua6 in the figure. Furthermore, the Lua script is put into the cache as a Redis command, and the cache of the Redis database executes the above Lua scripts in sequence.
[0040] In an optional manner, step S140 further includes: querying the database whether there is current limiting cache data with the IP address in the parameters of the Lua script as the data key; if there is no current limiting cache data with the IP address as the data key, setting the number of requests per unit time to 1, using the IP address, the timestamp in the parameters of the Lua script and the number of requests per unit time, constructing the current limiting cache data and storing it in the database, returning a response of successful execution, and processing according to the user request; if there is current limiting cache data with the IP address as the data key, determining whether the time interval between the timestamp in the current limiting cache data and the timestamp in the parameters of the Lua script is greater than the unit time; if the time interval is greater than the unit time, setting the number of requests per unit time to 1, using the timestamp in the parameters of the Lua script and the number of requests per unit time, updating the current limiting cache data, returning a response of successful execution, and processing according to the user request; if the time interval is less than or equal to the unit time, adding 1 to the number of requests per unit time in the current limiting cache data, and determining the execution result based on the comparison result of the number of requests per unit time after processing and the maximum access amount in the parameters of the Lua script.
[0041] In an optional manner, the data key of the current limiting cache data is the IP address, and the data value corresponding to the data key of the current limiting cache data is the hash request data obtained by combining the timestamp and the number of requests per unit time; the data key of the hash request data is the timestamp, and the data value corresponding to the data key of the hash request data is the number of requests per unit time.
[0042] Table 1 is a current limiting cache data storage table in the database, as shown in Table 1, where each row represents hash request data formed under a user request, where the key is a timestamp and the value is the number of requests per unit time.
[0043] Columns of current-limiting cache data (row) Data key Data value 1 1605173486172 44 2 1605173489863 3 3 1605173494233 1 4 1605173496299 1 5 1605173499230 1
[0044] Table 1 Current limiting cache data storage table in the database
[0045] In an optional manner, determining the execution result based on the comparison result of the number of requests per unit time after processing and the maximum access volume in the parameters of the Lua script further includes: judging whether the number of requests per unit time after processing is greater than the maximum access volume in the parameters of the Lua script; if it is less than or equal to the maximum access volume, using the timestamp in the parameters of the Lua script and the number of requests per unit time after processing, updating the current limiting cache data, returning a response of successful execution, and processing according to the user request; if it is greater than the maximum access volume, returning a response of failed execution, and rejecting the user request to limit the traffic to the IP address.
[0046] In an optional manner, the method further includes: setting a cache time for the current-limiting cache data.
[0047] Specifically, in step S140, the execution of the Lua script may result in four situations. The first situation is: based on the IP address [key1], query the database to see whether there is current limiting cache data with the IP address in the parameters of the Lua script as the data key; if there is no current limiting cache data with the IP address as the data key, it means that the IP address is initiating a user request for the first time, and the number of requests per unit time is set to 1. The current limiting cache data is constructed and stored in the database using the IP address, the timestamp in the parameters of the Lua script, and the number of requests per unit time, and the cache expiration time is set (generally set to 24 hours), and a successful execution response is returned. The user request has passed the current limiting process and continues to be processed according to the user request.
[0048] The second situation is: when there is current limiting cache data in the database with the IP address in the parameter of the Lua script as the data key, the timestamp of the time when the last user request was initiated is obtained based on the current limiting cache data, and it is determined whether the time interval between the timestamp in the current limiting cache data and the timestamp [argv1] in the parameter of the Lua script exceeds the unit time (for example, one second). If it exceeds the unit time, the count can be reset, and the number of requests per unit time is set to 1. The timestamp [argv1] in the parameter of the Lua script and the number of requests per unit time of 1 are used to update the current limiting cache data, set the cache expiration time, and return a successful response. The user request has passed the current limiting process and continues to be processed according to the user request.
[0049] The third situation is: if the time interval between the timestamp in the current limiting cache data and the timestamp [argv1] in the parameters of the Lua script does not exceed the unit time (for example, one second), that is, the time interval between the time when the current user request is initiated and the time when the last user request is initiated is within the unit time, then the number of requests per unit time in the current limiting cache data is obtained, and after adding 1 to it, it is determined whether it exceeds the maximum number of visits [argv2] preset per unit time for the IP address. If it is less than or equal to the maximum number of visits [argv2], the timestamp [argv1] in the parameters of the Lua script and the updated number of requests (i.e., after adding 1) are used to update the current limiting cache data, set the cache expiration time, and return a response of successful execution. The user request has passed the current limiting process and continues to be processed according to the user request.
[0050] The fourth case is: if the maximum number of visits per unit time preset for the IP address [argv2] is exceeded, that is, the request number limit is exceeded, a response of execution failure is returned and the user request is rejected to limit the traffic to the IP address, thereby achieving the effect of limiting the traffic to the IP.
[0051] By adopting the method of this embodiment, the present invention sets and executes Lua scripts in the Redis database in a high-concurrency cluster environment, so that operations are performed in the Lua script. Based on the strong atomicity of the Lua script and the single-threaded characteristics of the Redis database, the Redis database integrates operations such as reading, writing, judgment and calculation into one atomic operation; concurrent operations under multi-threading are avoided, the serial execution of user requests is guaranteed, and there is no need to use distributed locks in the prior art, thereby avoiding data confusion problems in high-concurrency situations; at the same time, through the setting of the cache in the Redis database, database IO operations are reduced and database pressure is avoided; and the calculation and reading and writing operations of the present invention are all in Redis, which hardly affects the normal response speed of the interface.
[0052] Figure 3FIG. 1 is a schematic diagram showing a structure of an embodiment of a current limiting device for cluster services of the present invention. Figure 3 As shown, the device includes: an information query module 310, a parameter setting module 320 and an execution module 330.
[0053] The information query module 310 is used to obtain the IP address corresponding to the user request and the timestamp corresponding to the time when the user request is initiated; and query the maximum number of visits corresponding to the preset IP address.
[0054] In an optional manner, the information query module 310 is further used to: extract the IP address corresponding to the user request from the request header of the user request in the cluster node of the cluster server; and obtain the timestamp corresponding to the time when the user request is initiated through a system function.
[0055] In an optional manner, the information query module 310 is further used to: preferentially query the maximum number of visits corresponding to a pre-set IP address from the cache; if the maximum number of visits corresponding to the IP address is not queried from the cache, query the maximum number of visits corresponding to the IP address from the database, and store the queried maximum number of visits in the cache for the next query from the cache.
[0056] The parameter setting module 320 is used to read the Lua script and set the parameters of the Lua script according to the IP address, timestamp and maximum access volume.
[0057] The execution module 330 is used to execute the Lua script and perform current limiting processing on the IP address according to the execution result.
[0058] In an optional manner, the execution module 330 is further used to: query whether there is current limiting cache data in the database with the IP address in the parameters of the Lua script as the data key; if there is no current limiting cache data with the IP address as the data key, the number of requests per unit time is set to 1, and the current limiting cache data is constructed and stored in the database using the IP address, the timestamp in the parameters of the Lua script, and the number of requests per unit time, and returns a response of successful execution, and processes according to the user request; if there is current limiting cache data with the IP address as the data key, it is determined whether the time interval between the timestamp in the current limiting cache data and the timestamp in the parameters of the Lua script is greater than the unit time; if the time interval is greater than the unit time, the number of requests per unit time is set to 1, and the timestamp in the parameters of the Lua script and the number of requests per unit time are used to update the current limiting cache data, return a response of successful execution, and process according to the user request; if the time interval is less than or equal to the unit time, the number of requests per unit time in the current limiting cache data is added by 1, and the execution result is determined based on the comparison result of the number of requests per unit time after processing and the maximum number of visits in the parameters of the Lua script.
[0059] In an optional manner, the data key of the current limiting cache data is the IP address, and the data value corresponding to the data key of the current limiting cache data is the hash request data obtained by combining the timestamp and the number of requests per unit time; the data key of the hash request data is the timestamp, and the data value corresponding to the data key of the hash request data is the number of requests per unit time.
[0060] In an optional manner, the execution module 330 is further used to: determine whether the number of requests per unit time after processing is greater than the maximum number of visits in the parameters of the Lua script; if it is less than or equal to the maximum number of visits, use the timestamp in the parameters of the Lua script and the number of requests per unit time after processing to update the current limiting cache data, return a response of successful execution, and process according to the user request; if it is greater than the maximum number of visits, return a response of failed execution and reject the user request to limit the traffic to the IP address.
[0061] In an optional manner, the execution module 330 is further used to: set a cache time for the current-limiting cache data.
[0062] By adopting the device of this embodiment, the present invention sets and executes Lua scripts in the Redis database in a high-concurrency cluster environment, so that operations are performed in the Lua scripts. Based on the strong atomicity of the Lua scripts and the single-threaded characteristics of the Redis database, the Redis database integrates operations such as reading, writing, judgment and calculation into one atomic operation, avoiding concurrent operations under multi-threading, ensuring the serial execution of user requests, and not needing to use distributed locks in the prior art, avoiding data confusion problems in high-concurrency situations.
[0063] An embodiment of the present invention provides a non-volatile computer storage medium, wherein the computer storage medium stores at least one executable instruction, and the computer executable instruction can execute a cluster service current limiting method in any of the above method embodiments.
[0064] The executable instructions may be specifically used to cause the processor to perform the following operations:
[0065] Get the IP address corresponding to the user request and the timestamp corresponding to the time when the user request was initiated;
[0066] Query the maximum number of visits corresponding to the preset IP address;
[0067] Read the Lua script and set the parameters of the Lua script according to the IP address, timestamp and maximum number of visits;
[0068] Execute the Lua script and perform current limiting on the IP address based on the execution result.
[0069] Figure 4 The schematic diagram of the structure of the computing device embodiment of the present invention is shown. The specific embodiment of the present invention does not limit the specific implementation of the computing device.
[0070] like Figure 4 As shown, the computing device may include:
[0071] Processor, Communications Interface, Memory, and Communications Bus.
[0072] Wherein: the processor, the communication interface, and the memory communicate with each other through the communication bus. The communication interface is used to communicate with other devices such as client or other server network elements. The processor is used to execute the program, and specifically can execute the relevant steps in the above-mentioned cluster service current limiting method embodiment.
[0073] Specifically, the program may include program codes including computer operation instructions.
[0074] The processor may be a central processing unit (CPU), or an application specific integrated circuit (ASIC), or one or more integrated circuits configured to implement the embodiments of the present invention. The one or more processors included in the server may be processors of the same type, such as one or more CPUs; or processors of different types, such as one or more CPUs and one or more ASICs.
[0075] The memory is used to store programs. The memory may include a high-speed RAM memory, and may also include a non-volatile memory, such as at least one disk memory.
[0076] The program can be specifically used to cause the processor to perform the following operations:
[0077] Get the IP address corresponding to the user request and the timestamp corresponding to the time when the user request was initiated;
[0078] Query the maximum number of visits corresponding to the preset IP address;
[0079] Read the Lua script and set the parameters of the Lua script according to the IP address, timestamp and maximum number of visits;
[0080] Execute the Lua script and perform current limiting on the IP address based on the execution result.
[0081] The algorithm or display provided herein is not inherently related to any particular computer, virtual system or other equipment. Various general purpose systems can also be used together with the teachings based on this. According to the above description, it is obvious to construct the structure required for this type of system. In addition, the embodiment of the present invention is not directed to any specific programming language yet. It should be understood that various programming languages can be utilized to realize the content of the present invention described herein, and the description made to specific languages above is for disclosing the best mode of the present invention.
[0082] In the description provided herein, a large number of specific details are described. However, it is understood that embodiments of the present invention can be practiced without these specific details. In some instances, well-known methods, structures and techniques are not shown in detail so as not to obscure the understanding of this description.
[0083] Similarly, it should be understood that in order to streamline the present invention and aid in understanding one or more of the various inventive aspects, in the above description of exemplary embodiments of the present invention, the various features of the embodiments of the present invention are sometimes grouped together into a single embodiment, figure, or description thereof. However, this disclosed method should not be interpreted as reflecting the following intention: that the claimed invention requires more features than the features explicitly recited in each claim. More specifically, as reflected in the claims below, inventive aspects lie in less than all the features of the individual embodiments disclosed above. Therefore, the claims that follow the specific embodiment are hereby expressly incorporated into the specific embodiment, with each claim itself serving as a separate embodiment of the present invention.
[0084] Those skilled in the art will appreciate that the modules in the devices in the embodiments may be adaptively changed and arranged in one or more devices different from the embodiments. The modules or units or components in the embodiments may be combined into one module or unit or component, and in addition they may be divided into a plurality of submodules or subunits or subcomponents. Except that at least some of such features and / or processes or units are mutually exclusive, all features disclosed in this specification (including the accompanying claims, abstracts and drawings) and all processes or units of any method or device disclosed in this manner may be combined in any combination. Unless otherwise expressly stated, each feature disclosed in this specification (including the accompanying claims, abstracts and drawings) may be replaced by an alternative feature providing the same, equivalent or similar purpose.
[0085] In addition, those skilled in the art will appreciate that, although some embodiments herein include certain features included in other embodiments but not other features, the combination of features of different embodiments is meant to be within the scope of the present invention and form different embodiments. For example, in the claims below, any one of the claimed embodiments may be used in any combination.
[0086] The various component embodiments of the present invention can be implemented in hardware, or in software modules running on one or more processors, or in a combination thereof. It should be understood by those skilled in the art that a microprocessor or digital signal processor (DSP) can be used in practice to implement some or all functions of some or all components according to embodiments of the present invention. The present invention can also be implemented as a device or apparatus program (e.g., computer program and computer program product) for executing part or all of the methods described herein. Such a program implementing the present invention can be stored on a computer-readable medium, or can have the form of one or more signals. Such a signal can be downloaded from an Internet website, or provided on a carrier signal, or provided in any other form.
[0087] It should be noted that the above embodiments illustrate the present invention rather than limit it, and that those skilled in the art may devise alternative embodiments without departing from the scope of the appended claims. In the claims, any reference symbol between brackets shall not be construed as a limitation on the claims. The word "comprising" does not exclude the presence of elements or steps not listed in the claims. The word "one" or "an" preceding an element does not exclude the presence of a plurality of such elements. The present invention may be implemented by means of hardware comprising a number of different elements and by means of a suitably programmed computer. In a unit claim enumerating a number of devices, several of these devices may be embodied by the same hardware item. The use of the words first, second, and third, etc. does not indicate any order. These words may be interpreted as names. The steps in the above embodiments, unless otherwise specified, should not be understood as limitations on the order of execution.
Claims
1. A method for limiting the flow of a cluster service, characterized in that: include: Obtain the IP address corresponding to the user request and the timestamp corresponding to the time when the user request was initiated; Query the preset maximum number of visits corresponding to the IP address; Reading a Lua script, and setting parameters of the Lua script according to the IP address, the timestamp, and the maximum number of visits; Execute the Lua script, and perform current limiting processing on the IP address according to the execution result; The executing of the Lua script and performing current limiting processing on the IP address according to the execution result further includes: Querying whether there is current limiting cache data with the IP address in the parameters of the Lua script as the data key in the database; If there is no current limiting cache data with the IP address as the data key, the number of requests per unit time is set to 1, and the current limiting cache data is constructed and stored in the database using the IP address, the timestamp in the parameters of the Lua script, and the number of requests per unit time, and a response of successful execution is returned to process according to the user request; If there is current limiting cache data using the IP address as a data key, determining whether the time interval between the timestamp in the current limiting cache data and the timestamp in the parameter of the Lua script is greater than the unit time; If the time interval is greater than the unit time, the number of requests within the unit time is set to 1, the timestamp in the parameters of the Lua script and the number of requests within the unit time are used to update the current limiting cache data, a response of successful execution is returned, and processing is performed according to the user request; If the time interval is less than or equal to the unit time, the number of requests per unit time in the current limiting cache data is incremented by 1, and the execution result is determined based on the comparison result between the number of requests per unit time after processing and the maximum access amount in the parameters of the Lua script.
2. The method according to claim 1, characterized in that: The step of obtaining the IP address corresponding to the user request and the timestamp corresponding to the time when the user request is initiated further comprises: In a cluster node of the cluster server, extracting an IP address corresponding to the user request from a request header of the user request; The timestamp corresponding to the time when the user request is initiated is obtained through the system function.
3. The method according to claim 1, characterized in that The querying of the maximum number of visits corresponding to the preset IP address further comprises: Priority is given to querying the pre-set maximum number of visits corresponding to the IP address from the cache; if the maximum number of visits corresponding to the IP address is not queried from the cache, the maximum number of visits corresponding to the IP address is queried from the database, and the queried maximum number of visits is stored in the cache for next query from the cache.
4. The method according to claim 1, characterized in that: The data key of the current limiting cache data is the IP address, and the data value corresponding to the data key of the current limiting cache data is the hash request data obtained by combining the timestamp and the number of requests per unit time; the data key of the hash request data is the timestamp, and the data value corresponding to the data key of the hash request data is the number of requests per unit time.
5. The method according to claim 1, characterized in that The step of comparing the number of requests per unit time after processing with the maximum number of accesses in the parameters of the Lua script to determine the execution result further includes: Determine whether the number of requests per unit time after processing is greater than the maximum number of visits in the parameters of the Lua script; If it is less than or equal to the maximum number of visits, the current limiting cache data is updated using the timestamp in the parameters of the Lua script and the number of requests per unit time after processing, a response of successful execution is returned, and processing is performed according to the user request; If it is greater than the maximum access amount, a response of execution failure is returned and the user request is rejected to limit the traffic to the IP address.
6. The method according to any one of claims 1 to 5, characterized in that: The method further comprises: Set a cache time for the current limiting cache data.
7. A current limiting device for cluster services, characterized in that: include: An information query module is used to obtain the IP address corresponding to the user request and the timestamp corresponding to the time when the user request is initiated; Query the preset maximum number of visits corresponding to the IP address; A parameter setting module, used for reading a Lua script and setting parameters of the Lua script according to the IP address, the timestamp and the maximum number of visits; An execution module, used for executing the Lua script and performing current limiting processing on the IP address according to the execution result; Wherein, the execution module is further used for: Querying whether there is current limiting cache data with the IP address in the parameters of the Lua script as the data key in the database; If there is no current limiting cache data with the IP address as the data key, the number of requests per unit time is set to 1, and the current limiting cache data is constructed and stored in the database using the IP address, the timestamp in the parameters of the Lua script, and the number of requests per unit time, and a response of successful execution is returned to process according to the user request; If there is current limiting cache data using the IP address as a data key, determining whether the time interval between the timestamp in the current limiting cache data and the timestamp in the parameter of the Lua script is greater than the unit time; If the time interval is greater than the unit time, the number of requests within the unit time is set to 1, the timestamp in the parameters of the Lua script and the number of requests within the unit time are used to update the current limiting cache data, a response of successful execution is returned, and processing is performed according to the user request; If the time interval is less than or equal to the unit time, the number of requests per unit time in the current limiting cache data is incremented by 1, and the execution result is determined based on the comparison result between the number of requests per unit time after processing and the maximum access amount in the parameters of the Lua script.
8. A computing device, characterized in that include: A processor, a memory, a communication interface and a communication bus, wherein the processor, the memory and the communication interface communicate with each other via the communication bus; The memory is used to store at least one executable instruction, and the executable instruction enables the processor to perform an operation corresponding to a current limiting method for a cluster service as described in any one of claims 1-6.
9. A computer storage medium, characterized in that The storage medium stores at least one executable instruction, and the executable instruction enables the processor to execute an operation corresponding to a current limiting method for a cluster service as described in any one of claims 1-6.