Task throttling method, device, equipment and storage medium
By introducing task object encapsulation and the creation of multiple queue patterns in the function throttling method, the problem of single function throttling function in the prior art is solved, efficient throttling processing for complex application scenarios is achieved, and development costs are reduced.
Patent Information
- Application Number
- CN202210280498.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-21
- Publication Date
- 2025-02-11
- Estimated Expiration
- 2042-03-21
AI Technical Summary
The function throttling function of the prior art is single, not suitable for complex application scenarios, and cannot effectively manage specific events with high frequency, such as network requests, data acquisition and interface refresh.
A task throttling method is proposed. By obtaining a function call request and determining whether there is a corresponding throttling queue in the preset throttling queue library, if it exists, the queue is used, and if it does not exist, a new one is created based on the call configuration and queue mode. throttling queue implements throttling processing of function call requests.
Different throttling schemes are implemented through queue mode, suitable for complex scenarios, and the throttling function is unified. There is no need to develop throttling function code for each function, which reduces development costs and increases the cost of high-frequency events Management efficiency.
Smart Images

Figure CN114610464B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of artificial intelligence technology, and in particular to a task throttling method, device, equipment and storage medium. Background Art
[0002] In the front end, the triggering of specific events such as network requests, data collection, file reading, and interface refresh usually occurs at a high frequency, which affects performance. If the frequency of calling and processing such specific events is unlimited, it will increase the burden on the browser or terminal and seriously affect the user experience. The existing technology uses function throttling to solve this problem, but the function throttling function is single and is not suitable for complex application scenarios. Summary of the invention
[0003] The main purpose of this application is to provide a task throttling method, device, equipment and storage medium, aiming to solve the technical problem that the function throttling in the prior art has a single function and is not suitable for complex application scenarios.
[0004] In order to achieve the above-mentioned invention object, the present application proposes a task throttling method, which comprises:
[0005] Obtaining a function call request, wherein the function call request carries a call configuration;
[0006] Determine whether there is a throttling queue corresponding to the calling configuration in the preset throttling queue library;
[0007] If it exists, the throttling queue corresponding to the calling configuration is used as the target throttling queue;
[0008] If not, obtaining a queue mode corresponding to the calling configuration from a preset throttling mode table as the target queue mode, and creating the throttling queue according to the calling configuration and the target queue mode as the target throttling queue;
[0009] Encapsulating the function call request into a task object to obtain a target task object;
[0010] The target task object is processed according to the target queue mode and each of the task objects in the target throttling queue.
[0011] Further, the step of obtaining a queue mode corresponding to the call configuration from a preset throttling mode table as a target queue mode, and creating the throttling queue according to the call configuration and the target queue mode as the target throttling queue includes:
[0012] Using the function identifier in the calling configuration to search the queue mode in the throttling mode table, the result is used as the target queue mode;
[0013] Adopting the target queue mode, obtaining a queue creation method from a preset queue creation method library as the target queue creation method;
[0014] The target queue creation method is adopted to create the throttling queue according to the calling configuration as the target throttling queue.
[0015] Furthermore, the step of processing the target task object according to the target queue mode and each of the task objects in the target throttling queue includes:
[0016] When the target queue mode is the first mode, determining whether the target throttling queue is empty;
[0017] If it is empty, the target task object is added to the tail of the target throttling queue, a function call is performed according to the target task object, the state identifier of the task object corresponding to the target task object in the target throttling queue is set as the execution identifier, and the first loop timer is started;
[0018] If it is not empty, determining whether the status identifier of the last task object in the target throttling queue is an execution identifier;
[0019] If yes, then adding the target task object to the tail of the target throttling queue;
[0020] If not, the last task object in the target throttling queue is discarded, and the target task object is added to the tail of the target throttling queue;
[0021] Among them, when the first cycle timer is triggered periodically, it is determined whether the status flag of the last task object in the target throttling queue is an unexecuted flag. If so, a function call is made to the last task object in the target throttling queue, and the status flag of the last task object in the target throttling queue is set to an executed flag.
[0022] Furthermore, the step of processing the target task object according to the target queue mode and each of the task objects in the target throttling queue further includes:
[0023] When the target queue mode is the second mode, determining whether the target throttling queue is empty;
[0024] If it is empty, the target task object is added to the tail of the target throttling queue, a function call is made according to the target task object, the state identifier of the task object corresponding to the target task object in the target throttling queue is set as the execution identifier, and a second loop timer is started;
[0025] If it is not empty, determining whether the cycle execution flag of the acquired i-1th throttling cycle is an executed flag, wherein the i-1th throttling cycle is the previous throttling cycle;
[0026] If it is not marked as executed, determining whether there is a task object of the i-th throttling cycle in the target throttling queue;
[0027] If it does not exist, the target task object is added to the tail of the target throttling queue, a function call is performed according to the target task object, the state flag of the task object corresponding to the target task object in the target throttling queue is set as the execution flag, and the cycle execution flag of the i-th throttling cycle is set as the executed flag;
[0028] If it exists, determine whether the status identifier of the last task object in the target throttling queue is an execution identifier. If it is an execution identifier, add the target task object to the tail of the target throttling queue. If it is a non-execution identifier, discard the last task object in the target throttling queue and add the target task object to the tail of the target throttling queue.
[0029] If it is marked as executed, determine whether the status identifier of the last task object in the target throttling queue is an execution identifier. If it is an execution identifier, add the target task object to the tail of the target throttling queue. If it is marked as not executed, discard the last task object in the target throttling queue and add the target task object to the tail of the target throttling queue.
[0030] Among them, when the second cycle timer is triggered in the i-th throttling cycle, it is determined whether the status flag of the last task object in the target throttling queue is an unexecuted flag. If so, a function call is performed according to the last task object in the target throttling queue, and the status flag of the last task object in the target throttling queue is set to an execution flag, and the cycle execution flag of the i-th throttling cycle is set to an executed flag.
[0031] Furthermore, the step of processing the target task object according to the target queue mode and each of the task objects in the target throttling queue further includes:
[0032] When the target queue mode is the third mode, determining whether the target throttling queue is empty;
[0033] If it is empty, adding the target task object to the tail of the target throttling queue, performing a function call according to the target task object, setting the state identifier of the task object corresponding to the target task object in the target throttling queue to the execution identifier, and starting the third loop timer;
[0034] If it is not empty, determine whether the cycle execution flag of the i-th throttling cycle is an executed flag, wherein the i-th throttling cycle is the current throttling cycle;
[0035] If it is marked as executed, the target task object is discarded;
[0036] If it is not marked as executed, the target task object is added to the tail of the target throttling queue, a function call is performed according to the target task object, the state flag of the task object corresponding to the target task object in the target throttling queue is set as the execution flag, and the cycle execution flag of the i-th throttling cycle is set as the executed flag;
[0037] When the third cycle timer is triggered in the i-th throttling cycle, the i+1-th throttling cycle is started, and the cycle execution mark of the i+1-th throttling cycle is set to a non-execution mark.
[0038] Furthermore, the step of processing the target task object according to the target queue mode and each of the task objects in the target throttling queue further includes:
[0039] When the target queue mode is the fourth mode, determining whether there is a first single timer in the preset first timer list;
[0040] If it does not exist, adding the target task object to the tail of the target throttling queue, performing a function call according to the target task object, setting the state identifier of the task object corresponding to the target task object in the target throttling queue to an execution identifier, creating the first one-shot timer, and adding the created first one-shot timer to the first timer list;
[0041] If it exists, determine whether the status identifier of the last task object in the target throttling queue is an execution identifier. If it is an execution identifier, add the target task object to the tail of the target throttling queue. If it is a non-execution identifier, discard the last task object in the target throttling queue and add the target task object to the tail of the target throttling queue.
[0042] Among them, when the first one-time timer is triggered, the first one-time timer is destroyed, and it is determined whether the status flag of the last task object in the target throttling queue is an unexecuted flag. If so, a function call is made to the last task object in the target throttling queue, and the status flag of the last task object in the target throttling queue is set to an executed flag.
[0043] Furthermore, the step of processing the target task object according to the target queue mode and each of the task objects in the target throttling queue further includes:
[0044] When the target queue mode is the fifth mode, determining whether there is a second single timer in the preset second timer list;
[0045] If it exists, destroy the second one-shot timer, discard the last task object in the target throttling queue, add the target task object to the tail of the target throttling queue, create the second one-shot timer according to the target task object, and add the created second one-shot timer to the second timer list;
[0046] If it does not exist, adding the target task object to the tail of the target throttling queue, creating the second one-shot timer according to the target task object, and adding the created second one-shot timer to the second timer list;
[0047] When the second one-shot timer is triggered, a function call is made to the last task object in the target throttling queue, the state flag of the last task object in the target throttling queue is set to an execution flag, and the second one-shot timer is destroyed.
[0048] The present application also proposes a task throttling device, the device comprising:
[0049] A request acquisition module, used to acquire a function call request, wherein the function call request carries a call configuration;
[0050] A judgment module, used for judging whether there is a throttling queue corresponding to the calling configuration in a preset throttling queue library;
[0051] a first target throttling queue determining module, configured to use the throttling queue corresponding to the calling configuration as the target throttling queue if it exists;
[0052] a second target throttling queue determination module, configured to obtain a queue mode corresponding to the calling configuration from a preset throttling mode table as a target queue mode if the queue mode does not exist, and to create the throttling queue according to the calling configuration and the target queue mode as the target throttling queue;
[0053] A target task object channel module is used to encapsulate the function call request into a task object to obtain a target task object;
[0054] The task object processing module is used to process the target task object according to the target queue mode and each task object in the target throttling queue.
[0055] The present application also proposes a computer device, comprising a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of any one of the above methods when executing the computer program.
[0056] The present application also proposes a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the steps of any of the above methods are implemented.
[0057] The task throttling method, device, equipment and storage medium of the present application, wherein the method implements different throttling schemes through a queue mode, thereby making the present application applicable to complex scenarios; through task object encapsulation, it is beneficial to implement the execution and status marking of function call requests based on a throttling queue; and the throttling function is uniformly implemented, without the need to develop throttling function code for each function, thereby reducing development costs. BRIEF DESCRIPTION OF THE DRAWINGS
[0058] Figure 1 A flowchart of a task throttling method according to an embodiment of the present application;
[0059] Figure 2 A schematic block diagram of the structure of a task throttling device according to an embodiment of the present application;
[0060] Figure 3 A schematic block diagram of the structure of a computer device according to an embodiment of the present application.
[0061] The realization of the purpose, functional features and advantages of this application will be further explained in conjunction with embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0062] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0063] Reference Figure 1 In an embodiment of the present application, a task throttling method is provided, the method comprising:
[0064] S1: Obtain a function call request, wherein the function call request carries a call configuration;
[0065] S2: Determine whether there is a throttling queue corresponding to the calling configuration in the preset throttling queue library;
[0066] S3: If it exists, taking the throttling queue corresponding to the calling configuration as the target throttling queue;
[0067] S4: if it does not exist, obtaining a queue mode corresponding to the calling configuration from a preset throttling mode table as the target queue mode, and creating the throttling queue according to the calling configuration and the target queue mode as the target throttling queue;
[0068] S5: encapsulating the function call request into a task object to obtain a target task object;
[0069] S6: Processing the target task object according to the target queue mode and each of the task objects in the target throttling queue.
[0070] This embodiment implements different throttling schemes through the queue mode, making the present application suitable for complex scenarios; through task object encapsulation, it is beneficial to implement the execution and status marking of function call requests based on the throttling queue; and the throttling function is implemented in a unified manner, without the need to develop throttling function code for each function, thereby reducing development costs.
[0071] It is understandable that the present application can be implemented as a private package, which is introduced when the application is packaged as an installation package, thereby packaging the private package implementing the present application into the installation package of the application, where the application includes but is not limited to: a client.
[0072] The client includes: a terminal of a mobile electronic device, a browser of a mobile electronic device, a terminal of a computer and a browser of a computer.
[0073] For S1, the function call request sent by the target application can be obtained.
[0074] A function call request is a request to call a function that requires throttling.
[0075] Functions that need to be throttled include: interval type functions and continuous call type functions. Interval type functions include but are not limited to: functions called by network requests, functions called by data collection, functions called by file reading, and functions called by interface refresh. Continuous call type functions include but are not limited to: functions called by data retrieval. It can be understood that continuous call type functions are also anti-shake type functions.
[0076] The call configuration includes: function identifier. The function identifier can be data that uniquely identifies a function, such as a function name or function ID.
[0077] Optionally, the calling configuration also includes: input parameter data.
[0078] For S2, the function identifier of the calling configuration is searched for in the throttling queue library. When the search is successful, it is determined that there is a throttling queue corresponding to the calling configuration in the throttling queue library. When the search fails, it is determined that there is no throttling queue corresponding to the calling configuration in the throttling queue library.
[0079] The throttling queue library includes: function identifier and throttling queue.
[0080] The throttling queue is a first-in-first-out queue.
[0081] For S3, if it exists, it means that the throttling queue corresponding to the calling configuration has been created, so the throttling queue corresponding to the calling configuration in the throttling queue library is used as the target throttling queue.
[0082] For S4, if it does not exist, it means that the throttling queue corresponding to the calling configuration has not been created, and the queue mode corresponding to the calling configuration is obtained from the preset throttling mode table, and the obtained queue mode is used as the target queue mode. The throttling queue is created according to the calling configuration and the target queue mode, and the created throttling queue is used as the target throttling queue.
[0083] The queue modes include: the first mode, the second mode, the third mode, the fourth mode and the fifth mode. The first mode is to set a timer with a fixed period, and execute the last unexecuted task object in the target throttling queue when the timer is triggered. The second mode is to set a timer with a fixed period, and when the timer is triggered, if the last task object in the target throttling queue has not been executed, it will be executed, and if the last task object in the target throttling queue has been executed, the first task object will be executed in the next throttling cycle. The third mode is to set a timer with a fixed period, and execute the first task object in each throttling cycle triggered by the timer, and discard the subsequent newly appearing task objects. The fourth mode is to destroy the unexecuted timer immediately when the timer is triggered, and execute the last unexecuted task object in the target throttling queue. The fifth model is that when a new task object appears, the untriggered timer is terminated, and a timer is created for the new task object. When the timer is triggered, the task object corresponding to the triggered timer will be executed.
[0084] For S5, a preset task object encapsulation method is used to encapsulate the function call request into a task object, and the encapsulated task object is used as the target task object.
[0085] The task object includes: a state flag and a closure of a function call, wherein the task flag is set as an unexecuted flag when encapsulating the task object. The closure of the function call of the target task object is the closure of the function that the calling configuration wants to call.
[0086] That is to say, the initial value of the status flag in the task object is the unexecuted flag. The unexecuted flag is used to indicate that the task object has not been executed.
[0087] Optionally, the closure of the function call also includes: the input parameter data in the call configuration.
[0088] For S6, according to the target queue mode and each of the task objects in the target throttling queue, the target task object is throttled. That is, the throttling includes: being executed immediately, being temporarily stored in the target throttling queue and then being discarded, being temporarily stored in the target throttling queue and then being executed, and being discarded.
[0089] In one embodiment, the step of obtaining the queue mode corresponding to the call configuration from the preset throttling mode table as the target queue mode, and creating the throttling queue according to the call configuration and the target queue mode as the target throttling queue includes:
[0090] S41: searching the queue mode in the throttling mode table for the function identifier in the calling configuration, and taking it as the target queue mode;
[0091] S42: adopting the target queue mode, obtaining a queue creation method from a preset queue creation method library as a target queue creation method;
[0092] S43: adopting the target queue creation method, creating the throttling queue according to the calling configuration as the target throttling queue.
[0093] This embodiment implements the creation of a throttling queue by using the queue creation method corresponding to the queue mode corresponding to the calling configuration, thereby implementing different throttling schemes through the queue mode and uniformly implementing the throttling function. There is no need to develop throttling function code for each function, thereby reducing development costs.
[0094] For S41, the function identifier in the calling configuration is used to search the queue mode in the throttling mode table, and the queue mode corresponding to the found function identifier in the throttling mode table is used as the target queue mode.
[0095] For S42, the target queue mode is adopted, a queue mode is searched from a preset queue creation method library, and a queue creation method corresponding to the found queue mode in the queue creation method library is obtained as the target queue creation method.
[0096] The queue creation method library includes: queue modes and queue creation methods.
[0097] For S43, the target queue creation method is used to create the throttling queue to obtain an initial throttling queue, the queue identifier of the initial throttling queue is set according to the function identifier of the call configuration, and the initial throttling queue with the set queue identifier is used as the target throttling queue.
[0098] In one embodiment, the step of processing the target task object according to the target queue mode and each of the task objects in the target throttling queue includes:
[0099] S611: When the target queue mode is the first mode, determine whether the target throttling queue is empty;
[0100] S612: if it is empty, adding the target task object to the tail of the target throttling queue, performing a function call according to the target task object, setting the state flag of the task object corresponding to the target task object in the target throttling queue to an execution flag, and starting a first loop timer;
[0101] S613: If it is not empty, determine whether the status flag of the last task object in the target throttling queue is an execution flag;
[0102] S614: If yes, then add the target task object to the tail of the target throttling queue;
[0103] S615: If not, discard the last task object in the target throttling queue and add the target task object to the tail of the target throttling queue;
[0104] Among them, when the first cycle timer is triggered periodically, it is determined whether the status flag of the last task object in the target throttling queue is an unexecuted flag. If so, a function call is made to the last task object in the target throttling queue, and the status flag of the last task object in the target throttling queue is set to an executed flag.
[0105] This embodiment implements setting a timer with a fixed period, and executes the last unexecuted task object in the target throttling queue when the timer is triggered. It is suitable for scenarios where the last unexecuted task object needs to be executed at the end of each throttling cycle, thereby ensuring that at most one task object is executed in each throttling cycle.
[0106] For S611, when the target queue mode is the first mode, if the target throttling queue includes a task object, it is determined that the target throttling queue is not empty; if the target throttling queue does not include a task object, it is determined that the target throttling queue is empty.
[0107] For S612, if it is empty, it means that the target throttling queue does not include a task object. Therefore, the target task object is first added to the tail of the target throttling queue, and then a function call is made according to the closure of the function call of the target task object. Finally, the state identifier of the task object corresponding to the target task object in the target throttling queue is set to the execution identifier, and the first loop timer is started, thereby starting the use of the target throttling queue.
[0108] The first cycle timer is a timer that is triggered cyclically at a first time interval.
[0109] For S613, if it is not empty, it means that the target throttling queue includes task objects. At this time, the target throttling queue already has a corresponding first cycle timer, so it is determined whether the status identifier of the last task object in the target throttling queue is an execution identifier.
[0110] The last task object in the target throttling queue is a task object in the target throttling queue that is closest to the tail of the target throttling queue.
[0111] For S614, if yes, it means that the status identifier of the last task object in the target throttling queue is an execution identifier, so the target task object is directly added to the tail of the target throttling queue, thereby realizing the storage of the target task object.
[0112] For S615, if no, it means that the status identifier of the last task object in the target throttling queue is not an execution identifier. Therefore, the last task object in the target throttling queue is first discarded, and then the target task object is added to the tail of the target throttling queue, so that the current throttling cycle always retains only one task object whose status identifier is not an execution identifier.
[0113] When the first cycle timer is triggered periodically, a new throttling cycle is started, and it is necessary to determine whether there is a task object in the previous throttling cycle that needs to perform a function call. Therefore, it is determined whether the status flag of the last task object in the target throttling queue is an unexecuted flag. If so, it means that there is a task object that needs to be executed. Therefore, a function call is made according to the closure of the function call of the last task object in the target throttling queue, and the status flag of the last task object in the target throttling queue is set to an execution flag, so as to timely update the status flag of the task objects in the target throttling queue.
[0114] In one embodiment, the step of processing the target task object according to the target queue mode and each of the task objects in the target throttling queue further includes:
[0115] S621: When the target queue mode is the second mode, determining whether the target throttling queue is empty;
[0116] S622: if it is empty, adding the target task object to the tail of the target throttling queue, performing a function call according to the target task object, setting the state flag of the task object corresponding to the target task object in the target throttling queue to an execution flag, and starting a second loop timer;
[0117] S623: If it is not empty, determine whether the cycle execution flag of the acquired i-1th throttling cycle is an executed flag, wherein the i-1th throttling cycle is the previous throttling cycle;
[0118] S624: If it is not marked as executed, determine whether there is a task object of the i-th throttling period in the target throttling queue;
[0119] S625: If it does not exist, then add the target task object to the tail of the target throttling queue, perform a function call according to the target task object, set the state flag of the task object corresponding to the target task object in the target throttling queue to the execution flag, and set the cycle execution flag of the i-th throttling cycle to the executed flag;
[0120] S626: If it exists, determine whether the status identifier of the last task object in the target throttling queue is an execution identifier. If it is an execution identifier, add the target task object to the tail of the target throttling queue. If it is a non-execution identifier, discard the last task object in the target throttling queue and add the target task object to the tail of the target throttling queue.
[0121] S627: If it is marked as executed, determine whether the status identifier of the last task object in the target throttling queue is an execution identifier. If it is an execution identifier, add the target task object to the tail of the target throttling queue. If it is marked as not executed, discard the last task object in the target throttling queue and add the target task object to the tail of the target throttling queue.
[0122] Among them, when the second cycle timer is triggered in the i-th throttling cycle, it is determined whether the status flag of the last task object in the target throttling queue is an unexecuted flag. If so, a function call is performed according to the last task object in the target throttling queue, and the status flag of the last task object in the target throttling queue is set to an execution flag, and the cycle execution flag of the i-th throttling cycle is set to an executed flag.
[0123] This embodiment implements a timer with a fixed period. When the timer is triggered, if the last task object in the target throttling queue has not been executed, the task object is executed. If the last task object in the target throttling queue has been executed, the first task object of the next throttling cycle is executed. This is suitable for scenarios where the last unexecuted task object needs to be executed at the end of each throttling cycle, and when the first throttling cycle of two adjacent throttling cycles has not executed a task object, the first task object is executed in the second throttling cycle.
[0124] For S621, when the target queue mode is the second mode, if the target throttling queue includes a task object, it is determined that the target throttling queue is not empty; if the target throttling queue does not include a task object, it is determined that the target throttling queue is empty.
[0125] For S622, if it is empty, it means that the target throttling queue does not include a task object. Therefore, the target task object is first added to the tail of the target throttling queue, and then a function call is made according to the closure of the function call of the target task object. Finally, the state identifier of the task object corresponding to the target task object in the target throttling queue is set to the execution identifier, and the second loop timer is started, thereby starting the use of the target throttling queue.
[0126] The second cycle timer is a timer that is triggered cyclically at a second time interval.
[0127] For S623, if it is not empty, it means that the target throttling queue includes the task object. At this time, the target throttling queue already has a corresponding second cycle timer. Therefore, it is determined whether the cycle execution mark of the i-1th throttling cycle is an executed mark to determine whether the task object has been executed in the previous throttling cycle.
[0128] Among them, the i-1th throttling cycle is the previous throttling cycle, the i-th throttling cycle is the current throttling cycle, and the i+1th throttling cycle is the next throttling cycle.
[0129] For S624, if it is not marked as executed, it means that the task object has not been executed in the previous throttling cycle. Therefore, it is determined whether there is a task object of the i-th throttling cycle in the target throttling queue to determine whether the task object has been executed in the current throttling cycle.
[0130] For S625, if it does not exist, it means that the task object has not been executed in the current throttling cycle. Therefore, the target task object is first added to the tail of the target throttling queue, and then a function call is made according to the closure of the function call of the target task object. Then, the state flag of the task object corresponding to the target task object in the target throttling queue is set to the execution flag, and finally, the cycle execution flag of the i-th throttling cycle is set to the executed flag, so as to realize that when the task object was not executed in the previous throttling cycle, the first task object is executed in the current throttling cycle.
[0131] The executed mark refers to the task object that has been executed during the throttling cycle.
[0132] It can be understood that the initial value of the cycle execution flag of the throttling cycle is the non-execution flag.
[0133] For S626, if it exists, it means that the task object has been executed in the current throttling cycle. Therefore, it is determined whether the status identifier of the last task object in the target throttling queue is an execution identifier; if it is an execution identifier, it means that the task object to be executed has not been added to the target throttling queue in the current throttling cycle, so the target task object is added to the tail of the target throttling queue; if it is an unexecuted identifier, it means that the task object to be executed has been added to the target throttling queue in the current throttling cycle, then the last task object in the target throttling queue is discarded, and the target task object is added to the tail of the target throttling queue, so that the target throttling queue always stores at most one unexecuted task object, and this task object is the task object added to the target throttling queue last.
[0134] For S627, if it is an executed mark, it means that the task object has been executed in the previous throttling cycle, and the current throttling cycle does not need to execute the first task object. Therefore, it is determined whether the status identifier of the last task object in the target throttling queue is an execution identifier; if it is an execution identifier, it means that the task object to be executed has not been added to the target throttling queue in the current throttling cycle, so the target task object is added to the tail of the target throttling queue; if it is an unexecuted identifier, it means that the task object to be executed has been added to the target throttling queue in the current throttling cycle, then the last task object in the target throttling queue is discarded, and the target task object is added to the tail of the target throttling queue, so that the target throttling queue always stores at most one unexecuted task object, and this task object is the task object added to the target throttling queue last.
[0135] When the second cycle timer is triggered in the i-th throttling cycle, the i+1-th throttling cycle is started, and it is necessary to determine whether there is a task object that needs to perform a function call in the i-th throttling cycle. Therefore, it is determined whether the status flag of the last task object in the target throttling queue is an unexecuted flag. If so, it means that there is a task object that needs to be executed. Therefore, a function call is made according to the closure of the function call of the last task object in the target throttling queue, and the status flag of the last task object in the target throttling queue is set to an execution flag, and the cycle execution mark of the i-th throttling cycle is set to an executed mark, so as to timely update the status flag of the task object in the target throttling queue and the cycle execution mark of the i-th throttling cycle.
[0136] In one embodiment, the step of processing the target task object according to the target queue mode and each of the task objects in the target throttling queue further includes:
[0137] S631: When the target queue mode is the third mode, determine whether the target throttling queue is empty;
[0138] S632: If it is empty, add the target task object to the tail of the target throttling queue, perform a function call according to the target task object, set the state flag of the task object corresponding to the target task object in the target throttling queue to the execution flag, and start the third loop timer;
[0139] S633: If it is not empty, determine whether the cycle execution flag of the i-th throttling cycle is an executed flag, wherein the i-th throttling cycle is the current throttling cycle;
[0140] S634: If the target task object is marked as executed, the target task object is discarded;
[0141] S635: If it is not marked as executed, the target task object is added to the tail of the target throttling queue, a function call is performed according to the target task object, the state flag of the task object corresponding to the target task object in the target throttling queue is set as the execution flag, and the cycle execution flag of the i-th throttling cycle is set as the executed flag;
[0142] When the third cycle timer is triggered in the i-th throttling cycle, the i+1-th throttling cycle is started, and the cycle execution mark of the i+1-th throttling cycle is set to a non-execution mark.
[0143] This embodiment implements a timer with a fixed period. When the timer is triggered and a new throttling period is started, the first task object of each throttling period is always executed. This is suitable for scenarios where the first task object needs to be executed at the end of each throttling period, thereby ensuring that at most one task object is executed in each throttling period.
[0144] For S631, when the target queue mode is the third mode, if the target throttling queue includes a task object, it is determined that the target throttling queue is not empty; if the target throttling queue does not include a task object, it is determined that the target throttling queue is empty.
[0145] For S632, if it is empty, it means that the target throttling queue does not include a task object. Therefore, the target task object is first added to the tail of the target throttling queue, and then a function call is made according to the closure of the function call of the target task object. Finally, the state identifier of the task object corresponding to the target task object in the target throttling queue is set to the execution identifier, and the third loop timer is started, thereby starting the use of the target throttling queue.
[0146] The third cycle timer is a timer that is cyclically triggered at a third time interval.
[0147] For S633, if it is not empty, it means that the target throttling queue includes the task object. At this time, the target throttling queue already has a corresponding third cycle timer. It is determined whether the cycle execution mark of the i-th throttling cycle is an executed mark to determine whether the task object has been executed in the current throttling cycle.
[0148] For S634, if it is marked as executed, it means that the task object has been executed in the current throttling cycle, and there is no need to execute the next task object received in the current throttling cycle, so the target task object is discarded.
[0149] For S635, if it is not marked as executed, it means that the task object has not been executed in the current throttling cycle, and the first task object received in the current throttling cycle needs to be executed. Therefore, the target task object is first added to the tail of the target throttling queue, and then a function call is made according to the closure of the function call of the target task object. Then, the state flag of the task object corresponding to the target task object in the target throttling queue is set to the execution flag, and finally, the cycle execution flag of the i-th throttling cycle is set to the executed flag, thereby realizing the execution of the first task object in the current throttling cycle.
[0150] Among them, when the third cycle timer is triggered in the i-th throttling cycle, it means that the current throttling cycle ends, so the i+1-th throttling cycle is started, and the cycle execution mark of the i+1-th throttling cycle is set to the unexecuted mark to realize the initialization of the cycle execution mark of the newly opened throttling cycle.
[0151] In one embodiment, the step of processing the target task object according to the target queue mode and each of the task objects in the target throttling queue further includes:
[0152] S641: When the target queue mode is the fourth mode, determine whether there is a first single timer in the preset first timer list;
[0153] S642: If it does not exist, add the target task object to the tail of the target throttling queue, perform a function call according to the target task object, set the state identifier of the task object corresponding to the target task object in the target throttling queue to an execution identifier, create the first one-shot timer, and add the created first one-shot timer to the first timer list;
[0154] S643: If it exists, determine whether the status identifier of the last task object in the target throttling queue is an execution identifier. If it is an execution identifier, add the target task object to the tail of the target throttling queue. If it is a non-execution identifier, discard the last task object in the target throttling queue and add the target task object to the tail of the target throttling queue.
[0155] Among them, when the first one-time timer is triggered, the first one-time timer is destroyed, and it is determined whether the status flag of the last task object in the target throttling queue is an unexecuted flag. If so, a function call is made to the last task object in the target throttling queue, and the status flag of the last task object in the target throttling queue is set to an executed flag.
[0156] This embodiment immediately executes the target task object and creates the first timer when the first timer does not exist. When the first timer exists, the last unexecuted task object of the current throttling cycle is retained. When the first timer is triggered, the first timer is destroyed, and the last unexecuted task object of the throttling cycle corresponding to the destroyed first timer is executed, thereby ensuring that at most one task object is executed in each throttling cycle.
[0157] For S641, when the target queue mode is the fourth mode, it is determined whether there is a first single timer in the preset first timer list, that is, it is determined whether the throttling cycle has been turned on.
[0158] The first one-shot timer is a one-shot timer that is destroyed after being executed once.
[0159] For S642, if it does not exist, it means that the throttling cycle has not been started. Therefore, the target task object is first added to the tail of the target throttling queue, and then a function call is made according to the closure of the function call of the target task object. Then, the state identifier of the task object corresponding to the target task object in the target throttling queue is set to the execution identifier. Finally, the first one-time timer is created, and the created first one-time timer is added to the first timer list, thereby starting the throttling cycle.
[0160] For S643, if it exists, it means that the throttling cycle has been started, therefore, it is determined whether the status identifier of the last task object in the target throttling queue is an execution identifier; if it is an execution identifier, it means that the task object to be executed has not been added to the target throttling queue in the current throttling cycle, therefore the target task object is added to the tail of the target throttling queue; if it is an unexecuted identifier, it means that the task object to be executed has been added to the target throttling queue in the current throttling cycle, then the last task object in the target throttling queue is discarded, and the target task object is added to the tail of the target throttling queue, so that the target throttling queue always stores at most one unexecuted task object, and this task object is the task object added to the target throttling queue last.
[0161] When the first one-shot timer is triggered, it means that the throttling cycle is over. Therefore, the first one-shot timer is first deleted from the first timer list to implement the destruction process, and then it is determined whether the status flag of the last task object in the target throttling queue is an unexecuted flag. If so, it means that there are unexecuted task objects in the throttling cycle. Therefore, a function call is made according to the closure of the function call of the last task object in the target throttling queue, and the status flag of the last task object in the target throttling queue is set to an execution flag.
[0162] In one embodiment, the step of processing the target task object according to the target queue mode and each of the task objects in the target throttling queue further includes:
[0163] S651: When the target queue mode is the fifth mode, determine whether there is a second single timer in the preset second timer list;
[0164] S652: If it exists, destroy the second one-shot timer, discard the last task object in the target throttling queue, add the target task object to the tail of the target throttling queue, create the second one-shot timer according to the target task object, and add the created second one-shot timer to the second timer list;
[0165] S653: If it does not exist, add the target task object to the tail of the target throttling queue, create the second one-shot timer according to the target task object, and add the created second one-shot timer to the second timer list;
[0166] When the second one-shot timer is triggered, a function call is made to the last task object in the target throttling queue, the state flag of the last task object in the target throttling queue is set to an execution flag, and the second one-shot timer is destroyed.
[0167] This embodiment destroys the untriggered second one-shot timer and creates a new second one-shot timer for the new task object, so that the task object is executed only when the pause duration of the function call exceeds the timing duration corresponding to the second one-shot timer, thereby achieving anti-shake.
[0168] For S651, when the target queue mode is the fourth mode, it is determined whether there is a second single timer in the preset second timer list, that is, it is determined whether the throttling cycle has been turned on.
[0169] The second one-shot timer is a one-shot timer that is destroyed after being executed once.
[0170] For S652, if it exists, it means that the throttling cycle has been started, but the throttling cycle has not ended. Therefore, the second single timer is first deleted from the second timer list to achieve destruction processing, and the last task object in the target throttling queue is discarded. Then the target task object is added to the tail of the target throttling queue, and the second single timer is created according to the target task object. The created second single timer is added to the second timer list to create a second single timer for the new task object to start a new throttling cycle. Thereby, the target throttling queue always retains only one task object whose status identifier is not an execution identifier.
[0171] For S653, if it does not exist, it means that the throttling cycle is not started. Therefore, the target task object is added to the tail of the target throttling queue, and the second one-shot timer is created according to the target task object, and the created second one-shot timer is added to the second timer list to start the throttling cycle.
[0172] When the second one-shot timer is triggered, it means that the throttling cycle ends. Therefore, the second one-shot timer is first deleted from the second timer list to implement the destruction process, and then a function call is made according to the closure of the function call of the last task object in the target throttling queue, and the state flag of the last task object in the target throttling queue is set to the execution flag.
[0173] When the user enters a search keyword in the search input box, the change in the input text will send a search request, and the returned search prompt will be given to the user in the drop-down box. If the user keeps changing the search keyword quickly, a large number of search requests will be triggered, and the backend search service may not be able to withstand the huge number of search requests, causing the service to crash. Steps S651 to S653 can solve this problem.
[0174] For example, the duration of the second single timer is 0.5 seconds. Through steps S651 to S653, the last task object in the target throttling queue will be executed only when the user pauses for more than 0.5 seconds. If the user inputs continuously and the pause does not exceed 0.5 seconds, the search request will be postponed until the user pauses for more than 0.5 seconds.
[0175] Reference Figure 2 , the present application also proposes a task throttling device, the device comprising:
[0176] The request acquisition module 100 is used to acquire a function call request, wherein the function call request carries a call configuration;
[0177] A determination module 200 is used to determine whether there is a throttling queue corresponding to the calling configuration in a preset throttling queue library;
[0178] A first target throttling queue determination module 300, configured to use the throttling queue corresponding to the calling configuration as a target throttling queue if it exists;
[0179] A second target throttling queue determination module 400 is used to obtain a queue mode corresponding to the calling configuration from a preset throttling mode table as a target queue mode if it does not exist, and create the throttling queue according to the calling configuration and the target queue mode as the target throttling queue;
[0180] The target task object channel module 500 is used to encapsulate the function call request into a task object to obtain a target task object;
[0181] The task object processing module 600 is used to process the target task object according to the target queue mode and each of the task objects in the target throttling queue.
[0182] This embodiment implements different throttling schemes through the queue mode, making the present application suitable for complex scenarios; through task object encapsulation, it is beneficial to implement the execution and status marking of function call requests based on the throttling queue; and the throttling function is implemented in a unified manner, without the need to develop throttling function code for each function, thereby reducing development costs.
[0183] Reference Figure 3In an embodiment of the present application, a computer device is also provided. The computer device may be a server, and its internal structure may be as follows: Figure 3 As shown. The computer device includes a processor, a memory, a network interface and a database connected through a system bus. Among them, the processor designed by the computer is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store data such as task throttling methods. The network interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a task throttling method is implemented. The task throttling method includes: obtaining a function call request, wherein the function call request carries a call configuration; determining whether a throttling queue corresponding to the call configuration exists in a preset throttling queue library; if so, using the throttling queue corresponding to the call configuration as a target throttling queue; if not, obtaining a queue mode corresponding to the call configuration from a preset throttling mode table as a target queue mode, creating the throttling queue according to the call configuration and the target queue mode as the target throttling queue; encapsulating the function call request into a task object to obtain a target task object; and processing the target task object according to the target queue mode and each of the task objects in the target throttling queue.
[0184] This embodiment implements different throttling schemes through the queue mode, making the present application suitable for complex scenarios; through task object encapsulation, it is beneficial to implement the execution and status marking of function call requests based on the throttling queue; and the throttling function is implemented in a unified manner, without the need to develop throttling function code for each function, thereby reducing development costs.
[0185] An embodiment of the present application also provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, a task throttling method is implemented, comprising the steps of: obtaining a function call request, wherein the function call request carries a call configuration; determining whether a throttling queue corresponding to the call configuration exists in a preset throttling queue library; if so, taking the throttling queue corresponding to the call configuration as a target throttling queue; if not, obtaining a queue mode corresponding to the call configuration from a preset throttling mode table as a target queue mode, creating the throttling queue according to the call configuration and the target queue mode as the target throttling queue; encapsulating the function call request as a task object to obtain a target task object; and processing the target task object according to the target queue mode and each of the task objects in the target throttling queue.
[0186] The task throttling method executed above implements different throttling schemes through the queue mode, making the present application suitable for complex scenarios; through task object encapsulation, it is conducive to the execution and status marking of function call requests based on the throttling queue; and the throttling function is implemented in a unified manner, without the need to develop throttling function code for each function, thereby reducing development costs.
[0187] Those of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media provided in this application and used in the embodiments may include non-volatile and / or volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (SSRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.
[0188] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, device, article or method including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, device, article or method. In the absence of further restrictions, an element defined by the sentence "includes a ..." does not exclude the existence of other identical elements in the process, device, article or method including the element.
[0189] The above description is only a preferred embodiment of the present application, and does not limit the patent scope of the present application. Any equivalent structure or equivalent process transformation made using the contents of the present application specification and drawings, or directly or indirectly applied in other related technical fields, are also included in the patent protection scope of the present application.
Claims
1. A task throttling method, characterized in that: The method comprises: Obtaining a function call request, wherein the function call request carries a call configuration; the call configuration corresponds to a queue mode, and different queue modes implement different throttling schemes; Determine whether there is a throttling queue corresponding to the calling configuration in the preset throttling queue library; If it exists, the throttling queue corresponding to the calling configuration is used as the target throttling queue; If not, obtaining a queue mode corresponding to the calling configuration from a preset throttling mode table as the target queue mode, and creating the throttling queue according to the calling configuration and the target queue mode as the target throttling queue; Encapsulating the function call request into a task object to obtain a target task object; According to the target queue mode and each of the task objects in the target throttling queue, the target task object is processed according to the throttling scheme corresponding to the target queue mode.
2. The task throttling method according to claim 1, characterized in that: The step of obtaining a queue mode corresponding to the calling configuration from a preset throttling mode table as a target queue mode, and creating the throttling queue according to the calling configuration and the target queue mode as the target throttling queue includes: Using the function identifier in the calling configuration to search the queue mode in the throttling mode table, the queue mode is used as the target queue mode; Adopting the target queue mode, obtaining a queue creation method from a preset queue creation method library as the target queue creation method; The target queue creation method is adopted to create the throttling queue according to the calling configuration as the target throttling queue.
3. The task throttling method according to claim 1, characterized in that: The step of processing the target task object according to the target queue mode and each of the task objects in the target throttling queue according to the throttling scheme corresponding to the target queue mode includes: When the target queue mode is the first mode, determining whether the target throttling queue is empty; If it is empty, the target task object is added to the tail of the target throttling queue, a function call is performed according to the target task object, the state identifier of the task object corresponding to the target task object in the target throttling queue is set as the execution identifier, and the first loop timer is started; If it is not empty, determining whether the status identifier of the last task object in the target throttling queue is an execution identifier; If yes, then adding the target task object to the tail of the target throttling queue; If not, the last task object in the target throttling queue is discarded, and the target task object is added to the tail of the target throttling queue; Among them, when the first cycle timer is triggered periodically, it is determined whether the status flag of the last task object in the target throttling queue is an unexecuted flag. If so, a function call is made to the last task object in the target throttling queue, and the status flag of the last task object in the target throttling queue is set to an executed flag.
4. The task throttling method according to claim 1, characterized in that: The step of processing the target task object according to the target queue mode and each of the task objects in the target throttling queue according to the throttling scheme corresponding to the target queue mode also includes: When the target queue mode is the second mode, determining whether the target throttling queue is empty; If it is empty, the target task object is added to the tail of the target throttling queue, a function call is made according to the target task object, the state identifier of the task object corresponding to the target task object in the target throttling queue is set as the execution identifier, and a second loop timer is started; If it is not empty, determining whether the cycle execution flag of the acquired i-1th throttling cycle is an executed flag, wherein the i-1th throttling cycle is the previous throttling cycle; If it is not marked as executed, determining whether there is a task object of the i-th throttling cycle in the target throttling queue; If it does not exist, the target task object is added to the tail of the target throttling queue, a function call is performed according to the target task object, the state flag of the task object corresponding to the target task object in the target throttling queue is set as the execution flag, and the cycle execution flag of the i-th throttling cycle is set as the executed flag; If it exists, determine whether the status identifier of the last task object in the target throttling queue is an execution identifier. If it is an execution identifier, add the target task object to the tail of the target throttling queue. If it is a non-execution identifier, discard the last task object in the target throttling queue and add the target task object to the tail of the target throttling queue. If it is marked as executed, determine whether the status identifier of the last task object in the target throttling queue is an execution identifier. If it is an execution identifier, add the target task object to the tail of the target throttling queue. If it is marked as not executed, discard the last task object in the target throttling queue and add the target task object to the tail of the target throttling queue. Among them, when the second cycle timer is triggered in the i-th throttling cycle, it is determined whether the status flag of the last task object in the target throttling queue is an unexecuted flag. If so, a function call is performed according to the last task object in the target throttling queue, and the status flag of the last task object in the target throttling queue is set to an execution flag, and the cycle execution flag of the i-th throttling cycle is set to an executed flag.
5. The task throttling method according to claim 1, characterized in that: The step of processing the target task object according to the target queue mode and each of the task objects in the target throttling queue according to the throttling scheme corresponding to the target queue mode also includes: When the target queue mode is the third mode, determining whether the target throttling queue is empty; If it is empty, adding the target task object to the tail of the target throttling queue, performing a function call according to the target task object, setting the state identifier of the task object corresponding to the target task object in the target throttling queue to the execution identifier, and starting the third loop timer; If it is not empty, determine whether the cycle execution mark of the i-th throttling cycle is an executed mark, wherein the i-th throttling cycle is the current throttling cycle; If it is marked as executed, the target task object is discarded; If it is not marked as executed, the target task object is added to the tail of the target throttling queue, a function call is performed according to the target task object, the state flag of the task object corresponding to the target task object in the target throttling queue is set as the execution flag, and the cycle execution flag of the i-th throttling cycle is set as the executed flag; When the third cycle timer is triggered in the i-th throttling cycle, the i+1-th throttling cycle is started, and the cycle execution mark of the i+1-th throttling cycle is set to a non-execution mark.
6. The task throttling method according to claim 1, characterized in that: The step of processing the target task object according to the target queue mode and each of the task objects in the target throttling queue according to the throttling scheme corresponding to the target queue mode also includes: When the target queue mode is the fourth mode, determining whether there is a first single timer in a preset first timer list; If it does not exist, adding the target task object to the tail of the target throttling queue, performing a function call according to the target task object, setting the state identifier of the task object corresponding to the target task object in the target throttling queue to an execution identifier, creating the first one-shot timer, and adding the created first one-shot timer to the first timer list; If it exists, determine whether the status identifier of the last task object in the target throttling queue is an execution identifier. If it is an execution identifier, add the target task object to the tail of the target throttling queue. If it is a non-execution identifier, discard the last task object in the target throttling queue and add the target task object to the tail of the target throttling queue. Among them, when the first one-time timer is triggered, the first one-time timer is destroyed, and it is determined whether the status flag of the last task object in the target throttling queue is an unexecuted flag. If so, a function call is made to the last task object in the target throttling queue, and the status flag of the last task object in the target throttling queue is set to an executed flag.
7. The task throttling method according to claim 1, characterized in that: The step of processing the target task object according to the target queue mode and each of the task objects in the target throttling queue according to the throttling scheme corresponding to the target queue mode also includes: When the target queue mode is the fifth mode, determining whether there is a second single timer in the preset second timer list; If it exists, destroy the second one-shot timer, discard the last task object in the target throttling queue, add the target task object to the tail of the target throttling queue, create the second one-shot timer according to the target task object, and add the created second one-shot timer to the second timer list; If it does not exist, adding the target task object to the tail of the target throttling queue, creating the second one-shot timer according to the target task object, and adding the created second one-shot timer to the second timer list; When the second one-shot timer is triggered, a function call is made to the last task object in the target throttling queue, the state flag of the last task object in the target throttling queue is set to an execution flag, and the second one-shot timer is destroyed.
8. A task throttling device, used to execute the method according to any one of claims 1 to 7, characterized in that: The device comprises: A request acquisition module, used for acquiring a function call request, wherein the function call request carries a call configuration; the call configuration corresponds to a queue mode, and different queue modes implement different throttling schemes; A judgment module, used for judging whether there is a throttling queue corresponding to the calling configuration in a preset throttling queue library; a first target throttling queue determining module, configured to use the throttling queue corresponding to the calling configuration as the target throttling queue if it exists; a second target throttling queue determination module, configured to obtain a queue mode corresponding to the calling configuration from a preset throttling mode table as a target queue mode if the queue mode does not exist, and to create the throttling queue according to the calling configuration and the target queue mode as the target throttling queue; A target task object channel module is used to encapsulate the function call request into a task object to obtain a target task object; The task object processing module is used to process the target task object according to the target queue mode and each task object in the target throttling queue according to the throttling scheme corresponding to the target queue mode.
9. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 7 are implemented.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Page updating method and device based on current limiter and computer storage medium
CN109725974A
Speculation control for improving transaction success rate, and instruction therefor
US20150378912A1