Business request processing method and device, electronic equipment and storage medium
By pre-setting the relationship between the duration of a single token retrieval and the number of stored tokens based on changes in server processing capacity in the token bucket rate limiting method, the problem of insufficient rate limiting accuracy in existing technologies is solved, achieving more accurate rate limiting protection and avoiding server overload and business interruption.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- SHANGHAI ZHONG YUAN NETWORK CO LTD
- Filing Date
- 2023-04-25
- Publication Date
- 2026-04-17
AI Technical Summary
Existing token bucket rate limiting methods fail to effectively account for changes in server processing capacity, resulting in low rate limiting accuracy and an inability to effectively protect the server from sudden traffic surges.
Based on changes in server processing capacity, the relationship between the time taken to retrieve a single token and the number of stored tokens is preset. The token retrieval time is calculated using a function graph model to ensure that the rate limiting method adapts to the current processing capacity of the server. This includes a positive correlation between the time taken to retrieve a single token and the number of stored tokens when the number of stored tokens is greater than or equal to a preset threshold, and a constant value when the number of stored tokens is less than or equal to the preset threshold.
This improves the accuracy of rate limiting, avoids server overload and service cascading failures caused by sudden traffic surges, and ensures stable business operation.
Smart Images

Figure CN116527589B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a business request processing method, apparatus, electronic device, and storage medium. Background Technology
[0002] With the rapid development of computer technology, servers often face a large number of business requests. Therefore, to ensure server stability, rate limiting measures are usually implemented on the server side. Rate limiting is an effective measure to protect the server from overload, ensuring that the server can operate normally.
[0003] In existing technologies, token bucket rate limiting is commonly used. In token bucket rate limiting, the server generates tokens and puts them into the token bucket at a constant rate. After receiving a business request, a token must be taken from the token bucket and allocated to the business request before the request can be processed. If there are not enough tokens in the token bucket, the processing of the business request will be suspended.
[0004] However, in practical applications, the server's processing capacity is not constant. The token bucket rate limiting method mentioned above generates tokens at a constant rate to limit the rate, without taking into account changes in the server's processing capacity. Therefore, the accuracy of the token bucket rate limiting method in the existing technology is relatively low. Summary of the Invention
[0005] The purpose of this invention is to provide a service request processing method, apparatus, electronic device, and storage medium to improve the accuracy of server-side rate limiting. The specific technical solution is as follows:
[0006] In a first aspect of this invention, a business request processing method is provided, the method comprising:
[0007] Obtain the first number of tokens required for the pending business request and the second number of tokens currently stored;
[0008] Based on the first quantity, the second quantity, and the preset relationship information between the single token retrieval time and the number of stored tokens, the time required to retrieve the first quantity of tokens is calculated; the relationship information includes: when the number of stored tokens is greater than or equal to a preset threshold, the single token retrieval time is positively correlated with the number of stored tokens; when the number of stored tokens is less than or equal to the preset threshold, the single token retrieval time remains unchanged as the number of stored tokens changes.
[0009] After the specified delay, the first number of tokens are retrieved and allocated to the pending service request, and the pending service request is processed.
[0010] Optionally, the relationship information is a function graph, where the horizontal axis of the function graph represents the number of stored tokens, and the vertical axis represents the time required to retrieve a single token. The step of calculating the time required to retrieve the first number of tokens based on the first number, the second number, and a preset relationship between the time required to retrieve a single token and the number of stored tokens includes: calculating the remaining number after subtracting the first number from the second number; calculating the area enclosed by the function graph between the remaining number and the second number and the horizontal axis, and using the area as the time required to retrieve the first number of tokens.
[0011] Optionally, the function graph includes a hyperbolic tangent function portion, which indicates that when the number of stored tokens is greater than or equal to a preset threshold, the retrieval time of a single token is positively correlated with the number of stored tokens.
[0012] Optionally, the function graph includes a constant function portion, which indicates that when the number of stored tokens is less than or equal to the preset threshold, the duration of retrieving a single token remains constant as the number of stored tokens changes.
[0013] Optionally, calculating the area enclosed by the partial function graph between the remaining quantity and the second quantity and the horizontal axis includes: when both the remaining quantity and the second quantity are located in the hyperbolic tangent function portion, performing an integral calculation on the partial function graph to obtain the area enclosed by the partial function graph and the horizontal axis.
[0014] Optionally, calculating the area enclosed by the partial function graph between the remaining quantity and the second quantity and the horizontal axis includes: when both the remaining quantity and the second quantity are located in the constant function portion, calculating the area of the rectangle enclosed by the partial function graph and the horizontal axis to obtain the area enclosed by the partial function graph and the horizontal axis; when the remaining quantity is located in the constant function portion and the second quantity is located in the hyperbolic tangent function portion, performing an integral calculation on the image of the partial function graph located in the hyperbolic tangent function portion to obtain a first area, calculating the area of the rectangle enclosed by the image of the partial function graph located in the constant function portion and the horizontal axis to obtain a second area, and using the sum of the first area and the second area as the area enclosed by the partial function graph and the horizontal axis.
[0015] Optionally, before calculating the time required to retrieve the first number of tokens based on the first number, the second number, and the preset relationship information between the single token retrieval time and the number of stored tokens, the method further includes: determining whether the second number is greater than or equal to the first number; the step of calculating the time required to retrieve the first number of tokens based on the first number, the second number, and the preset relationship information between the single token retrieval time and the number of stored tokens includes: when the second number is greater than or equal to the first number, calculating the time required to retrieve the first number of tokens based on the first number, the second number, and the preset relationship information between the single token retrieval time and the number of stored tokens.
[0016] In a second aspect of the invention, a service request processing apparatus is also provided, the apparatus comprising:
[0017] The acquisition module is used to acquire the first number of tokens required for the pending business request and the second number of tokens currently stored.
[0018] The calculation module is used to calculate the time required to retrieve the first number of tokens based on the first number, the second number, and a preset relationship information between the retrieval time of a single token and the number of stored tokens; the relationship information includes: when the number of stored tokens is greater than or equal to a preset threshold, the retrieval time of a single token is positively correlated with the number of stored tokens; when the number of stored tokens is less than or equal to the preset threshold, the retrieval time of a single token remains unchanged as the number of stored tokens changes.
[0019] The processing module is configured to, after a delay of the specified duration, extract the first number of tokens and allocate them to the pending business request, and process the pending business request.
[0020] Optionally, the relationship information is a function graph, where the horizontal axis of the function graph represents the number of stored tokens, and the vertical axis represents the time required to retrieve a single token; the calculation module includes: a first calculation unit, used to calculate the remaining quantity after subtracting the first quantity from the second quantity; and a second calculation unit, used to calculate the area enclosed by the partial function graph between the remaining quantity and the second quantity and the horizontal axis, and to use the area as the time required to retrieve the first quantity of tokens.
[0021] Optionally, the function graph includes a hyperbolic tangent function portion, which indicates that when the number of stored tokens is greater than or equal to a preset threshold, the retrieval time of a single token is positively correlated with the number of stored tokens.
[0022] Optionally, the function graph includes a constant function portion, which indicates that when the number of stored tokens is less than or equal to the preset threshold, the duration of retrieving a single token remains constant as the number of stored tokens changes.
[0023] Optionally, the second calculation unit includes: a first calculation subunit, configured to perform integral calculation on the partial function image when both the remaining quantity and the second quantity are located in the hyperbolic tangent function portion, to obtain the area enclosed by the partial function image and the horizontal axis.
[0024] Optionally, the second calculation unit includes: a second calculation subunit, configured to calculate the area of the rectangle enclosed by the partial function image and the horizontal axis when both the remaining quantity and the second quantity are located in the constant function portion, to obtain the area enclosed by the partial function image and the horizontal axis; and a third calculation subunit, configured to perform integration calculation on the image located in the hyperbolic tangent function portion of the partial function image to obtain a first area when the remaining quantity is located in the constant function portion and the second quantity is located in the hyperbolic tangent function portion, calculate the area of the rectangle enclosed by the image located in the constant function portion of the partial function image and the horizontal axis to obtain a second area, and use the sum of the first area and the second area as the area enclosed by the partial function image and the horizontal axis.
[0025] Optionally, the device further includes: a judgment module, used to judge whether the second quantity is greater than or equal to the first quantity; and a calculation module, specifically used to calculate the time required to retrieve the first quantity of tokens based on the first quantity, the second quantity, and a preset relationship information between the single token retrieval time and the number of stored tokens when the judgment module judges that the second quantity is greater than or equal to the first quantity.
[0026] In another aspect of the present invention, an electronic device is also provided, including a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus; the memory is used to store computer programs; and the processor, when executing the program stored in the memory, implements any of the above-described business request processing methods.
[0027] In another aspect of the present invention, a computer-readable storage medium is also provided, wherein instructions are stored therein, which, when executed on a computer, cause the computer to implement any of the above-described business request processing methods.
[0028] In another aspect of the present invention, a computer program product containing instructions is also provided, which, when run on a computer, causes the computer to implement any of the above-described business request processing methods.
[0029] The business request processing method, apparatus, electronic device, and storage medium provided in this invention are based on the characteristic that the server's processing capacity is relatively poor when it first starts up or has not received business requests for a long time, but recovers and stabilizes at a normal level after a series of business request loading warm-ups. According to the changes in the server's processing capacity, the relationship between the single token retrieval time and the number of stored tokens is preset. The number of stored tokens represents the server's processing capacity; the larger the number of stored tokens, the fewer business requests the server is currently receiving, and the worse the server's processing capacity. The single token retrieval time represents the speed of retrieving tokens with a corresponding number of stored tokens; the larger the number of stored tokens, the worse the server's processing capacity, and the longer the single token retrieval time should be to better implement rate limiting. Therefore, when the number of stored tokens is greater than or equal to a preset threshold, the single token retrieval time is positively correlated with the number of stored tokens; when the number of stored tokens is less than or equal to the preset threshold, the single token retrieval time remains constant with changes in the number of stored tokens, thus allowing the single token retrieval time to change based on changes in the server's processing capacity. Upon receiving pending business requests, rate limiting is applied based on a pre-defined relationship between the time it takes to retrieve a single token and the number of stored tokens. This approach better adapts to the server's current processing capacity and improves the accuracy of rate limiting. By protecting the server through a cold-start rate limiting method that more closely approximates the server's actual load, it prevents the server from being overwhelmed by sudden traffic surges, which could lead to traffic overload, cascading service failures, and ultimately, the entire business from becoming unusable. Attached Figure Description
[0030] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the accompanying drawings used in the description of the embodiments or the prior art will be briefly introduced below.
[0031] Figure 1 This is a flowchart illustrating the steps of a business request processing method according to an embodiment of the present invention.
[0032] Figure 2 This is a schematic diagram of a function graph in an embodiment of the present invention.
[0033] Figure 3 This is a flowchart illustrating the steps of another business request processing method in an embodiment of the present invention.
[0034] Figure 4 This is a structural block diagram of a service request processing device according to an embodiment of the present invention.
[0035] Figure 5 This is a structural block diagram of an electronic device according to an embodiment of the present invention. Detailed Implementation
[0036] The technical solutions of the present invention will now be described with reference to the accompanying drawings in the embodiments of the present invention.
[0037] The embodiments in this invention are improvements on the token bucket rate limiting method, so that the rate limiting method can better adapt to changes in server processing capacity.
[0038] The token bucket rate limiting method refers to setting up a token bucket to store tokens. The server generates tokens and puts them into the token bucket at a constant rate. After receiving a business request, a token must be taken from the token bucket and allocated to the business request before the business request can be processed. If there are not enough tokens in the token bucket, the processing of the business request will be suspended.
[0039] However, in practical applications, the server's processing capacity is relatively poor when it first starts up or when it hasn't received any business requests for a long time. This is because the server's resources haven't been loaded and warmed up. After a series of business requests load and warm up, the server's processing capacity recovers and stabilizes at a normal level. Therefore, in the embodiments of this invention, based on the changes in the server's processing capacity, a pre-defined relationship information between the time it takes to retrieve a single token and the number of stored tokens is used, and rate limiting is performed based on this relationship information. This is described in detail below.
[0040] The business request processing method in this embodiment of the invention can be executed on the server side.
[0041] Figure 1 This is a flowchart illustrating the steps of a business request processing method according to an embodiment of the present invention.
[0042] like Figure 1 As shown, the business request processing method may include the following steps:
[0043] Step 101: Obtain the first number of tokens required for the pending business request and the second number of tokens currently stored.
[0044] For example, after receiving a pending business request, the server can obtain a first number of tokens required for the pending business request and a second number of currently stored tokens. Alternatively, the server can obtain the first number of tokens required for the pending business request and the second number of currently stored tokens at preset time intervals. The specific value of the time interval can be set according to actual needs; this embodiment does not impose any limitations on this.
[0045] The server obtains the number of tokens required for each pending business request and uses this number as the first quantity. In the token bucket rate limiting method, a pending business request needs to be allocated a token before it can continue to be processed; that is, one pending business request requires one token. Therefore, the server can obtain the number of pending business requests and use this number as the first quantity of tokens required for each pending business request.
[0046] The server retrieves the number of tokens currently stored in the token bucket and uses this number as the second quantity.
[0047] Step 102: Calculate the time required to retrieve the first number of tokens based on the first quantity, the second quantity, and the preset relationship information between the retrieval time of a single token and the number of stored tokens.
[0048] In this embodiment of the invention, considering the relationship between the server's processing capacity and the number of tokens stored in the token bucket, the number of stored tokens represents the server's processing capacity. The larger the number of stored tokens, the fewer business requests the server is currently receiving, and the worse the server's processing capacity is. The time taken to retrieve a single token represents the speed at which tokens are retrieved with the corresponding number of stored tokens. The larger the number of stored tokens, the worse the server's processing capacity is, and the longer the time taken to retrieve a single token should be to achieve greater rate limiting.
[0049] Therefore, based on the characteristic that the server's processing capacity is relatively poor when it is first started or when it has not received business requests for a long time, but recovers and stabilizes at a normal level after a series of business requests are loaded and warmed up, the relationship between the single token retrieval time and the number of stored tokens is set according to the changes in the server's processing capacity. This includes: when the number of stored tokens is greater than or equal to a preset threshold, the single token retrieval time is positively correlated with the number of stored tokens; when the number of stored tokens is less than or equal to the preset threshold, the single token retrieval time remains unchanged as the number of stored tokens changes. This allows the single token retrieval time to change based on the changes in the server's processing capacity.
[0050] In one alternative implementation, the relationship between the duration of a single token retrieval and the number of stored tokens can be a function graph of the duration of a single token retrieval with respect to the number of stored tokens, wherein the horizontal axis of the function graph represents the number of stored tokens and the vertical axis represents the duration of a single token retrieval.
[0051] When constructing a function graph of the single token retrieval time with respect to the number of stored tokens, the maximum value of the number of stored tokens, the single token retrieval time corresponding to the maximum value of the number of stored tokens, the preset threshold corresponding to the number of stored tokens, and the single token retrieval time corresponding to the preset threshold can be set according to actual needs. Based on the above preset values, a function graph can be constructed that shows that when the number of stored tokens is greater than or equal to the preset threshold, the single token retrieval time is positively correlated with the number of stored tokens, and when the number of stored tokens is less than or equal to the preset threshold, the single token retrieval time remains unchanged with the change of the number of stored tokens.
[0052] For example, the process of calculating the time required to retrieve the first number of tokens based on the first number, the second number, and the preset relationship information between the retrieval time of a single token and the number of stored tokens may include the following steps A1 to A2:
[0053] Step A1: Calculate the remaining quantity after subtracting the first quantity from the second quantity.
[0054] The first quantity refers to the number of tokens required for the pending business request, and the second quantity refers to the number of tokens currently stored in the token bucket. Therefore, the difference between the second quantity and the first quantity refers to the number of tokens remaining in the token bucket after the tokens required for the pending business request are taken from the tokens currently stored in the token bucket.
[0055] Step A2: Calculate the area enclosed by the partial function graph between the remaining quantity and the second quantity and the horizontal axis, and use the area as the time required to retrieve the first quantity of tokens.
[0056] From the function graph of the duration of retrieving a single token with respect to the number of stored tokens, a portion of the function graph between the remaining quantity and the second quantity is extracted. This portion of the function graph represents the change in the duration of retrieving a single token from the tokens currently stored in the token bucket for the service request to be processed (i.e., the aforementioned first quantity of tokens). In other words, each token in the first quantity of tokens to be retrieved has its own corresponding retrieval duration. By summing the retrieval durations of each token in the first quantity of tokens, the duration required to retrieve the first quantity of tokens can be obtained. Therefore, in this embodiment of the invention, the area enclosed by the partial function graph and the horizontal axis can be calculated, and the calculated area can be used as the duration required to retrieve the first quantity of tokens.
[0057] In one alternative implementation, the graph of the single token retrieval time as a function of the number of stored tokens needs to represent the following characteristics: when the number of stored tokens is greater than or equal to a preset threshold, the single token retrieval time is positively correlated with the number of stored tokens; when the number of stored tokens is less than or equal to the preset threshold, the single token retrieval time remains constant with changes in the number of stored tokens. Therefore, the function graph may include a hyperbolic tangent function component and a constant function component. The hyperbolic tangent function component can represent that when the number of stored tokens is greater than or equal to the preset threshold, the single token retrieval time is positively correlated with the number of stored tokens; the constant function component can represent that when the number of stored tokens is less than or equal to the preset threshold, the single token retrieval time remains constant with changes in the number of stored tokens.
[0058] The expression corresponding to the function graph of the single token retrieval time with respect to the number of stored tokens can be represented as follows:
[0059]
[0060] Where y represents the duration of retrieving a single token, and x represents the number of tokens stored. ′ Let x' represent the maximum preset number of stored tokens, x0 represent the preset threshold, B represent the single token retrieval time corresponding to the preset threshold, and A represent the coefficient of the hyperbolic tangent function. Assuming the single token retrieval time corresponding to the maximum preset number of stored tokens x' is D, then the coefficient A can be calculated using tanh(Ax')=D and tanh(Ax0)=B.
[0061] Reference Figure 2 The diagram illustrates a function graph according to an embodiment of the present invention. Figure 2 As shown in the graph, the graph represents the function of the time taken to retrieve a single token as a function of the number of stored tokens. The straight line represents the constant function, and the curved line represents the hyperbolic tangent function. The constant function and the hyperbolic tangent function intersect at a point, which corresponds to the preset threshold. The hyperbolic tangent function corresponds to the cold start phase of the server, during which the time taken to retrieve a single token decreases as the number of stored tokens decreases. The constant function corresponds to the stable phase after the server has completed its cold start, where the time taken to retrieve a single token remains constant.
[0062] For example, the process of calculating the area enclosed by the partial function graph between the remaining quantity and the second quantity and the horizontal axis can include the following cases:
[0063] (1) When both the remaining quantity and the second quantity are located in the hyperbolic tangent function portion, the partial function graph is integrated to obtain the area enclosed by the partial function graph and the horizontal axis.
[0064] For example, the integral formula of the hyperbolic tangent function shown below can be used to perform integral calculations on the graph of the partial function:
[0065]
[0066] Where C represents a preset constant.
[0067] (2) When both the remaining quantity and the second quantity are located in the constant function part, calculate the area of the rectangle enclosed by the partial function image and the horizontal axis to obtain the area enclosed by the partial function image and the horizontal axis.
[0068] For example, if the remaining quantity is x1, the second quantity is x2, and the constant function part is y = B, then the area of the rectangle enclosed by the graph of the partial function and the horizontal axis is: (x2 - x1) × B.
[0069] (3) When the remaining quantity is located in the constant function part and the second quantity is located in the hyperbolic tangent function part, the first area is obtained by integrating the image in the hyperbolic tangent function part of the partial function image, the area of the rectangle enclosed by the image in the constant function part of the partial function image and the horizontal axis is calculated to obtain the second area, and the sum of the first area and the second area is taken as the area enclosed by the partial function image and the horizontal axis.
[0070] For example, if the remaining quantity is x1, the second quantity is x2, and the preset threshold is x0, then for the function graph between x1 and x0, the area of the rectangle enclosed by the function graph and the horizontal axis is calculated to obtain the second area. For the function graph between x0 and x2, the first area is calculated by integration. Then, the sum of the first area and the second area is calculated, and the sum is used as the area enclosed by the partial function graph and the horizontal axis.
[0071] Step 103: After the delay period, the first number of tokens are taken out and allocated to the pending service request, and the pending service request is processed.
[0072] After calculating the time required to retrieve the first number of tokens, the first number of tokens is retrieved and allocated to the pending business requests after a delay of the specified time. The pending business requests are then processed. This allows for more accurate rate limiting based on the server's current processing capacity, building upon the token bucket rate limiting mechanism. When the server's current processing capacity is low, the time required to retrieve the first number of tokens will also be longer, thus reducing the server's processing load.
[0073] Figure 3 This is a flowchart illustrating the steps of another business request processing method in an embodiment of the present invention.
[0074] like Figure 3 As shown, the business request processing method may include the following steps:
[0075] Step 301: Obtain the first number of tokens required for the pending business request and the second number of currently stored tokens.
[0076] Step 302: Determine whether the second quantity is greater than or equal to the first quantity. If yes, proceed to step 303; otherwise, return to step 302.
[0077] The first quantity represents the number of tokens required for the pending business request, and the second quantity represents the number of tokens currently stored.
[0078] If the second quantity is greater than or equal to the first quantity, it means that the currently stored tokens can meet the needs of the pending business requests. In this case, step 303 can be executed to perform more accurate rate limiting based on the server's processing capacity.
[0079] If the second quantity is less than the first quantity, it means that the currently stored tokens cannot meet the needs of the pending business requests. In this case, it is necessary to wait for the server to generate tokens until the tokens stored in the token bucket can meet the needs of the pending business requests before continuing to execute step 303.
[0080] Step 303: Calculate the time required to retrieve the first number of tokens based on the first quantity, the second quantity, and the preset relationship information between the retrieval time of a single token and the number of stored tokens.
[0081] Step 304: After the delay period, the first number of tokens are taken out and allocated to the pending service request, and the pending service request is processed.
[0082] For the specific procedures of steps 303 and 304, please refer to the above. Figure 1 The relevant descriptions will not be discussed in detail in this embodiment.
[0083] Figure 4 This is a structural block diagram of a service request processing device according to an embodiment of the present invention.
[0084] like Figure 4 As shown, the service request processing device may include the following modules:
[0085] The acquisition module 401 is used to acquire the first number of tokens required for the pending business request and the second number of currently stored tokens;
[0086] The calculation module 402 is used to calculate the time required to retrieve the first number of tokens based on the first number, the second number, and a preset relationship information between the retrieval time of a single token and the number of stored tokens; the relationship information includes: when the number of stored tokens is greater than or equal to a preset threshold, the retrieval time of a single token is positively correlated with the number of stored tokens; when the number of stored tokens is less than or equal to the preset threshold, the retrieval time of a single token remains unchanged as the number of stored tokens changes.
[0087] The processing module 403 is used to, after the delay period, extract the first number of tokens and allocate them to the pending service request, and process the pending service request.
[0088] Optionally, the relationship information is a function graph, where the horizontal axis of the function graph represents the number of stored tokens, and the vertical axis represents the time required to retrieve a single token; the calculation module 402 includes: a first calculation unit, used to calculate the remaining quantity after subtracting the first quantity from the second quantity; and a second calculation unit, used to calculate the area enclosed by the partial function graph between the remaining quantity and the second quantity and the horizontal axis, and to use the area as the time required to retrieve the first quantity of tokens.
[0089] Optionally, the function graph includes a hyperbolic tangent function portion, which indicates that when the number of stored tokens is greater than or equal to a preset threshold, the retrieval time of a single token is positively correlated with the number of stored tokens.
[0090] Optionally, the function graph includes a constant function portion, which indicates that when the number of stored tokens is less than or equal to the preset threshold, the duration of retrieving a single token remains constant as the number of stored tokens changes.
[0091] Optionally, the second calculation unit includes: a first calculation subunit, configured to perform integral calculation on the partial function image when both the remaining quantity and the second quantity are located in the hyperbolic tangent function portion, to obtain the area enclosed by the partial function image and the horizontal axis.
[0092] Optionally, the second calculation unit includes: a second calculation subunit, configured to calculate the area of the rectangle enclosed by the partial function image and the horizontal axis when both the remaining quantity and the second quantity are located in the constant function portion, to obtain the area enclosed by the partial function image and the horizontal axis; and a third calculation subunit, configured to perform integration calculation on the image located in the hyperbolic tangent function portion of the partial function image to obtain a first area when the remaining quantity is located in the constant function portion and the second quantity is located in the hyperbolic tangent function portion, calculate the area of the rectangle enclosed by the image located in the constant function portion of the partial function image and the horizontal axis to obtain a second area, and use the sum of the first area and the second area as the area enclosed by the partial function image and the horizontal axis.
[0093] Optionally, the device further includes: a judgment module, used to judge whether the second quantity is greater than or equal to the first quantity; the calculation module 402 is specifically used to calculate the time required to retrieve the first quantity of tokens based on the first quantity, the second quantity, and a preset relationship information between the single token retrieval time and the number of stored tokens when the judgment module judges that the second quantity is greater than or equal to the first quantity.
[0094] In this embodiment of the invention, based on the characteristic that the server's processing capacity is relatively poor when it first starts up or has not received business requests for a long time, but recovers and stabilizes at a normal level after a series of business request loading warm-ups, the relationship between the single token retrieval time and the number of stored tokens is preset according to the changes in the server's processing capacity. The number of stored tokens represents the server's processing capacity; a larger number of stored tokens indicates fewer business requests currently being received by the server, and thus a poorer processing capacity. The single token retrieval time represents the speed of token retrieval with a given number of stored tokens; a larger number of stored tokens indicates a poorer processing capacity, and thus a longer single token retrieval time should be used to better implement rate limiting. Therefore, when the number of stored tokens is greater than or equal to a preset threshold, the single token retrieval time is positively correlated with the number of stored tokens; when the number of stored tokens is less than or equal to the preset threshold, the single token retrieval time remains constant regardless of the number of stored tokens, thereby allowing the single token retrieval time to change based on changes in the server's processing capacity. Upon receiving a pending business request, rate limiting is applied based on the preset relationship between the time it takes to retrieve a single token and the number of tokens stored. This approach better adapts to the server's current processing capacity and improves the accuracy of rate limiting.
[0095] This invention also provides an electronic device, such as... Figure 5As shown, it includes a processor 501, a communication interface 502, a memory 503, and a communication bus 504, wherein the processor 501, the communication interface 502, and the memory 503 communicate with each other through the communication bus 504.
[0096] Memory 503 is used to store computer programs;
[0097] When processor 501 executes the program stored in memory 503, it performs the following steps:
[0098] Obtain the first number of tokens required for the pending business request and the second number of tokens currently stored;
[0099] Based on the first quantity, the second quantity, and the preset relationship information between the single token retrieval time and the number of stored tokens, the time required to retrieve the first quantity of tokens is calculated; the relationship information includes: when the number of stored tokens is greater than or equal to a preset threshold, the single token retrieval time is positively correlated with the number of stored tokens; when the number of stored tokens is less than or equal to the preset threshold, the single token retrieval time remains unchanged as the number of stored tokens changes.
[0100] After the specified delay, the first number of tokens are retrieved and allocated to the pending service request, and the pending service request is processed.
[0101] Optionally, the relationship information is a function graph, where the horizontal axis of the function graph represents the number of stored tokens, and the vertical axis represents the time required to retrieve a single token. The step of calculating the time required to retrieve the first number of tokens based on the first number, the second number, and a preset relationship between the time required to retrieve a single token and the number of stored tokens includes: calculating the remaining number after subtracting the first number from the second number; calculating the area enclosed by the function graph between the remaining number and the second number and the horizontal axis, and using the area as the time required to retrieve the first number of tokens.
[0102] Optionally, the function graph includes a hyperbolic tangent function portion, which indicates that when the number of stored tokens is greater than or equal to a preset threshold, the retrieval time of a single token is positively correlated with the number of stored tokens.
[0103] Optionally, the function graph includes a constant function portion, which indicates that when the number of stored tokens is less than or equal to the preset threshold, the duration of retrieving a single token remains constant as the number of stored tokens changes.
[0104] Optionally, calculating the area enclosed by the partial function graph between the remaining quantity and the second quantity and the horizontal axis includes: when both the remaining quantity and the second quantity are located in the hyperbolic tangent function portion, performing an integral calculation on the partial function graph to obtain the area enclosed by the partial function graph and the horizontal axis.
[0105] Optionally, calculating the area enclosed by the partial function graph between the remaining quantity and the second quantity and the horizontal axis includes: when both the remaining quantity and the second quantity are located in the constant function portion, calculating the area of the rectangle enclosed by the partial function graph and the horizontal axis to obtain the area enclosed by the partial function graph and the horizontal axis; when the remaining quantity is located in the constant function portion and the second quantity is located in the hyperbolic tangent function portion, performing an integral calculation on the image of the partial function graph located in the hyperbolic tangent function portion to obtain a first area, calculating the area of the rectangle enclosed by the image of the partial function graph located in the constant function portion and the horizontal axis to obtain a second area, and using the sum of the first area and the second area as the area enclosed by the partial function graph and the horizontal axis.
[0106] Optionally, before calculating the time required to retrieve the first number of tokens based on the first number, the second number, and the preset relationship information between the single token retrieval time and the number of stored tokens, the method further includes: determining whether the second number is greater than or equal to the first number; the step of calculating the time required to retrieve the first number of tokens based on the first number, the second number, and the preset relationship information between the single token retrieval time and the number of stored tokens includes: when the second number is greater than or equal to the first number, calculating the time required to retrieve the first number of tokens based on the first number, the second number, and the preset relationship information between the single token retrieval time and the number of stored tokens.
[0107] The communication bus mentioned in the above terminal can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. This communication bus can be divided into address bus, data bus, control bus, etc. For ease of illustration, only one thick line is used to represent it in the diagram, but this does not mean that there is only one bus or one type of bus.
[0108] The communication interface is used for communication between the aforementioned terminal and other devices.
[0109] The memory may include random access memory (RAM) or non-volatile memory, such as at least one disk storage device. Optionally, the memory may also be at least one storage device located remotely from the aforementioned processor.
[0110] The processors mentioned above can be general-purpose processors, including central processing units (CPUs), network processors (NPs), etc.; they can also be digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.
[0111] In another embodiment of the present invention, a computer-readable storage medium is also provided, which stores instructions that, when executed on a computer, cause the computer to implement any of the business request processing methods described in the above embodiments.
[0112] In another embodiment of the present invention, a computer program product containing instructions is also provided, which, when run on a computer, enables the computer to implement any of the business request processing methods described in the above embodiments.
[0113] In the above embodiments, implementation can be achieved entirely or partially through software, hardware, firmware, or any combination thereof. When implemented using software, it can be implemented entirely or partially in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of the present invention are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that integrates one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid-state disk (SSD)).
[0114] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0115] The various embodiments in this specification are described in a related manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the system embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions of the method embodiments.
[0116] The above description is merely a preferred embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention are included within the scope of protection of the present invention.
Claims
1. A business request processing method, characterized in that, The method includes: Obtain the first number of tokens required for the pending business request and the second number of tokens currently stored; Based on the first quantity, the second quantity, and the preset relationship information between the single token retrieval time and the number of stored tokens, the time required to retrieve the first quantity of tokens is calculated; the relationship information includes: when the number of stored tokens is greater than or equal to a preset threshold, the single token retrieval time is positively correlated with the number of stored tokens; when the number of stored tokens is less than or equal to the preset threshold, the single token retrieval time remains unchanged as the number of stored tokens changes. After the specified delay, the first number of tokens are retrieved and allocated to the pending business request, and the pending business request is processed. Before calculating the time required to retrieve the first number of tokens based on the first number, the second number, and the preset relationship information between the retrieval time of a single token and the number of stored tokens, the method further includes: determining whether the second number is greater than or equal to the first number; The step of calculating the time required to retrieve the first number of tokens based on the first number, the second number, and the preset relationship information between the single token retrieval time and the number of stored tokens includes: when the second number is greater than or equal to the first number, calculating the time required to retrieve the first number of tokens based on the first number, the second number, and the preset relationship information between the single token retrieval time and the number of stored tokens.
2. The method according to claim 1, characterized in that, The relationship information is a function graph, where the horizontal axis represents the number of stored tokens and the vertical axis represents the time required to retrieve a single token. The step of calculating the time required to retrieve the first number of tokens based on the first number, the second number, and a preset relationship between the time required to retrieve a single token and the number of stored tokens includes: Calculate the remaining quantity after subtracting the first quantity from the second quantity; Calculate the area enclosed by the partial function graph between the remaining quantity and the second quantity and the horizontal axis, and use the area as the time required to retrieve the first quantity of tokens.
3. The method according to claim 2, characterized in that, The function graph includes a hyperbolic tangent function component, which indicates that when the number of stored tokens is greater than or equal to a preset threshold, the retrieval time of a single token is positively correlated with the number of stored tokens.
4. The method according to claim 3, characterized in that, The function graph includes a constant function portion, which indicates that when the number of stored tokens is less than or equal to the preset threshold, the time for retrieving a single token remains constant as the number of stored tokens changes.
5. The method according to claim 3, characterized in that, The calculation of the area enclosed by the partial function graph between the remaining quantity and the second quantity and the horizontal axis includes: When both the remaining quantity and the second quantity are located within the hyperbolic tangent function portion, the partial function graph is integrated to obtain the area enclosed by the partial function graph and the horizontal axis.
6. The method according to claim 4, characterized in that, The calculation of the area enclosed by the partial function graph between the remaining quantity and the second quantity and the horizontal axis includes: When both the remaining quantity and the second quantity are located within the constant function portion, the area of the rectangle enclosed by the partial function graph and the horizontal axis is calculated to obtain the area enclosed by the partial function graph and the horizontal axis. When the remaining quantity is located in the constant function portion and the second quantity is located in the hyperbolic tangent function portion, the first area is obtained by integrating the image in the partial function image located in the hyperbolic tangent function portion. The area of the rectangle enclosed by the image in the partial function image located in the constant function portion and the horizontal axis is calculated to obtain the second area. The sum of the first area and the second area is taken as the area enclosed by the partial function image and the horizontal axis.
7. A service request processing apparatus, characterized in that, The device includes: The acquisition module is used to acquire the first number of tokens required for the pending business request and the second number of tokens currently stored. The calculation module is used to calculate the time required to retrieve the first number of tokens based on the first number, the second number, and a preset relationship information between the retrieval time of a single token and the number of stored tokens; the relationship information includes: when the number of stored tokens is greater than or equal to a preset threshold, the retrieval time of a single token is positively correlated with the number of stored tokens; when the number of stored tokens is less than or equal to the preset threshold, the retrieval time of a single token remains unchanged as the number of stored tokens changes. The processing module is configured to, after the delay period, extract the first number of tokens and allocate them to the pending business request, and process the pending business request. The judgment module is used to determine whether the second quantity is greater than or equal to the first quantity; Specifically, the judgment module is used to determine whether the second quantity is greater than or equal to the first quantity before calculating the time required to retrieve the first quantity of tokens based on the first quantity, the second quantity, and the preset relationship information between the single token retrieval time and the number of stored tokens. The step of calculating the time required to retrieve the first number of tokens based on the first number, the second number, and the preset relationship information between the single token retrieval time and the number of stored tokens includes: when the second number is greater than or equal to the first number, calculating the time required to retrieve the first number of tokens based on the first number, the second number, and the preset relationship information between the single token retrieval time and the number of stored tokens.
8. An electronic device, characterized in that, It includes a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus; Memory, used to store computer programs; A processor, when executing a program stored in memory, implements the method as described in any one of claims 1-6.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1-6.
Citation Information
Patent Citations
Message sending method and device
CN103229465A
Method and device for network traffic limiting based on tokens
CN110838989A