Current limiting method, device, computing equipment and medium based on token bucket
By determining the number of target tokens based on time in the token bucket and adding tokens when insufficient, the problem of increasing risks of token production nodes in the distributed system is solved, and a higher reliability of current limiting operation is achieved.
Patent Information
- Application Number
- CN202011199853.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-10-30
- Publication Date
- 2024-11-29
- Estimated Expiration
- 2040-10-30
AI Technical Summary
The existing token bucket-based current limiting solution requires setting up token production nodes in distributed systems, which increases the risk of system problems.
By determining the target token number in the token bucket based on the time of the last production token and the current time, only performing addition operations when the tokens are insufficient in the token bucket, avoiding additional timing tasks, the token bucket itself performs current limiting operate.
Reduce the number of system nodes, improve the reliability of the system, and avoid potential risks caused by additional nodes.
Smart Images

Figure CN113765820B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, and more specifically, to a token bucket-based current limiting method, device, computing equipment, and medium. Background Art
[0002] The token bucket-based current limiting scheme used in the related art requires at least two roles, one is the token production node and the other is the current limiter itself. Among them, the token production node is used to perform timed tasks and add tokens to the token bucket. Since this scheme requires the setting of a token production node, it makes the distributed system have more nodes, which invisibly increases the risk of system problems. Summary of the invention
[0003] In view of this, the present disclosure provides a token bucket-based current limiting method, apparatus, computing device and medium.
[0004] One aspect of the present disclosure provides a token bucket-based current limiting method, including: receiving a pending request; obtaining the number of tokens in a token bucket; determining a target number of tokens based on the last time a token was produced and the current time when the number of tokens in the token bucket is less than the number of tokens required for the pending request; adding the target number of tokens to the token bucket; and determining whether to allow the pending request to pass based on the number of tokens in the token bucket after the addition operation is performed.
[0005] According to an embodiment of the present disclosure, determining the target number of tokens based on the time of the last token production and the current time includes: determining the time difference between the time of the last token production and the current time; determining the number of token production times based on the time difference; and determining the target number of tokens based on the number of token production times.
[0006] According to an embodiment of the present disclosure, determining the target number of tokens according to the number of token production times includes: obtaining the number of tokens produced in a single time; and determining the target number of tokens according to the number of token production times and the number of tokens produced in a single time.
[0007] According to an embodiment of the present disclosure, the target number of tokens is determined based on the number of token production times, including: when the number of token production times is less than 1: obtaining the cumulative number of credit tokens and the number of tokens produced for a single time; when the credit condition is met, determining the number of tokens required for the pending request as the target number of tokens, and determining the sum of the cumulative number of credit tokens and the number of tokens required for the pending request as the new cumulative number of credit tokens; and when the number of token production times is greater than or equal to 1: determining the number of producible tokens based on the token production times and the number of tokens produced for a single time; obtaining the cumulative number of credit tokens; and when the number of producible tokens is greater than the cumulative number of credit tokens, determining the difference between the number of producible tokens and the cumulative number of credit tokens as the target number of tokens, and setting the cumulative number of credit tokens to 0.
[0008] According to an embodiment of the present disclosure, the credit conditions include: the sum of the accumulated credit token number and the token number required for the pending requests is less than the single production token number; and / or the sum of the last credit token time and the credit time interval is less than the last token production time.
[0009] According to an embodiment of the present disclosure, determining whether to allow the pending request to pass based on the number of tokens in the token bucket after performing the adding operation includes: when the number of tokens in the token bucket is greater than or equal to the number of tokens required for the pending request, deducting the number of tokens required for the pending request from the token bucket to allow the pending request to pass; and when the number of tokens in the token bucket is less than the number of tokens required for the pending request, refusing to allow the pending request to pass.
[0010] Another aspect of the present disclosure provides a token bucket-based current limiting device, including: a receiving module for receiving pending requests; an acquisition module for acquiring the number of tokens in a token bucket; a first determination module for determining a target number of tokens based on the last time tokens were produced and the current time when the number of tokens in the token bucket is less than the number of tokens required for the pending requests; an adding module for adding the target number of tokens to the token bucket; and a passing module for determining whether to allow the pending request to pass based on the number of tokens in the token bucket after the adding operation is performed.
[0011] According to an embodiment of the present disclosure, the first determination module includes: a second determination submodule, used to determine the time difference between the time of the last token production and the current time; a third determination submodule, used to determine the number of token production times based on the time difference; and a fourth determination submodule, used to determine the target number of tokens based on the number of token production times.
[0012] Another aspect of the present disclosure provides a computing device, including: one or more processors; a storage device for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement the method as described above.
[0013] Another aspect of the present disclosure provides a computer-readable storage medium storing computer-executable instructions, which are used to implement the above method when executed.
[0014] Another aspect of the present disclosure provides a computer program, which includes computer executable instructions, and the instructions are used to implement the method described above when executed.
[0015] According to the token bucket-based current limiting method of the embodiment of the present disclosure, the token bucket addition operation is performed only when there are insufficient tokens in the token bucket, and there is no need to set up additional timed tasks for adding tokens at regular intervals. Therefore, the method can be executed by the current limiter itself, and there is no need to set up additional token production nodes, thereby reducing the number of nodes in the system and improving reliability. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] The above and other objects, features and advantages of the present disclosure will become more apparent through the following description of the embodiments of the present disclosure with reference to the accompanying drawings, in which:
[0017] Figure 1 An exemplary application scenario in which a token bucket-based current limiting method can be applied according to an embodiment of the present disclosure is schematically shown;
[0018] Figure 2 The flowchart of the token bucket-based current limiting method according to an embodiment of the present disclosure is schematically shown;
[0019] Figure 3 A flowchart for determining a target number of tokens according to an embodiment of the present disclosure is schematically shown;
[0020] Figure 4 Schematically shows a flow chart for determining a target number of tokens according to another embodiment of the present disclosure;
[0021] Figure 5 A schematic diagram of a token bucket-based current limiting method according to an embodiment of the present disclosure is schematically shown;
[0022] Figure 6 A schematic diagram of a token bucket-based current limiting method according to another embodiment of the present disclosure is schematically shown;
[0023] Figure 7 A block diagram schematically shows a token bucket-based current limiting device according to an embodiment of the present disclosure; and
[0024] Figure 8 A block diagram of a computer system suitable for the method described in the embodiment of the present disclosure is schematically shown according to an embodiment of the present disclosure. DETAILED DESCRIPTION
[0025] Hereinafter, embodiments of the present disclosure will be described with reference to the accompanying drawings. However, it should be understood that these descriptions are exemplary only and are not intended to limit the scope of the present disclosure. In the following detailed description, for ease of explanation, many specific details are set forth to provide a comprehensive understanding of the embodiments of the present disclosure. However, it is apparent that one or more embodiments may also be implemented without these specific details. In addition, in the following description, descriptions of known structures and technologies are omitted to avoid unnecessary confusion of the concepts of the present disclosure.
[0026] The terms used herein are only for describing specific embodiments and are not intended to limit the present disclosure. The terms "comprise", "include", etc. used herein indicate the existence of the features, steps, operations and / or components, but do not exclude the existence or addition of one or more other features, steps, operations or components.
[0027] All terms (including technical and scientific terms) used herein have the meanings commonly understood by those skilled in the art, unless otherwise defined. It should be noted that the terms used herein should be interpreted as having a meaning consistent with the context of this specification, and should not be interpreted in an idealized or overly rigid manner.
[0028] In the case of using expressions such as "at least one of A, B, and C, etc.", it should generally be interpreted in accordance with the meaning of the expression generally understood by those skilled in the art (for example, "a system having at least one of A, B, and C" should include but is not limited to a system having A alone, B alone, C alone, A and B, A and C, B and C, and / or A, B, C, etc.). In the case of using expressions such as "at least one of A, B, or C, etc.", it should generally be interpreted in accordance with the meaning of the expression generally understood by those skilled in the art (for example, "a system having at least one of A, B, or C" should include but is not limited to a system having A alone, B alone, C alone, A and B, A and C, B and C, and / or A, B, C, etc.).
[0029] The embodiment of the present disclosure provides a method for limiting current based on a token bucket and a device capable of applying the method. The method includes obtaining the number of tokens in a token bucket; when the number of tokens in the token bucket is less than the number of tokens required for a pending request, determining the target number of tokens according to the time of the last token production and the current time; adding the target number of tokens to the token bucket; and determining whether to allow the pending request to pass according to the number of tokens in the token bucket after the addition operation is performed.
[0030] Figure 1 The exemplary application scenario 100 to which the token bucket-based current limiting method according to the embodiment of the present disclosure can be applied is schematically shown. It should be noted that: Figure 1 What is shown are merely examples of application scenarios to which the embodiments of the present disclosure can be applied, in order to help those skilled in the art understand the technical content of the present disclosure, but it does not mean that the embodiments of the present disclosure cannot be used in other devices, systems, environments or scenarios.
[0031] like Figure 1 As shown, the application scenario 100 according to this embodiment may include terminal devices 101, 102, 103, a network 104 and a server 105. The network 104 is used to provide a medium for a communication link between the terminal devices 101, 102, 103 and the server 105. The network 104 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.
[0032] Users can use terminal devices 101, 102, 103 to interact with server 105 through network 104 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 101, 102, 103, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc. (only examples).
[0033] The terminal devices 101 , 102 , and 103 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers.
[0034] The server 105 may be a server that provides various services, such as a background management server (only an example) that provides support for websites browsed by users using the terminal devices 101, 102, and 103. The background management server may analyze and process the received data such as user requests, and feed back the processing results (such as web pages, information, or data obtained or generated according to user requests) to the terminal device.
[0035] It should be noted that the token bucket-based current limiting method provided in the embodiment of the present disclosure can generally be executed by the server 105. Accordingly, the token bucket-based current limiting device provided in the embodiment of the present disclosure can generally be set in the server 105. The token bucket-based current limiting method provided in the embodiment of the present disclosure can also be executed by a server or server cluster that is different from the server 105 and can communicate with the terminal devices 101, 102, 103 and / or the server 105. Accordingly, the token bucket-based current limiting device provided in the embodiment of the present disclosure can also be set in a server or server cluster that is different from the server 105 and can communicate with the terminal devices 101, 102, 103 and / or the server 105.
[0036] It should be understood that Figure 1 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.
[0037] Figure 2 The flowchart of the token bucket-based current limiting method according to an embodiment of the present disclosure is schematically shown.
[0038] like Figure 2 As shown, the method includes, in operation S210, receiving a request to be processed.
[0039] Then, in operation S220, the number of tokens in the token bucket is obtained.
[0040] In operation S230, when the number of tokens in the token bucket is less than the number of tokens required for the request to be processed, a target number of tokens is determined according to the time when the tokens were last generated and the current time.
[0041] According to an embodiment of the present disclosure, different amounts of data in pending requests require different numbers of tokens. The larger the amount of data in a pending request, the more tokens are required.
[0042] According to an embodiment of the present disclosure, operation S230 may, for example, include determining the time difference between the last time a token was produced and the current time, determining the number of token productions based on the time difference, and then determining the target number of tokens based on the number of token productions.
[0043] According to an embodiment of the present disclosure, the time difference can be divided by the interval length of adding tokens to calculate the number of token production times. The interval length of adding tokens can be set in advance as needed, and the present disclosure does not specifically limit the value of the interval length of adding tokens.
[0044] Figure 3 The flowchart of determining the target number of tokens according to an embodiment of the present disclosure is schematically shown.
[0045] like Figure 3 As shown, the target number of tokens may be determined through the following operations S331 to S332.
[0046] Wherein, in operation S331, the number of single production tokens is obtained.
[0047] In operation S332, a target number of tokens is determined according to the number of token generation times and the number of tokens generated at a single time.
[0048] According to the embodiments of the present disclosure, the product of the number of token production times and the number of tokens produced at a time can be calculated to obtain the number of replenishable tokens, and the number of replenishable tokens is used as the target number of tokens. The number of tokens produced at a time can be set in advance as needed, and the present disclosure does not specifically limit the value of the number of tokens produced at a time.
[0049] Figure 4 The flowchart of determining the target number of tokens according to another embodiment of the present disclosure is schematically shown.
[0050] like Figure 4 As shown, the target number of tokens may also be determined through the following operations S431 to S437.
[0051] Among them, in operation S431, it is determined whether the number of token production times is less than 1. If the number of token production times is less than 1, operations S432 to S433 are executed. If the number of token production times is greater than or equal to 1, operations S434 to S436 are executed.
[0052] In operation S432, the cumulative number of credit tokens and the number of single production tokens are acquired.
[0053] In operation S433, when the credit condition is met, the number of tokens required for the pending request is determined as the target number of tokens, and the sum of the cumulative number of credit tokens and the number of tokens required for the pending request is determined as a new cumulative number of credit tokens.
[0054] According to an embodiment of the present disclosure, the credit condition is used to prevent the occurrence of over-credit and ensure that the debt can be repaid normally when the next token is produced. The credit condition may include, for example: the sum of the cumulative number of credit tokens and the number of tokens required for pending requests is less than the number of tokens produced at a single time, and the sum of the last credit token time and the credit time interval is less than the last token production time.
[0055] In operation S434, the number of tokens that can be produced is determined according to the number of token production times and the number of tokens produced at a single time.
[0056] In operation S435, the accumulated credit token number is acquired.
[0057] In operation S436, when the number of producible tokens is greater than the cumulative number of credit tokens, the difference between the number of producible tokens and the cumulative number of credit tokens is determined as the target number of tokens, and the cumulative number of credit tokens is set to zero.
[0058] It should be noted that in practical applications, Figure 3 and Figure 4 The methods for determining the target number of tokens shown in the figure can be used either one or both, that is, for a portion of the requests, Figure 3 The method shown determines the target number of tokens, and for another part of the request, use Figure 4 The method shown in the figure determines the target number of tokens. For example, you can use Figure 3 The method shown determines the target number of tokens and uses the Figure 4 The method shown determines the target number of tokens. By allocating tokens in advance in the form of credit to requests with higher priority, it can be ensured that the requests with higher priority can be processed in time.
[0059] In operation S240 , a target number of tokens are added to the token bucket.
[0060] In operation S250, it is determined whether to allow the pending request to pass according to the number of tokens in the token bucket after the adding operation is performed.
[0061] According to an embodiment of the present disclosure, after adding tokens to the token bucket, if the number of tokens in the token bucket is greater than or equal to the number of tokens required for the pending request, the number of tokens required for the pending request can be deducted from the token bucket to allow the pending request to pass. If the number of tokens in the token bucket is still less than the number of tokens required for the pending request, the pending request can be rejected.
[0062] According to the token bucket-based current limiting method of the embodiment of the present disclosure, the token bucket addition operation is performed only when there are insufficient tokens in the token bucket, and there is no need to set up additional timed tasks for adding tokens at regular intervals. Therefore, the method can be executed by the current limiter itself, and there is no need to set up additional token production nodes, thereby reducing the number of nodes in the system and improving reliability.
[0063] Reference below Figure 5-6 , combined with specific embodiments Figure 2 to Figure 4 Those skilled in the art will appreciate that the following exemplary embodiments are only used to understand the present disclosure, and the present disclosure is not limited thereto.
[0064] According to an embodiment of the present disclosure, Redis can be used as a distributed medium, and a distributed current limiter program (referred to as current limiter) can be written based on the Lua scripting language to execute the token bucket-based current limiting method of the embodiment of the present disclosure. Since Redis has the characteristics of a single thread, writing a current limiter based on Redis and Lua scripts can ensure the atomicity of the current limiting operation and avoid errors caused by asynchronous execution of various operations.
[0065] According to an embodiment of the present disclosure, a Hash data structure in Redis can be used to store a defined token bucket object. Exemplarily, in this embodiment, the token bucket object can have the following key variables:
[0066] bucketSize: indicates the capacity of the token bucket.
[0067] nextAddTime: indicates the time when the token was last generated.
[0068] tokenSize: indicates the number of remaining tokens in the current token bucket.
[0069] addIntervalTime: indicates the interval time for adding tokens.
[0070] singleAddSize, which indicates the number of tokens produced at a time.
[0071] creditTokenSize, indicating the number of credit tokens, the initial value is 0.
[0072] nextCreditTime indicates the last time a token can be credited. According to the embodiment of the present disclosure, if nextCreditTime>nextAddTime, credit cannot be given, thus avoiding the situation of unlimited credit.
[0073] creditTintervalTime, which indicates the credit token interval. It should be noted that the credit token interval cannot exceed the interval for adding tokens.
[0074] singlerCreditSize represents the number of single credit tokens. It should be noted that singlerCreditSize = singleAddSize / (addIntervalTime / creditTintervalTime).
[0075] creditCountSize, indicating the cumulative credit token.
[0076] Figure 5 A schematic diagram of a token bucket-based current limiting method according to an embodiment of the present disclosure is schematically shown.
[0077] like Figure 5 As shown, the method may include operations S501 to S612.
[0078] Specifically, in operation S501, a request is made to enter a current limiter and a token is applied for from the current limiter.
[0079] In operation S502, the number of remaining tokens in the token bucket is obtained.
[0080] In operation S503, it is determined whether the remaining number of tokens is sufficient. If the remaining number of tokens meets the number of tokens required by the request, it means that the remaining tokens are sufficient, and operation S511 is performed. If the remaining number of tokens does not meet the number of tokens required by the request, it means that the remaining tokens are insufficient, and operation S504 is performed.
[0081] In operation S504, the “last time of generating a token” is obtained. It should be noted that the value of the “last time of generating a token” is the time of generating a token most recently from the current time.
[0082] In operation S505, the "time difference" is calculated by subtracting the "time of the last token production" from the "current time". The quotient is calculated by dividing the "time difference" by the "interval length of adding tokens", which is the "number of tokens that can be produced". If the "number of tokens that can be produced" is less than 1, it proves that it is not time to produce tokens yet, and the number of tokens that can be replenished is 0. If the "number of tokens that can be produced" is ≥ 1, the "number of tokens that can be replenished" is calculated by multiplying the "number of tokens that can be produced" by the "number of tokens produced at a time".
[0083] In operation S506, it is determined whether the number of replenishable tokens is greater than 0, and if it is less than or equal to 0, operation S507 is performed. If it is greater than 0, operation S508 is performed.
[0084] In operation S507, no token is returned.
[0085] In operation S508, to prevent the token from exceeding the capacity of the token bucket, the total number of tokens is obtained by adding the "number of tokens remaining in the token bucket" to the "number of tokens that can be replenished". It is determined whether the "total number of tokens" exceeds the "token bucket capacity". If the "total number of tokens" exceeds the token bucket capacity, the value of the "number of tokens remaining in the token bucket" is set to the "token bucket capacity" and operation S509 is executed. If the "total number of tokens" does not exceed the token bucket capacity, the "number of tokens that can be replenished" tokens are added to the "number of tokens remaining in the token bucket" and S510 is executed.
[0086] In operation S509, the difference between the "remaining token quantity" and the "token bucket capacity" is calculated to obtain the supplementary capacity.
[0087] In operation S510, the token is replenished, and the value of the last replenishment time is updated to the current time.
[0088] In operation S511, the token required for the request is deducted from the token bucket.
[0089] In operation S512, a token required for the request is returned.
[0090] Figure 6 The following schematically shows a token bucket-based current limiting method according to another embodiment of the present disclosure. Figure 6 As shown, the method may include operations S601 to S618.
[0091] Specifically, in operation S601, a request is made to enter a current limiter and a token is applied for from the current limiter.
[0092] In operation S602, the number of remaining tokens in the token bucket is obtained.
[0093] In operation S603, it is determined whether the number of remaining tokens is sufficient. If the number of remaining tokens is sufficient, operation S617 is performed. If the number of remaining tokens is insufficient, operation S604 is performed.
[0094] In operation S604, the time of the last token replenishment is obtained. It should be noted that the value of "the time of the last token production" is the time of the last token production closest to the current time.
[0095] In operation S605, the number of replenishable tokens is calculated according to the time of the last token replenishment, and the calculation formula is: the number of replenishable tokens = (current time - the time of the last token replenishment) / the interval length of adding tokens * the number of tokens produced at a single time.
[0096] In operation S606, it is determined whether the number of replenishable tokens is greater than 0. If the number of replenishable tokens is greater than 0, operation S613 is performed; otherwise, operation S607 is performed.
[0097] In operation S607, the last credit token time is obtained. It should be noted that the value of the last credit token time is the time of the last credit token operation triggered closest to the current time.
[0098] In operation S608, it is determined whether the credit condition is met. If not, operation S612 is performed. If satisfied, operations S609 to S611 are performed. The credit condition may include, for example: the sum of the cumulative credit token number and the number of tokens required for the pending request is less than the number of tokens produced at a single time, and the sum of the last credit token time and the credit time interval is less than the last token production time.
[0099] In operation S609, the token bucket is replenished with the credited amount of tokens.
[0100] In operation S610, credit tokens are accumulated.
[0101] In operation S611, the last time of credit token payment is updated to the current time, and then the process jumps to operation S617.
[0102] In operation S612, no token is returned to reject the request.
[0103] In operation S613, the difference between the number of replenishable tokens and the cumulative amount of credit tokens is calculated.
[0104] In operation S614, it is determined whether the difference is greater than 0, and if the difference is greater than 0, operation S615 is performed. If the difference is less than or equal to 0, operation S612 is performed.
[0105] In operation S615, the difference number of tokens is added to the token bucket, and the number of tokens owed is cleared to complete the debt repayment.
[0106] In operation S616, the last time the token was replenished is updated to the current time.
[0107] In operation S617, the number of tokens required for the request is deducted.
[0108] In operation S618, a request is returned to allocate the required number of tokens.
[0109] like Figure 5 and Figure 6 As shown, the embodiments of the present disclosure provide two solutions, one based on the common version of the token bucket ( Figure 5 ), a token bucket based on credit ( Figure 6 ). In the process of implementing the token bucket algorithm, the idea of 'lazy loading' is adopted. When the token is insufficient, the token bucket addition mechanism will be triggered, and the current limiter component will be called to perform the corresponding current limiting operation. There is no need to set up additional token production nodes, which can reduce the number of system nodes and improve reliability.
[0110] Figure 7 A block diagram of a token bucket-based current limiting device according to an embodiment of the present disclosure is schematically shown.
[0111] like Figure 7 As shown, the token bucket-based current limiting device 700 includes a receiving module 710 , an acquiring module 720 , a first determining module 730 , an adding module 740 and a passing module 750 .
[0112] The receiving module 710 may be configured to receive a request to be processed.
[0113] The acquisition module 720 may be used to acquire the number of tokens in the token bucket.
[0114] The first determination module 730 may be configured to determine a target number of tokens according to the last time tokens were generated and the current time when the number of tokens in the token bucket is less than the number of tokens required for the request to be processed.
[0115] The adding module 740 may be configured to add a target number of tokens to the token bucket.
[0116] Module 750 can be used to determine whether to allow the pending request to pass according to the number of tokens in the token bucket after the adding operation is performed.
[0117] According to an embodiment of the present disclosure, the first determination module may include, for example: a second determination submodule, which may be used to determine the time difference between the time when the token was last produced and the current time. A third determination submodule, which may be used to determine the number of token productions based on the time difference. A fourth determination submodule, which may be used to determine the target number of tokens based on the number of token productions.
[0118] According to the token bucket-based current limiting device of the embodiment of the present disclosure, the token bucket addition operation is performed only when there are insufficient tokens in the token bucket, and there is no need to set up additional timed tasks for adding tokens at regular intervals. Therefore, the method can be executed by the current limiting device (such as a current limiter) itself, and there is no need to set up additional token production nodes, thereby reducing the number of nodes in the system and improving reliability.
[0119] According to the embodiments of the present invention, any one or more of the modules, submodules, units, and subunits, or at least part of the functions of any one of them can be implemented in one module. According to the embodiments of the present invention, any one or more of the modules, submodules, units, and subunits can be split into multiple modules for implementation. According to the embodiments of the present invention, any one or more of the modules, submodules, units, and subunits can be at least partially implemented as hardware circuits, such as field programmable gate arrays (FPGAs), programmable logic arrays (PLAs), systems on chips, systems on substrates, systems on packages, application specific integrated circuits (ASICs), or can be implemented by hardware or firmware in any other reasonable way of integrating or packaging the circuit, or implemented in any one of the three implementation methods of software, hardware, and firmware, or in any appropriate combination of any of them. Alternatively, according to the embodiments of the present invention, one or more of the modules, submodules, units, and subunits can be at least partially implemented as computer program modules, and when the computer program modules are run, the corresponding functions can be performed.
[0120] For example, any multiple of the receiving module 710, the acquisition module 720, the first determination module 730, the adding module 740, and the passing module 750 can be combined in one module for implementation, or any one of the modules can be split into multiple modules. Alternatively, at least part of the functions of one or more of these modules can be combined with at least part of the functions of other modules and implemented in one module. According to an embodiment of the present disclosure, at least one of the receiving module 710, the acquisition module 720, the first determination module 730, the adding module 740, and the passing module 750 can be at least partially implemented as a hardware circuit, such as a field programmable gate array (FPGA), a programmable logic array (PLA), a system on a chip, a system on a substrate, a system on a package, an application specific integrated circuit (ASIC), or can be implemented by hardware or firmware such as any other reasonable way of integrating or packaging the circuit, or implemented in any one of the three implementation methods of software, hardware, and firmware or in a suitable combination of any of them. Alternatively, at least one of the receiving module 710 , the acquiring module 720 , the first determining module 730 , the adding module 740 , and the passing module 750 may be at least partially implemented as a computer program module, and when the computer program module is executed, a corresponding function may be performed.
[0121] Figure 8 The block diagram of a computer system suitable for implementing the method described above according to an embodiment of the present disclosure is schematically shown. Figure 8 The computer system shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.
[0122] like Figure 8 As shown, the computer system 800 according to the embodiment of the present disclosure includes a processor 801, which can perform various appropriate actions and processes according to the program stored in the read-only memory (ROM) 802 or the program loaded from the storage part 808 to the random access memory (RAM) 803. The processor 801 may include, for example, a general-purpose microprocessor (such as a CPU), an instruction set processor and / or a related chipset and / or a special-purpose microprocessor (for example, an application-specific integrated circuit (ASIC)), etc. The processor 801 may also include an onboard memory for caching purposes. The processor 801 may include a single processing unit or multiple processing units for performing different actions of the method flow according to the embodiment of the present disclosure.
[0123] In RAM 803, various programs and data required for the operation of system 800 are stored. Processor 801, ROM 802 and RAM 803 are connected to each other through bus 804. Processor 801 performs various operations of the method flow according to the embodiment of the present disclosure by executing the program in ROM 802 and / or RAM 803. It should be noted that the program can also be stored in one or more memories other than ROM 802 and RAM 803. Processor 801 can also perform various operations of the method flow according to the embodiment of the present disclosure by executing the program stored in the one or more memories.
[0124] According to an embodiment of the present disclosure, the system 800 may further include an input / output (I / O) interface 805, which is also connected to the bus 804. The system 800 may further include one or more of the following components connected to the I / O interface 805: an input section 806 including a keyboard, a mouse, etc.; an output section 807 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 808 including a hard disk, etc.; and a communication section 809 including a network interface card such as a LAN card, a modem, etc. The communication section 809 performs communication processing via a network such as the Internet. A drive 810 is also connected to the I / O interface 805 as needed. A removable medium 811, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 810 as needed, so that a computer program read therefrom is installed into the storage section 808 as needed.
[0125] According to an embodiment of the present disclosure, the method flow according to an embodiment of the present disclosure can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a computer-readable storage medium, and the computer program contains a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 809, and / or installed from the removable medium 811. When the computer program is executed by the processor 801, the above-mentioned functions defined in the system of the embodiment of the present disclosure are executed. According to an embodiment of the present disclosure, the system, equipment, device, module, unit, etc. described above can be implemented by a computer program module.
[0126] The present disclosure also provides a computer-readable storage medium, which may be included in the device / apparatus / system described in the above embodiments; or may exist independently without being assembled into the device / apparatus / system. The above computer-readable storage medium carries one or more programs, and when the above one or more programs are executed, the method according to the embodiment of the present disclosure is implemented.
[0127] According to an embodiment of the present disclosure, the computer-readable storage medium may be a non-volatile computer-readable storage medium, for example, may include but is not limited to: a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof. In the present disclosure, a computer-readable storage medium may be any tangible medium containing or storing a program, which may be used by or in combination with an instruction execution system, an apparatus or a device. For example, according to an embodiment of the present disclosure, a computer-readable storage medium may include the ROM 802 and / or RAM 803 described above and / or one or more memories other than ROM 802 and RAM 803.
[0128] The flow charts and block diagrams in the accompanying drawings illustrate the possible architecture, functions and operations of the systems, methods and computer program products according to various embodiments of the present disclosure. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0129] It will be appreciated by those skilled in the art that the features described in the various embodiments and / or claims of the present disclosure may be combined and / or combined in a variety of ways, even if such combinations and / or combinations are not explicitly described in the present disclosure. In particular, the features described in the various embodiments and / or claims of the present disclosure may be combined and / or combined in a variety of ways without departing from the spirit and teachings of the present disclosure. All of these combinations and / or combinations fall within the scope of the present disclosure.
[0130] The embodiments of the present disclosure are described above. However, these embodiments are only for illustrative purposes and are not intended to limit the scope of the present disclosure. Although the embodiments are described above separately, this does not mean that the measures in the various embodiments cannot be used in combination to advantage. The scope of the present disclosure is defined by the attached claims and their equivalents. Without departing from the scope of the present disclosure, those skilled in the art may make a variety of substitutions and modifications, which should all fall within the scope of the present disclosure.
Claims
1. A token bucket-based current limiting method, comprising: receiving a pending request, the pending request having a priority level, the priority level including a first priority level and a second priority level, and the first priority level is greater than the second priority level; Get the number of tokens in the token bucket; In a case where the number of tokens in the token bucket is less than the number of tokens required for the pending request, determining the target number of tokens according to the time when the tokens were last produced and the current time; wherein, in a case where the priority level represents the first priority, determining the target number of tokens according to the time when the tokens were last produced and the current time includes: When the number of token generation times is less than 1: Get the cumulative number of credit tokens and the number of single production tokens; In the case where the credit condition is met, determining the number of tokens required for the pending request as the target number of tokens, and determining the sum of the cumulative number of credit tokens and the number of tokens required for the pending request as a new cumulative number of credit tokens; and When the token generation number is greater than or equal to 1: Determine the number of tokens that can be produced according to the number of token production times and the number of tokens produced at a single time; Obtaining the accumulated credit token count; and In the case where the number of producible tokens is greater than the cumulative number of credit tokens, the difference between the number of producible tokens and the cumulative number of credit tokens is determined as the target number of tokens, and the cumulative number of credit tokens is set to 0; wherein the number of token production times is determined based on the time of the last token production and the current time; Adding the target number of tokens to the token bucket; and Whether to allow the pending request to pass is determined according to the number of tokens in the token bucket after the adding operation is performed.
2. The method according to claim 1, wherein: The step of determining the target number of tokens according to the time of the last token production and the current time includes: Determine the time difference between the time when the token was last generated and the current time; Determining the number of token generation times according to the time difference; and The target number of tokens is determined according to the number of times the tokens are produced.
3. The method according to claim 2, wherein: In the case where the priority level represents the second priority level, determining the target number of tokens according to the number of token production times includes: Get the number of tokens produced at a time; and The target number of tokens is determined according to the number of token production times and the number of tokens produced at a single time.
4. The method according to claim 1, wherein: The credit terms include: The sum of the accumulated number of credit tokens and the number of tokens required for the pending requests is less than the number of tokens produced at a single time; and / or The sum of the last credit token time and the credit interval is less than the last token generation time.
5. The method according to claim 1, wherein: The determining, according to the number of tokens in the token bucket after the adding operation is performed, whether to allow the pending request to pass, comprises: When the number of tokens in the token bucket is greater than or equal to the number of tokens required by the pending request, deduct the number of tokens required by the pending request from the token bucket to allow the pending request to pass; and When the number of tokens in the token bucket is less than the number of tokens required by the pending request, the pending request is rejected.
6. A current limiting device based on a token bucket, comprising: A receiving module, configured to receive a pending request, wherein the pending request has a priority level, the priority level includes a first priority level and a second priority level, and the first priority level is greater than the second priority level; An acquisition module is used to obtain the number of tokens in the token bucket; A first determination module is used to determine the target number of tokens according to the time of the last token production and the current time when the number of tokens in the token bucket is less than the number of tokens required for the pending request, wherein, when the priority level represents the first priority, the determination of the target number of tokens according to the time of the last token production and the current time includes: when the number of token production times is less than 1: obtaining the cumulative number of credit tokens and the number of tokens produced once; when the credit condition is met, determining the number of tokens required for the pending request as the target number of tokens, and determining the sum of the cumulative number of credit tokens and the number of tokens required for the pending request as the new cumulative number of credit tokens; and when the number of token production times is greater than or equal to 1: determining the number of producible tokens according to the number of token production times and the number of tokens produced once; obtaining the cumulative number of credit tokens; and when the number of producible tokens is greater than the cumulative number of credit tokens, determining the difference between the number of producible tokens and the cumulative number of credit tokens as the target number of tokens, and setting the cumulative number of credit tokens to 0; wherein, the number of token production times is determined according to the time of the last token production and the current time; an adding module, configured to add the target number of tokens to the token bucket; and The passing module is used to determine whether to allow the pending request to pass according to the number of tokens in the token bucket after the adding operation is performed.
7. The apparatus according to claim 6, wherein the first determining module comprises: A second determination submodule is used to determine the time difference between the time when the token was last generated and the current time; A third determination submodule is used to determine the number of token production times according to the time difference; as well as The fourth determination submodule is used to determine the target number of tokens according to the number of token production times.
8. A computing device comprising: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method of any one of claims 1 to 5.
9. A computer-readable storage medium having executable instructions stored thereon, which, when executed by a processor, causes the processor to implement the method of any one of claims 1 to 5.
Citation Information
Patent Citations
Bandwidth traffic monitoring method and system
CN107465630A
