Supporting dynamic configuration of fine-grained flow control methods and systems
By intercepting business requests and using the token bucket strategy to obtain tokens, and combining flow control annotations and configuration menus to achieve fine-grained flow control, it solves the problems of high interface flow control complexity and resource occupation in the existing technology, and improves the ability to cope with multiple scenarios. and interface service capabilities.
Patent Information
- Application Number
- CN202211325694.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-10-27
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2042-10-27
AI Technical Summary
The existing technology fails to provide a dynamic configuration optimization solution, resulting in high complexity of interface flow control, difficulty in meeting the needs of multiple scenarios, and high resource usage of traditional token bucket implementation.
By intercepting business requests, determine whether flow control is required, use the token bucket strategy to obtain the token, and process the request if successful, otherwise block it; use flow control annotations and configuration menus to achieve fine-grained flow control, combined with redis token bucket, optimization Token bucket update mechanism to reduce resource usage.
It implements dynamic configuration flow control, improves multi-scenario response capabilities, enhances interface service capabilities, optimizes the token bucket update mechanism, and reduces resource occupation.
Smart Images

Figure CN115801688B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of flow control technology, and in particular to a method and system for supporting dynamic configuration of fine-grained flow control. Background Art
[0002] Due to user experience, risk prevention and control and other reasons, the complexity of interfaces involved in system requests continues to increase. The system needs to increase its ability to cope with various scenarios, enhance interface service capabilities, and implement interface flow control.
[0003] Patent document CN101854402A discloses an interface conversion device and a flow control implementation method, the device comprising: a flow control state capture module, used to capture first flow control information of a first interface; a flow control conversion module, used to convert the first flow control information into second flow control information in a format supported by a second interface connected to the first interface; and a flow control module, used to control the second interface according to the second flow control information. However, the patent document does not provide an optimization solution for dynamic configuration. Summary of the invention
[0004] In view of the defects in the prior art, an object of the present invention is to provide a method and system that supports dynamic configuration of fine-grained flow control.
[0005] A method for supporting dynamic configuration of fine-grained flow control provided by the present invention includes:
[0006] The core steps of flow control: intercept the business request passing through the front-end system, obtain the interface address of the current business request; determine whether flow control restriction is required based on the interface address;
[0007] If flow control is required, the token is obtained according to the token bucket strategy indicated by the configuration data; if the token is successfully obtained, the business logic of the business request will continue to be processed; if the token is not successfully obtained, the business request will be blocked;
[0008] If flow control is not required, continue to process the business logic of the business request.
[0009] Preferably, in the fluid control core step:
[0010] Generate the corresponding URL address according to the business request, query the configuration data according to the URL address, and query whether the corresponding entry method has flow control annotations. If there are flow control annotations, it is considered that flow control is required, otherwise, it is considered that flow control is not required;
[0011] Obtain the corresponding token bucket strategy according to the token bucket type in the configuration data, generate the corresponding token bucket keyword key according to the token bucket restriction type and request address in the configuration data, and obtain the token in the current token bucket strategy; wherein the token bucket types include local token bucket and redis token bucket;
[0012] After blocking the request, the corresponding flow control prompt information is returned to the front-end page of the front-end system.
[0013] Preferably, it also includes:
[0014] Flow control annotation step: Use flow control annotations in the front-end system to annotate the entry method of the business page that needs flow control. When the front-end system starts, scan the entry method in the code. If the current entry method is annotated, add it to the mandatory annotation configuration flow control list, so as to obtain the list of all mandatory flow controls in the front-end system, and put the mandatory flow control list data into the memory; the annotation contains the flow control type; the flow control type includes the request address, IP, customer number, device number and other fine-grained types to specify the flow control granularity; among them, the customer number and IP limit the request frequency of the customer and IP respectively, and each customer has its own independent token bucket for the interface to be used for each customer's request flow control;
[0015] Flow control configuration steps: Perform interface flow control configuration through an independent configuration menu in the background system, provide flow control interface configuration and process strategy configuration; realize dynamic binding of process strategy configuration and flow control interface, and persist the binding relationship between process strategy configuration and flow control interface;
[0016] Flow control log step: record the flow control request address, IP, and user information parameters through the flow control log.
[0017] Preferably, the step of obtaining a token includes:
[0018] Step S1: call the Lua script, pass in parameters, including: token generation rate rate, token bucket capacity, current timestamp now, requested token quantity requested, token bucket keyword key, and execute the Lua script in the remote dictionary service redis;
[0019] Step S2: According to the input parameters, the time required to reach the maximum number fill_time is calculated through the token bucket capacity and the token generation rate rate:
[0020] fill_time = capacity / rate
[0021] The time required to reach the maximum number, fill_time*2, is set as the corresponding token bucket keyword key expiration time ttl:
[0022] ttl = fill_time * 2
[0023] Use the token bucket keyword key to query the current token bucket quantity last_tokens in the remote dictionary service redis; if the query result is empty, assign the current token bucket quantity last_tokens to the token bucket quantity capacity;
[0024] Query the last updated timestamp last_refreshed of the current token bucket in the remote dictionary service redis through the token bucket keyword key; if it is empty, assign the last updated timestamp last_refreshed of the current token bucket to 0;
[0025] Step S3: Calculate the call time interval delta by using the last updated timestamp last_refreshed of the current token bucket and the current timestamp now:
[0026] delta=math.max(0,now-last_refreshed)
[0027] Among them, the function math.max() means taking the maximum value;
[0028] Step S4: Calculate the number of tokens filled_tokens that need to be added to the token bucket through the token bucket capacity, token generation rate rate, current token bucket last_tokens, and call time interval delta:
[0029] filled_tokens=math.min(capacity, last_tokens+(delta*rate))
[0030] Among them, the function math.min() means taking the minimum value;
[0031] Step S5: Compare the requested token quantity requested and the final token quantity filled_tokens that needs to be added to the token bucket to obtain the comparison result allowed:
[0032] If the number of tokens that need to be added to the token bucket in the end, filled_tokens, is greater than or equal to the number of requested tokens, then it is marked as available tokens, and the current number of token buckets, last_tokens, is calculated, that is, the number of remaining tokens:
[0033] last_tokens=filled_tokens-requested
[0034] If the number of tokens that need to be added to the token bucket in the end, filled_tokens, is less than the number of requested tokens, it is marked as a failure to obtain tokens, and last_tokens is assigned to filled_tokens;
[0035] Step S6: store the remaining token number last_tokens and the current timestamp now in the remote dictionary service redis, and set the expiration time to the token bucket keyword key expiration time ttl.
[0036] According to the present invention, a system for supporting dynamic configuration of fine-grained flow control is provided, comprising:
[0037] Flow control core module: intercepts business requests passing through the front-end system, obtains the interface address of the current business request; determines whether flow control restriction is required based on the interface address;
[0038] If flow control is required, the token is obtained according to the token bucket strategy indicated by the configuration data; if the token is successfully obtained, the business logic of the business request will continue to be processed; if the token is not successfully obtained, the business request will be blocked;
[0039] If flow control is not required, continue to process the business logic of the business request.
[0040] Preferably, in the flow control core module:
[0041] Generate the corresponding URL address according to the business request, query the configuration data according to the URL address, and query whether the corresponding entry method has flow control annotations. If there are flow control annotations, it is considered that flow control is required, otherwise, it is considered that flow control is not required;
[0042] Obtain the corresponding token bucket strategy according to the token bucket type in the configuration data, generate the corresponding token bucket keyword key according to the token bucket restriction type and request address in the configuration data, and obtain the token in the current token bucket strategy; wherein the token bucket types include local token bucket and redis token bucket;
[0043] After blocking the request, the corresponding flow control prompt information is returned to the front-end page of the front-end system.
[0044] Preferably, it also includes:
[0045] Flow control annotation module: flow control annotation is used in the front-end system to mark the entry method of the business page that needs flow control. When the front-end system starts, the entry method in the code is scanned. If the current entry method is marked, it is added to the mandatory annotation configuration flow control list, so as to obtain the list of all mandatory flow controls in the front-end system, and put the mandatory flow control list data into the memory; the annotation contains the flow control type; the flow control type includes the request address, IP, customer number, device number and other fine-grained types to specify the flow control granularity; among them, the customer number and IP limit the request frequency of the customer and IP respectively, and each customer has its own independent token bucket for the interface to be used for each customer's request flow control;
[0046] Flow control configuration module: Perform interface flow control configuration through an independent configuration menu in the background system, provide flow control interface configuration and process strategy configuration; realize dynamic binding of process strategy configuration and flow control interface, and persist the binding relationship between process strategy configuration and flow control interface;
[0047] Flow control log module: records the parameters of flow control request address, IP, and user information through flow control log.
[0048] Preferably, the module for obtaining a token includes:
[0049] Module M1: Call the Lua script, pass in parameters, including: token generation rate rate, token bucket capacity, current timestamp now, requested token quantity requested, token bucket keyword key, and execute the Lua script in the remote dictionary service redis;
[0050] Module M2: According to the input parameters, the time required to reach the maximum number fill_time is calculated through the token bucket capacity and the token generation rate rate:
[0051] fill_time = capacity / rate
[0052] The time required to reach the maximum number, fill_time*2, is set as the corresponding token bucket keyword key expiration time ttl:
[0053] ttl = fill_time * 2
[0054] Use the token bucket keyword key to query the current token bucket quantity last_tokens in the remote dictionary service redis; if the query result is empty, assign the current token bucket quantity last_tokens to the token bucket quantity capacity;
[0055] Query the last updated timestamp last_refreshed of the current token bucket in the remote dictionary service redis through the token bucket keyword key; if it is empty, assign the last updated timestamp last_refreshed of the current token bucket to 0;
[0056] Module M3: Calculate the call time interval delta through the last update timestamp last_refreshed of the current token bucket and the current timestamp now:
[0057] delta=math.max(0,now-last_refreshed)
[0058] Among them, the function math.max() means taking the maximum value;
[0059] Module M4: Calculate the number of tokens filled_tokens that need to be added to the token bucket through the token bucket capacity, token generation rate rate, current token bucket last_tokens, and call time interval delta:
[0060] filled_tokens=math.min(capacity, last_tokens+(delta*rate))
[0061] Among them, the function math.min() means taking the minimum value;
[0062] Module M5: Compare the requested token quantity requested and the final number of tokens to be added to the token bucket filled_tokens to obtain the comparison result allowed:
[0063] If the number of tokens that need to be added to the token bucket in the end, filled_tokens, is greater than or equal to the number of requested tokens, then it is marked as available tokens, and the current number of token buckets, last_tokens, is calculated, that is, the number of remaining tokens:
[0064] last_tokens=filled_tokens-requested
[0065] If the number of tokens that need to be added to the token bucket in the end, filled_tokens, is less than the number of requested tokens, it is marked as a failure to obtain tokens, and last_tokens is assigned to filled_tokens;
[0066] Module M6: Store the remaining token number last_tokens and the current timestamp now in the remote dictionary service redis, and set the expiration time to the token bucket keyword key expiration time ttl.
[0067] According to a computer-readable storage medium storing a computer program provided by the present invention, when the computer program is executed by a processor, the steps of the method for supporting dynamic configuration of fine-grained flow control are implemented.
[0068] An electronic device provided according to the present invention includes the system supporting dynamic configuration of fine-grained flow control, or includes the computer-readable storage medium storing a computer program.
[0069] Compared with the prior art, the present invention has the following beneficial effects:
[0070] 1. The present invention provides a technical support component that supports dynamic configuration of flow control interface, multi-dimensional flow control configuration, and fine-grained flow control.
[0071] 2. The present invention implements flow control interface configuration through flow control annotation and flow control configuration. When receiving the corresponding business request, the request interface is verified according to the configuration data. If the request interface exists in the configuration data, an attempt is made to obtain a token according to the flow control parameters and flow control type of the configuration data. If the token is successfully obtained, the corresponding business method continues to be executed. Otherwise, the prompt information returned by the flow control core component is returned to the foreground page, thereby achieving the purpose of dynamically configuring flow control and dynamically configuring strategies.
[0072] 3. The present invention improves the coping capability for various scenarios, enhances the interface service capability, and realizes dynamic interface flow control.
[0073] 4. The present invention optimizes the update mechanism for obtaining the remaining token bucket. The traditional way to implement the token bucket is to set the maximum bucket capacity and require an independent thread to generate tokens, which will cause system resources to be occupied; the implementation scheme of redis obtaining the token bucket in the present invention adopts the method of calculating the time difference delta between two token acquisitions, delta = math.max(0,now-last_refreshed), and the number of newly added tokens is calculated by the product of the time difference and the generation rate delta*rate, so that no additional threads are needed to occupy resources. BRIEF DESCRIPTION OF THE DRAWINGS
[0074] Other features, objects and advantages of the present invention will become more apparent from the detailed description of non-limiting embodiments made with reference to the following drawings:
[0075] Figure 1 It is a schematic diagram of the flow control process of the present invention.
[0076] Figure 2 The figure is a schematic diagram of the process of obtaining a token in the flow control of the present invention. DETAILED DESCRIPTION
[0077] The present invention is described in detail below in conjunction with specific embodiments. The following embodiments will help those skilled in the art to further understand the present invention, but are not intended to limit the present invention in any form. It should be noted that, for those of ordinary skill in the art, several changes and improvements can also be made without departing from the concept of the present invention. These all belong to the protection scope of the present invention.
[0078] A method for supporting dynamic configuration of fine-grained flow control provided by the present invention includes:
[0079] Flow control annotation steps: implement the functions of marking mandatory flow control interfaces and identifying mandatory flow control interfaces. The front-end system uses flow control annotations to mark the entry methods of business pages that require flow control. When the front-end system starts, it scans the entry methods in the code. If the current entry method is marked, it is added to the mandatory annotation configuration flow control list, thereby obtaining a list of all mandatory flow controls in the front-end system and placing the mandatory flow control list data into memory. This is a fallback mechanism for process configuration, which is used to configure the system's mandatory flow control interfaces and implement mandatory flow control data collection. Among them, the annotations include token bucket type, flow control type, number of tokens produced per second, bucket capacity, etc. The token bucket type includes local token bucket and redis token bucket to choose from. The local token bucket is used for single-machine flow control, and the redis token bucket is used for distributed flow control to meet the flow control requirements in different scenarios; the flow control type includes fine-grained types such as request address, IP, customer number, and device number to accurately specify the flow control granularity, wherein the customer number and IP limit the request frequency of the customer and IP respectively, and each customer has its own independent token bucket for the interface to be used for the request flow control of each customer; the number of tokens produced per second, that is, the token generation rate rate, is used to specify the number of tokens added to the token bucket per second, similar to QPS; the bucket capacity is used to specify the maximum capacity of the current token bucket, similar to the maximum supported flow per second. The front-end system refers to a channel system open to the Internet. Among them, the present invention optimizes the interception mechanism of the distributed token bucket for interface granularity. The traditional token bucket is intercepted based on the interface request address. For Internet projects, what needs to be limited is not the number of requests of the interface, but the actual scenarios such as the request frequency of each customer or IP, etc., and the implementation of the traditional token bucket currently has no corresponding implementation. The present invention just implements support for this scenario. Each client has its own independent token bucket for the interface for request flow control of each client, which is more in line with actual application scenarios.
[0080] Flow control configuration steps: Implement the dynamic process policy configuration function of the background system. The background system performs interface flow control configuration through an independent configuration menu, and provides flow control interface configuration and process policy configuration functions. Implement dynamic binding of process policy configuration and flow control interface, and persist the binding relationship between process policy configuration and flow control interface, and provide modification and deletion configuration functions. Among them, the functional address supports fuzzy matching strategy and exact matching strategy, and the restriction type implements fine-grained flow control based on multiple dimensions such as request address, IP, user information, device code, etc. This module implements dynamic configuration of flow control data and sends it to the flow control core module in real time. The background system refers to the configuration system used by system administrators to maintain flow control interface configuration data and perform dynamic flow control configuration of the corresponding interface in real time.
[0081] Flow control log steps: implement the functions of recording triggered flow control logs, second-level monitoring logs, and regular business logs. The triggered flow control log records the flow control request address, IP, user information and other parameters; the second-level monitoring log records the configured flow control resource indicator data log, including the number of concurrent connections, processing time, number of exceptions, etc.; the regular business log records the call link log of the flow control interface, which is convenient for querying the actual situation of the flow control core module and business functions.
[0082] The core step of flow control: parse the user request and perform corresponding flow control. Create a request interceptor in the foreground system. When the business request passes through the request interceptor, the interface address of the current request is obtained. First, determine whether the interface address is subject to flow control restrictions, that is, whether flow control is required. Specifically, generate the corresponding URL address according to the request, query the configuration parameters according to the URL address, and query whether the corresponding entry method has flow control annotations. If flow control is required, obtain the corresponding token bucket policy processing method according to the token bucket type of the configuration data, and generate the corresponding token bucket keyword key according to the token bucket restriction type and request address in the configuration, and obtain the token in the current token bucket policy. If the token is successfully obtained, continue to process the original business logic, otherwise the request will be blocked and the corresponding flow control prompt information will be returned to the foreground page. Among them, the token bucket types include local token buckets and redis token buckets. If flow control is not required, continue to process the original business logic.
[0083] The present invention adopts a secondary cache token bucket configuration. In order to prevent high-frequency access to database configuration information, which leads to increased database pressure, when obtaining flow control configuration, by setting the secondary cache, first query whether there is corresponding configuration data from the local cache, and verify whether the corresponding obtained version is the latest version (and the version in the redis cache is verified). If it is not the latest version, the latest configuration information is obtained from the redis cache, and the local cache is updated. If the background management system updates the configuration information, it will synchronously update the latest configuration information to the redis cache, thereby ensuring that the data in the redis cache is consistent with the database.
[0084] The following is an explanation of the process of obtaining tokens from a distributed token bucket.
[0085] Step S1: Call the Lua script, pass in parameters including: token generation rate rate, token bucket quantity capacity, current timestamp now, requested token quantity requested, token bucket keyword key, and execute the Lua script in the remote dictionary service redis.
[0086] Step S2: According to the input parameters, the time required to reach the maximum number fill_time is calculated through the token bucket capacity and the token generation rate rate:
[0087] fill_time = capacity / rate
[0088] The time required to reach the maximum number, fill_time*2, is set as the corresponding token bucket keyword key expiration time ttl:
[0089] ttl = fill_time * 2
[0090] Use the token bucket keyword key to query the current token bucket quantity last_tokens in the remote dictionary service redis; if the query result is empty, assign the current token bucket quantity last_tokens to the token bucket quantity capacity;
[0091] Query the last updated timestamp last_refreshed of the current token bucket in the remote dictionary service redis through the token bucket keyword key; if it is empty, assign the last updated timestamp last_refreshed of the current token bucket to 0;
[0092] Step S3: Calculate the call time interval delta by using the last updated timestamp last_refreshed of the current token bucket and the current timestamp now:
[0093] delta=math.max(0,now-last_refreshed)
[0094] Among them, the function math.max() means taking the maximum value.
[0095] Step S4: Calculate the number of tokens filled_tokens that need to be added to the token bucket through the token bucket capacity, token generation rate rate, current token bucket last_tokens, and call time interval delta:
[0096] filled_tokens=math.min(capacity, last_tokens+(delta*rate))
[0097] Among them, the function math.min() means taking the minimum value.
[0098] Step S5: Compare the requested token quantity requested and the final token quantity filled_tokens that needs to be added to the token bucket to obtain the comparison result allowed:
[0099] If the number of tokens that need to be added to the token bucket in the end, filled_tokens, is greater than or equal to the number of requested tokens, it is marked as available tokens, and the remaining number of tokens, that is, the current number of token buckets, last_tokens, is calculated:
[0100] last_tokens=filled_tokens-requested
[0101] If the number of tokens that need to be added to the token bucket in the end, filled_tokens, is less than the number of requested tokens, it is marked as a failure to obtain tokens, and last_tokens is assigned = filled_tokens.
[0102] Step S6: store the remaining token number last_tokens and the current timestamp now in the remote dictionary service redis, and set the expiration time to the token bucket keyword key expiration time ttl.
[0103] In a variation, the present invention optimizes the traffic processing mechanism in the initialization phase of the distributed token bucket. Since the number of tokens in the traditional token bucket is not full during the initialization phase, it takes a certain amount of time to fill it through the generation rate, which will result in some requests that should be normally accessible being intercepted due to failure to obtain tokens during the initialization phase. The implementation scheme of the present invention is to use the preheating mode, from the first time the request enters the token bucket, the token bucket is full, so that normal requests will no longer be intercepted.
[0104] The present invention also provides a system for supporting dynamic configuration of fine-grained flow control. Those skilled in the art can implement the system for supporting dynamic configuration of fine-grained flow control by executing the process steps of the method for supporting dynamic configuration of fine-grained flow control, that is, the method for supporting dynamic configuration of fine-grained flow control can be understood as a preferred implementation of the system for supporting dynamic configuration of fine-grained flow control. Specifically, a system for supporting dynamic configuration of fine-grained flow control provided by the present invention includes:
[0105] Flow control core module: intercept business requests passing through the front-end system, obtain the interface address of the current business request; determine whether flow control restriction is required based on the interface address; if flow control is required, obtain the token according to the token bucket strategy indicated by the configuration data; if the token is successfully obtained, continue to process the business logic of the business request; if the token is not successfully obtained, block the business request; if flow control is not required, continue to process the business logic of the business request. In the flow control core module: generate the corresponding url address according to the business request, query the configuration data according to the url address, query whether the corresponding entry method has flow control annotations, if there are flow control annotations, it is considered that flow control is required, otherwise, it is considered that flow control is not required; obtain the corresponding token bucket strategy according to the token bucket type of the configuration data, and generate the corresponding token bucket keyword key according to the token bucket restriction type and request address in the configuration data, and obtain the token in the current token bucket strategy; wherein, the token bucket types include local token bucket and redis token bucket; after blocking the request, return the corresponding flow control prompt information to the front-end page of the front-end system.
[0106] Flow control annotation module: flow control annotation is used in the front-end system to annotate the entry method of the business page that needs flow control. When the front-end system starts, the entry method in the code is scanned. If the current entry method is annotated, it is added to the mandatory annotation configuration flow control list, so as to obtain the list of all mandatory flow controls in the front-end system, and put the mandatory flow control list data into the memory; the annotation contains the flow control type; the flow control type includes request address, IP, customer number, device number and other fine-grained types, which are optional to specify the flow control granularity;
[0107] Flow control configuration module: Perform interface flow control configuration through an independent configuration menu in the background system, provide flow control interface configuration and process strategy configuration; realize dynamic binding of process strategy configuration and flow control interface, and persist the binding relationship between process strategy configuration and flow control interface;
[0108] Flow control log module: records the parameters of flow control request address, IP, and user information through flow control log.
[0109] Preferably, the module for obtaining a token includes:
[0110] Module M1: Call the Lua script, pass in parameters, including: token generation rate rate, token bucket capacity, current timestamp now, requested token quantity requested, token bucket keyword key, and execute the Lua script in the remote dictionary service redis;
[0111] Module M2: According to the input parameters, the time required to reach the maximum number fill_time is calculated through the token bucket capacity and the token generation rate rate:
[0112] fill_time = capacity / rate
[0113] The time required to reach the maximum number, fill_time*2, is set as the corresponding token bucket keyword key expiration time ttl:
[0114] ttl = fill_time * 2
[0115] Use the token bucket keyword key to query the current token bucket quantity last_tokens in the remote dictionary service redis; if the query result is empty, assign the current token bucket quantity last_tokens to the token bucket quantity capacity;
[0116] Query the last updated timestamp last_refreshed of the current token bucket in the remote dictionary service redis through the token bucket keyword key; if it is empty, assign the last updated timestamp last_refreshed of the current token bucket to 0;
[0117] Module M3: Calculate the call time interval delta through the last update timestamp last_refreshed of the current token bucket and the current timestamp now:
[0118] delta=math.max(0,now-last_refreshed)
[0119] Among them, the function math.max() means taking the maximum value;
[0120] Module M4: Calculate the number of tokens filled_tokens that need to be added to the token bucket through the token bucket capacity, token generation rate rate, current token bucket last_tokens, and call time interval delta:
[0121] filled_tokens=math.min(capacity, last_tokens+(delta*rate))
[0122] Among them, the function math.min() means taking the minimum value;
[0123] Module M5: Compare the requested token quantity requested and the final number of tokens to be added to the token bucket filled_tokens to obtain the comparison result allowed:
[0124] If the number of tokens that need to be added to the token bucket in the end, filled_tokens, is greater than or equal to the number of requested tokens, then it is marked as available tokens, and the current number of token buckets, last_tokens, is calculated, that is, the number of remaining tokens:
[0125] last_tokens=filled_tokens-requested
[0126] If the number of tokens that need to be added to the token bucket in the end, filled_tokens, is less than the number of requested tokens, it is marked as a failure to obtain tokens, and last_tokens is assigned to filled_tokens;
[0127] Module M6: Store the remaining token number last_tokens and the current timestamp now in the remote dictionary service redis, and set the expiration time to the token bucket keyword key expiration time ttl.
[0128] Those skilled in the art know that, in addition to implementing the system, device and its various modules provided by the present invention in a purely computer-readable program code, it is entirely possible to implement the same program in the form of logic gates, switches, application-specific integrated circuits, programmable logic controllers and embedded microcontrollers by logically programming the method steps. Therefore, the system, device and its various modules provided by the present invention can be considered as a hardware component, and the modules included therein for implementing various programs can also be considered as structures within the hardware component; the modules for implementing various functions can also be considered as both software programs for implementing the method and structures within the hardware component.
[0129] According to a computer-readable storage medium storing a computer program provided by the present invention, when the computer program is executed by a processor, the steps of the method for supporting dynamic configuration of fine-grained flow control are implemented.
[0130] An electronic device provided according to the present invention includes the system supporting dynamic configuration of fine-grained flow control, or includes the computer-readable storage medium storing a computer program.
[0131] The above describes the specific embodiments of the present invention. It should be understood that the present invention is not limited to the above specific embodiments, and those skilled in the art can make various changes or modifications within the scope of the claims, which does not affect the essence of the present invention. In the absence of conflict, the embodiments of the present application and the features in the embodiments can be combined with each other arbitrarily.
Claims
1. A method for supporting dynamic configuration of fine-grained flow control, characterized in that: include: The core steps of flow control: intercept the business request passing through the front-end system and obtain the interface address of the current business request; Determine whether flow control restriction is required by determining the interface address; If flow control is required, tokens are obtained according to the token bucket strategy indicated by the configuration data; If the token is obtained successfully, the business logic of the business request will continue to be processed; if the token is not obtained successfully, the business request will be blocked; If flow control is not required, continue to process the business logic of the business request; In the flow control core step: Generate the corresponding URL address according to the business request, query the configuration data according to the URL address, and query whether the corresponding entry method has flow control annotations. If there are flow control annotations, it is considered that flow control is required, otherwise, it is considered that flow control is not required; Obtain the corresponding token bucket strategy according to the token bucket type in the configuration data, generate the corresponding token bucket keyword key according to the token bucket restriction type and request address in the configuration data, and obtain the token in the current token bucket strategy; wherein the token bucket types include local token bucket and redis token bucket; After blocking the request, the corresponding flow control prompt information is returned to the front page of the front-end system; Also includes: Flow control annotation step: Use flow control annotations in the front-end system to annotate the entry method of the business page that needs flow control. When the front-end system starts, scan the entry method in the code. If the current entry method is annotated, add it to the mandatory annotation configuration flow control list, so as to obtain the list of all mandatory flow controls in the front-end system, and put the mandatory flow control list data into the memory; the annotation contains the flow control type; the flow control type includes the request address, IP, customer number, device number and other fine-grained types to specify the flow control granularity; among them, the customer number and IP limit the request frequency of the customer and IP respectively, and each customer has its own independent token bucket for the interface to be used for each customer's request flow control; Flow control configuration steps: Perform interface flow control configuration through an independent configuration menu in the background system, provide flow control interface configuration and process strategy configuration; realize dynamic binding of process strategy configuration and flow control interface, and persist the binding relationship between process strategy configuration and flow control interface; Flow control log step: record the flow control request address, IP, and user information parameters through the flow control log; The steps to obtain a token include: Step S1: call the Lua script, pass in parameters, including: token generation rate rate, token bucket capacity, current timestamp now, requested token quantity requested, token bucket keyword key, and execute the Lua script in the remote dictionary service redis; Step S2: According to the input parameters, the time required to reach the maximum number fill_time is calculated through the token bucket capacity and the token generation rate rate: fill_time = capacity / rate The time required to reach the maximum number, fill_time*2, is set as the corresponding token bucket keyword key expiration time ttl: ttl = fill_time*2 Use the token bucket keyword key to query the current token bucket quantity last_tokens in the remote dictionary service redis; if the query result is empty, assign the current token bucket quantity last_tokens to the token bucket quantity capacity; Query the last updated timestamp last_refreshed of the current token bucket in the remote dictionary service redis through the token bucket keyword key; if it is empty, assign the last updated timestamp last_refreshed of the current token bucket to 0; Step S3: Calculate the call time interval delta by using the last updated timestamp last_refreshed of the current token bucket and the current timestamp now: delta = math.max(0,now - last_refreshed) Among them, the function math.max() means taking the maximum value; Step S4: Calculate the number of tokens filled_tokens that need to be added to the token bucket through the token bucket capacity, token generation rate rate, current token bucket last_tokens, and call time interval delta: filled_tokens = math.min(capacity, last_tokens+(delta*rate)) Among them, the function math.min() means taking the minimum value; Step S5: Compare the requested token quantity requested and the final number of tokens to be added to the token bucket filled_tokens to obtain the comparison result allowed: If the number of tokens that need to be added to the token bucket in the end, filled_tokens, is greater than or equal to the number of requested tokens, then it is marked as available tokens, and the current number of token buckets, last_tokens, is calculated: last_tokens=filled_tokens-requested If the number of tokens that need to be added to the token bucket in the end, filled_tokens, is less than the number of requested tokens, it is marked as a failure to obtain tokens, and last_tokens is assigned to filled_tokens; Step S6: store the remaining token number last_tokens and the current timestamp now in the remote dictionary service redis, and set the expiration time to the token bucket keyword key expiration time ttl.
2. A system supporting dynamic configuration of fine-grained flow control, characterized in that: include: Flow control core module: intercepts business requests passing through the front-end system and obtains the interface address of the current business request; Determine whether flow control restriction is required by determining the interface address; If flow control is required, tokens are obtained according to the token bucket strategy indicated by the configuration data; If the token is obtained successfully, the business logic of the business request will continue to be processed; if the token is not obtained successfully, the business request will be blocked; If flow control is not required, continue to process the business logic of the business request; In the flow control core module: Generate the corresponding URL address according to the business request, query the configuration data according to the URL address, and query whether the corresponding entry method has flow control annotations. If there are flow control annotations, it is considered that flow control is required, otherwise, it is considered that flow control is not required; Obtain the corresponding token bucket strategy according to the token bucket type in the configuration data, generate the corresponding token bucket keyword key according to the token bucket restriction type and request address in the configuration data, and obtain the token in the current token bucket strategy; wherein the token bucket types include local token bucket and redis token bucket; After blocking the request, the corresponding flow control prompt information is returned to the front page of the front-end system; Also includes: Flow control annotation module: flow control annotation is used in the front-end system to mark the entry method of the business page that needs flow control. When the front-end system starts, the entry method in the code is scanned. If the current entry method is marked, it is added to the mandatory annotation configuration flow control list, so as to obtain the list of all mandatory flow controls in the front-end system, and put the mandatory flow control list data into the memory; the annotation contains the flow control type; the flow control type includes the request address, IP, customer number, device number and other fine-grained types to specify the flow control granularity; among them, the customer number and IP limit the request frequency of the customer and IP respectively, and each customer has its own independent token bucket for the interface to be used for each customer's request flow control; Flow control configuration module: Perform interface flow control configuration through an independent configuration menu in the background system, provide flow control interface configuration and process strategy configuration; realize dynamic binding of process strategy configuration and flow control interface, and persist the binding relationship between process strategy configuration and flow control interface; Flow control log module: records the flow control request address, IP, and user information parameters through flow control logs; The modules for obtaining tokens include: Module M1: Call the Lua script, pass in parameters, including: token generation rate rate, token bucket capacity, current timestamp now, requested token quantity requested, token bucket keyword key, and execute the Lua script in the remote dictionary service redis; Module M2: According to the input parameters, the time required to reach the maximum number fill_time is calculated through the token bucket capacity and token generation rate rate: fill_time = capacity / rate The time required to reach the maximum number, fill_time*2, is set as the corresponding token bucket keyword key expiration time ttl: ttl = fill_time*2 Use the token bucket keyword key to query the current token bucket quantity last_tokens in the remote dictionary service redis; if the query result is empty, assign the current token bucket quantity last_tokens to the token bucket quantity capacity; Query the last updated timestamp last_refreshed of the current token bucket in the remote dictionary service redis through the token bucket keyword key; if it is empty, assign the last updated timestamp last_refreshed of the current token bucket to 0; Module M3: Calculate the call time interval delta through the last update timestamp last_refreshed of the current token bucket and the current timestamp now: delta = math.max(0,now - last_refreshed) Among them, the function math.max() means taking the maximum value; Module M4: Calculate the number of tokens filled_tokens that need to be added to the token bucket through the token bucket capacity, token generation rate rate, current token bucket last_tokens, and call time interval delta: filled_tokens = math.min(capacity, last_tokens+(delta*rate)) Among them, the function math.min() means taking the minimum value; Module M5: Compare the requested token quantity requested and the final token quantity filled_tokens that needs to be added to the token bucket to obtain the comparison result allowed: If the number of tokens that need to be added to the token bucket in the end, filled_tokens, is greater than or equal to the number of requested tokens, then it is marked as available tokens, and the current number of token buckets, last_tokens, is calculated: last_tokens=filled_tokens-requested If the number of tokens that need to be added to the token bucket in the end, filled_tokens, is less than the number of requested tokens, it is marked as a failure to obtain tokens, and last_tokens is assigned to filled_tokens; Module M6: Store the remaining token number last_tokens and the current timestamp now in the remote dictionary service redis, and set the expiration time to the token bucket keyword key expiration time ttl.
3. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the method for supporting dynamic configuration of fine-grained flow control described in claim 1 are implemented.
4. An electronic device, characterized in that: It includes the system supporting dynamic configuration of fine-grained flow control as described in claim 2, or it includes the computer-readable storage medium storing a computer program as described in claim 3.
Citation Information
Patent Citations
Interface conversion device and stream control implementing method
CN101854402A
System dynamic current limiting method and device, and storage medium
CN112333111A
Flow control method and device based on Redis, electronic equipment and storage medium
CN114915593A
