Method, apparatus, and storage medium for limiting the number of concurrent requests
By judging and processing the N current limiters matching the target request, the problem of difficulty in accurately controlling the number of server concurrent requests in the prior art is solved, and precise control of the number of server concurrent requests is realized, and system stability and performance are improved.
Patent Information
- Application Number
- CN202210861820.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-07-20
- Publication Date
- 2025-06-17
- Estimated Expiration
- 2042-07-20
AI Technical Summary
The prior art is difficult to accurately control the number of requests that the server runs concurrently, making it difficult to prevent system crashes in the case of high concurrent requests.
By matching the target request with N current limiters, it is determined whether the first target current limiter allows the request to pass, and obtains the semaphore when allowed. Otherwise, the request will be added to the blocking queue until all the N current limiters are executed.
It realizes precise control of the number of concurrent requests on the server, avoids system crashes, and improves system stability and performance.
Smart Images

Figure CN115269183B_ABST
Abstract
Description
Technical Field
[0001] This application belongs to the field of software development, and particularly relates to a method, device, and storage medium for limiting the number of concurrent requests.
Background Art
[0002] For software systems that provide open interfaces, as the number of users increases, instantaneous high-concurrency requests often occur. In such cases, in addition to improving the performance of the software system, it is also necessary to limit the request rate to prevent the system from crashing.
[0003] Currently, mainly by restricting the request frequency, when the limit frequency is exceeded, the request is directly rejected; or, when the limit frequency is exceeded, the request waits with a delay until it meets the allowed conditions before running (or is rejected after the waiting time expires).
[0004] Although the existing solutions control the request frequency, since the running time of each request is different, the number of requests running concurrently on the server cannot be accurately controlled.
Summary of the Invention
[0005] This application provides a method, device, and storage medium for limiting the number of concurrent requests, which can accurately control the number of requests running concurrently.
[0006] The first aspect of this application provides a method for limiting the number of concurrent requests, including:
[0007] Step 1: If a target request is received, determine whether N current limiters corresponding to the target request are matched according to the request conditions corresponding to the target request, where N is an integer greater than or equal to 1;
[0008] Step 2: If the N current limiters are matched, determine whether a first target limiter allows the target request to pass, where the first target limiter is any one of the N current limiters;
[0009] Step 3: If the first target limiter allows the target request to pass, perform a P operation on the target request to obtain a semaphore;
[0010] Step 4: If the first target limiter does not allow the target request to pass, add the target request to a first blocking queue;
[0011] If the waiting duration of the target request in the first blocking queue does not exceed the time limit, repeat Steps 2 to 4 until all the N current limiters have been executed;
[0012] Perform a V operation on the target request through a target thread to release the semaphore, where the target thread is the running thread corresponding to the target request.
[0013] The second aspect of the present application provides a device for limiting the number of concurrent requests, including:
[0014] A first judgment unit, configured to execute step 1: if a target request is received, determine whether N current limiters corresponding to the target request are matched according to the request conditions corresponding to the target request, where N is an integer greater than or equal to 1;
[0015] A second judgment unit, configured to execute step 2: if the N current limiters are matched, determine whether a first target current limiter allows the target request to pass, where the first target current limiter is any one of the N current limiters;
[0016] An operation unit, configured to execute step 3: if the first target current limiter allows the target request to pass, perform a P operation on the target request to obtain a semaphore;
[0017] A processing unit, configured to execute step 4: if the first target current limiter does not allow the target request to pass, add the target request to a first blocking queue;
[0018] A repeated execution unit, configured to: if the waiting duration of the target request in the first blocking queue does not time out, repeatedly execute step 2 to step 4 until all the N current limiters have been executed;
[0019] The operation unit is further configured to perform a V operation on the target request through a target thread to release the semaphore, where the target thread is the running thread corresponding to the target request.
[0020] The third aspect of the embodiments of the present application provides a computer device, which includes at least one connected processor, a memory, and a transceiver, where the memory is used to store program codes, and the processor is used to call the program codes in the memory to execute the steps of the vehicle diagnosis method described in the first aspect above.
[0021] The fourth aspect of the embodiments of the present application provides a computer storage medium, which includes instructions that, when running on a computer, cause the computer to execute the steps of the vehicle diagnosis method described in the first aspect above.
[0022] In summary, it can be seen that in the embodiments provided by the present application, N current limiters corresponding to the target request are matched, where N is an integer greater than or equal to 1, and then it is determined whether the first target current limiter allows the target request to pass, and the first target current limiter is any one of the N current limiters; if the first target current limiter allows the target request to pass, then perform operation P on the target request to obtain a semaphore; if the first target current limiter does not allow the target request to pass, then add the target request to the first blocking queue; if the waiting duration of the target request in the first blocking queue does not time out, then repeat the above steps until all N current limiters have been executed; perform operation V on the target request to release the semaphore. Thus, the number of requests for concurrent operation of the server can be precisely controlled by setting current limiters.
Description of the Drawings
[0023] Figure 1 It is a schematic flowchart of a method for limiting the number of concurrent requests provided by an embodiment of the present application;
[0024] Figure 2 It is a schematic virtual structure diagram of a device for limiting the number of concurrent requests provided by an embodiment of the present application;
[0025] Figure 3 It is a schematic hardware structure diagram of a device for limiting the number of concurrent requests provided by an embodiment of the present invention;
[0026] Figure 4 It is a schematic diagram of an embodiment of an electronic device provided by an embodiment of the present invention;
[0027] Figure 5 It is a schematic diagram of an embodiment of a computer-readable storage medium provided by an embodiment of the present invention.
Detailed Embodiments
[0028] Next, the technical solutions in the embodiments of the present application will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all of the embodiments.
[0029] In the description and claims of this application and the above-mentioned drawings, terms such as "first" and "second" are used to distinguish similar objects, and do not necessarily describe a specific order or sequence. It should be understood that the data used in this way can be interchanged under appropriate circumstances, so that the embodiments described here can be implemented in an order other than that illustrated or described here. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product or device that includes a series of steps or modules does not necessarily have to be limited to those steps or modules clearly listed, but may include other steps or modules that are not clearly listed or are inherent to these processes, methods, products or devices. The division of modules in this application is only a logical division, and there may be other division methods in actual implementation. For example, multiple modules can be combined or integrated into another system, or some feature vectors can be ignored or not executed. In addition, the shown or discussed coupling or direct coupling or communication connection between each other can be through some interfaces, and the indirect coupling or communication connection between modules can be in an electrical or other similar form, which is not limited in this application. And the modules or sub-modules described as separate components may or may not be physically separated, may or may not be physical modules, or may be distributed to multiple circuit modules. Some or all of the modules can be selected according to actual needs to achieve the purpose of the solution of this application.
[0030] The following will describe the concurrent request number limiting device provided by the embodiments of this application from the perspective of the concurrent request number limiting device. The concurrent request number limiting device can be a server or a service unit in the server, and specific limitations are not made.
[0031] Please refer to Figure 1 , Figure 1 which is a schematic flowchart of the concurrent request number limiting device provided by the embodiments of this application, including:
[0032] 101. If a target request is received, determine whether N current limiters corresponding to the target request are matched according to the request conditions corresponding to the target request. If so, execute step 102.
[0033] In this embodiment, when the concurrent request number limiting device receives a target request, it can determine whether N current limiters corresponding to the target request are matched according to the request conditions corresponding to the target request. If the concurrent request number limiting device matches N current limiters corresponding to the target request, step 102 is executed, where N is an integer greater than or equal to 1. The current limiter is short for a program that limits requests, and the request conditions corresponding to the target request refer to the conditions for limiting requests. For example, requests of types such as URL and HTTP are limited.
[0034] It should be noted that after receiving the target request, N current limiters corresponding to the target request can be created, and the current limit parameters corresponding to the target request are configured for the N current limiters, where the current limit parameters include at least one of a request matching rule, a semaphore size, and a timeout waiting duration.
[0035] 102. Determine whether the first target current limiter allows the target request to pass. If so, execute step 103.
[0036] In this embodiment, the concurrent request quantity limiting device can arbitrarily select one current limiter from the N current limiters, that is, the first target current limiter, and determine whether the target request allowed by the first target current limiter passes. Specifically, the maximum number of requests allowed to pass in parallel in the current limiter can be set. For example, the maximum number of requests allowed to pass in parallel in the first current limiter is 5, and there are already 5 requests in the first target current limiter, then the target request is not allowed to pass, and step 104 is executed. If the number of requests existing in the first target current limiter is less than 5, the target request is allowed to pass, and step 103 is executed.
[0037] It should be noted that if the N current limiters corresponding to the target request are not matched according to the request conditions corresponding to the target request, it means that there is no current limiting operation in the target request, and the target request is directly run.
[0038] 103. Execute a P operation on the target request to obtain a semaphore.
[0039] In this embodiment, if the first target current limiter allows the target request to pass, a P operation is executed on the target request to obtain a semaphore.
[0040] It should be noted that the P and V operations are two operations used for communication between processes in an operating system. The P and V operations are common methods for realizing process synchronization and mutual exclusion. The P and V operations are low-level communication primitives and are indivisible during execution. Among them, the P operation represents applying for a resource, and the V operation represents releasing a resource. The definition of the P operation is: s := s - 1. If s >= 0, the process executing the P operation continues to execute; otherwise, if s < 0, the process is set to the blocked state and inserted into the blocked queue. The definition of the V operation is: s := s - 1. If s > 0, the process executing the V operation continues to execute; otherwise, if s <= 0, another process is awakened from the blocked state and inserted into the ready queue, and the process executing the V operation continues to execute.
[0041] 104. If the first target current limiter does not allow the target request to pass, add the target request to the first blocked queue.
[0042] In this embodiment, when the concurrent request quantity limiting device determines that the first target rate limiter does not allow the target request to pass, the target request can be added to the first blocking queue. Since there may be multiple requests in the first blocking queue, and according to the first-in, first-out principle, the target request may have to wait until the requests that entered the first blocking queue before it are executed (that is, wait until other requests in the first blocking queue release the semaphore) before being executed. Therefore, by setting a timeout threshold, continuously monitoring the waiting duration of the target request in the first blocking queue, and determining whether the waiting duration of the target request in the first blocking queue times out. If the waiting duration of the target request in the first blocking queue does not time out, then step 106 is executed.
[0043] 105. If the waiting duration of the target request in the first blocking queue does not time out, then repeat steps 102 to 104 until all N rate limiters have been executed.
[0044] In this embodiment, if the waiting duration of the target request in the first blocking queue does not time out, then directly proceed to the next rate limiter among the N rate limiters, and repeat the above steps until all N rate limiters have been executed. That is to say, after determining the N rate limiters corresponding to the target request, any one of the N rate limiters can be selected, and it is determined whether this rate limiter allows the target request to run. If it does, then a P operation is performed on the target request to obtain the corresponding semaphore. If this rate limiter does not allow the target request to run, then the target request is added to the blocking queue, and after waiting for the requests in the blocking queue to release the semaphore and not timing out, a P operation is performed to obtain the corresponding semaphore, and then proceed to the next rate limiter among the N rate limiters, and repeat until all the rate limiters among the N rate limiters have been executed.
[0045] It should be noted that if the waiting duration of the target request in the first blocking queue times out, then there is no need to execute the unexecuted rate limiters among the N rate limiters, and the target request is directly added to the request execution queue, and a failure function is executed.
[0046] 106. Perform a V operation on the target request through the target thread to release the semaphore.
[0047] In this embodiment, when the concurrent request quantity limiting device determines that each of the N current limiters has been executed, it can perform a V operation on the target request through the target thread to release the semaphore, where the target thread is the running thread corresponding to the target request. Specifically, the target request can be first added to the request execution queue, and after waiting for other requests to be executed, the target request is obtained, and the target thread corresponding to the target request is initialized to run the target request through the target thread, and it is determined whether the target request successfully passes through the N current limiters to obtain a judgment result, and corresponding operations are performed on the target request according to the judgment result.
[0048] In one embodiment, the concurrent request quantity limiting device performing corresponding operations on the target request according to the judgment result includes:
[0049] If the judgment result is that the target request successfully passes through the N current limiters, the target request is run, and the semaphores corresponding to the N current limiters are released;
[0050] If the judgment result is that the target current limiter successfully passes through some of the N current limiters, a V operation is performed on the target request to release the semaphores corresponding to the current limiters that are successfully executed among the N current limiters, and a failure function is executed;
[0051] If the judgment result is that the target request fails to pass through any of the N current limiters, the failure function is executed.
[0052] In this embodiment, when the concurrent request quantity limiting device determines that the judgment result is that the target request successfully passes through the N current limiters, it determines that the request is successful, runs the target request, and then releases the semaphores corresponding to the N current limiters. That is, each current limiter corresponds to a P operation. When successfully passing through the current limiter, a P operation is performed on the target request, and the corresponding semaphore is obtained. In this way, after the next N current limiters are all executed, when it is determined to be successful, the target request can be run, and a V operation is performed to release the semaphores corresponding to the N current limiters. If only some of the current limiters are successfully passed and some are not, a V operation is directly performed to release the semaphores corresponding to the successfully passed current limiters, and the failure function is executed; if all the current limiters are not successfully passed, the failure function is directly executed.
[0053] In summary, it can be seen that in the embodiments provided by this application, N current limiters corresponding to a target request are matched, where N is an integer greater than or equal to 1, and then it is determined whether the first target current limiter allows the target request to pass, and the first target current limiter is any one of the N current limiters; if the first target current limiter allows the target request to pass, then perform operation P on the target request to obtain a semaphore; if the first target current limiter does not allow the target request to pass, then add the target request to the first blocking queue; if the waiting duration of the target request in the first blocking queue does not time out, then repeat the above steps until all N current limiters have been executed; perform operation V on the target request to release the semaphore. Thus, the number of requests for concurrent operation of the server can be precisely controlled by setting current limiters.
[0054] Please refer to Figure 2 , Figure 2 which is the virtual structure diagram of the concurrent request quantity limiting device provided by this application. The concurrent request quantity limiting device 200 includes:
[0055] The first judgment unit 201 is configured to execute step 1. If a target request is received, then determine whether N current limiters corresponding to the target request are matched according to the request conditions corresponding to the target request, where N is an integer greater than or equal to 1;
[0056] The second judgment unit 202 is configured to execute step 2. If the N current limiters are matched, then determine whether the first target current limiter allows the target request to pass, and the first target current limiter is any one of the N current limiters;
[0057] The operation unit 203 is configured to execute step 3. If the first target current limiter allows the target request to pass, then perform operation P on the target request to obtain a semaphore;
[0058] The processing unit 204 is configured to execute step 4. If the first target current limiter does not allow the target request to pass, then add the target request to the first blocking queue;
[0059] The repeated execution unit 205 is configured to, if the waiting duration of the target request in the first blocking queue does not time out, then repeatedly execute steps 2 to 4 until all N current limiters have been executed;
[0060] The operation unit 203 is further configured to perform operation V on the target request through a target thread to release the semaphore, and the target thread is the running thread corresponding to the target request.
[0061] In a possible design, the operation unit 203 is specifically configured to:
[0062] Add the target request to the request execution queue;
[0063] Retrieve the target request from the request execution queue;
[0064] Initialize the target thread to run the target request through the target thread;
[0065] Judge whether the target request successfully passes through the N current limiters to obtain a judgment result;
[0066] Perform corresponding operations on the target request according to the judgment result.
[0067] In a possible design, the operation unit 203 performing corresponding operations on the target request according to the judgment result includes:
[0068] If the judgment result is that the target request successfully passes through the N current limiters, run the target request and release the semaphores corresponding to the N current limiters;
[0069] If the judgment result is that the target current limiter successfully passes through some of the N current limiters, perform a V operation on the target request to release the semaphores corresponding to the current limiters that are successfully executed among the N current limiters, and execute a failure function;
[0070] If the judgment result is that the target request fails to pass through any of the N current limiters, execute a failure function.
[0071] In a possible design, the processing unit 204 is further configured to add the target request to the request execution queue if the waiting duration of the target request in the first blocking queue times out;
[0072] The repeating execution unit 205 is further configured to retrieve the target request from the request execution queue;
[0073] The operation unit 203 is further configured to perform corresponding operations on the target request.
[0074] In a possible design, the operation unit 203 is further configured to:
[0075] Create the N current limiters corresponding to the target request;
[0076] Configure current limiting parameters corresponding to the target request for the N current limiters, where the current limiting parameters include at least one of a request matching rule, a semaphore size, and a timeout waiting time.
[0077] The operation unit 203 is further configured to:
[0078] If N current limiters corresponding to the target request are not matched according to the request conditions corresponding to the target request, the target request is run.
[0079] Above Figure 2 The concurrent request quantity limiting device in the embodiment of the present application has been described from the perspective of modular functional entities. Below, the concurrent request quantity limiting device in the embodiment of the present application will be described in detail from the perspective of hardware processing. Please refer to Figure 3 An embodiment of the concurrent request quantity limiting device 300 in the embodiment of the present application includes:
[0080] An input device 301, an output device 302, a processor 303, and a memory 304 (where the number of processors 303 can be one or more, Figure 3 and one processor 303 is taken as an example herein). In some embodiments of the present application, the input device 301, the output device 302, the processor 303, and the memory 304 can be connected through a bus or other means. Among them, Figure 3 connection through a bus is taken as an example herein.
[0081] Among them, by invoking the operation instructions stored in the memory 304, the processor 303 is configured to execute the following steps:
[0082] Step 1: If a target request is received, it is determined whether N current limiters corresponding to the target request are matched according to the request conditions corresponding to the target request, where N is an integer greater than or equal to 1;
[0083] Step 2: If the N current limiters are matched, it is determined whether a first target current limiter allows the target request to pass, and the first target current limiter is any one of the N current limiters;
[0084] Step 3: If the first target current limiter allows the target request to pass, a P operation is performed on the target request to obtain a semaphore;
[0085] Step 4: If the first target current limiter does not allow the target request to pass, the target request is added to a first blocking queue;
[0086] If the waiting duration of the target request in the first blocking queue does not time out, steps 2 to 4 are repeatedly executed until all the N current limiters have been executed;
[0087] A V operation is performed on the target request through a target thread to release the semaphore, and the target thread is the running thread corresponding to the target request.
[0088] By invoking the operation instructions stored in the memory 304, the processor 303 is further configured to executeFigure 1 Any implementation mode in the corresponding embodiment.
[0089] Please refer to Figure 4 , Figure 4 which is a schematic diagram of an embodiment of an electronic device provided by an embodiment of the present application.
[0090] As Figure 4 shown, an embodiment of the present application provides an electronic device, including a memory 410, a processor 420, and a computer program 411 stored on the memory 410 and executable on the processor 420. When the processor 420 executes the computer program 411, the following steps are implemented:
[0091] Step 1: If a target request is received, it is determined whether N current limiters corresponding to the target request are matched according to the request conditions corresponding to the target request, where N is an integer greater than or equal to 1;
[0092] Step 2: If the N current limiters are matched, it is determined whether a first target current limiter allows the target request to pass, and the first target current limiter is any one of the N current limiters;
[0093] Step 3: If the first target current limiter allows the target request to pass, perform a P operation on the target request to obtain a semaphore;
[0094] Step 4: If the first target current limiter does not allow the target request to pass, add the target request to a first blocking queue;
[0095] If the waiting duration of the target request in the first blocking queue does not time out, repeat steps 2 to 4 until all the N current limiters have been executed;
[0096] Perform a V operation on the target request through a target thread to release the semaphore, where the target thread is the running thread corresponding to the target request.
[0097] In a specific implementation process, when the processor 420 executes the computer program 411, it can implement Figure 1 any implementation mode in the corresponding embodiment.
[0098] Since the electronic device introduced in this embodiment is the device adopted by an apparatus for limiting the number of concurrent requests in an embodiment of the present application, based on the method introduced in the embodiment of the present application, those skilled in the art can understand the specific implementation manners and various variations of the electronic device in this embodiment. Therefore, the specific implementation of how this electronic device implements the method in the embodiment of the present application will not be described in detail here. As long as the device adopted by those skilled in the art to implement the method in the embodiment of the present application falls within the scope of protection of the present application.
[0099] Please refer to Figure 5 , Figure 5 which is a schematic diagram of an embodiment of a computer-readable storage medium provided by an embodiment of the present application.
[0100] As Figure 5 shown, this embodiment provides a computer-readable storage medium 500, on which a computer program 511 is stored. When the computer program 751 is executed by a processor, the following steps are implemented:
[0101] Step 1: If a target request is received, determine whether N current limiters corresponding to the target request are matched according to the request conditions corresponding to the target request, where N is an integer greater than or equal to 1;
[0102] Step 2: If the N current limiters are matched, determine whether a first target current limiter allows the target request to pass through. The first target current limiter is any one of the N current limiters;
[0103] Step 3: If the first target current limiter allows the target request to pass through, perform a P operation on the target request to obtain a semaphore;
[0104] Step 4: If the first target current limiter does not allow the target request to pass through, add the target request to a first blocking queue;
[0105] If the waiting duration of the target request in the first blocking queue does not time out, repeat Steps 2 to 4 until all the N current limiters have been executed;
[0106] Perform a V operation on the target request through a target thread to release the semaphore. The target thread is the running thread corresponding to the target request.
[0107] In a specific implementation process, when the computer program 511 is executed by a processor, it can implement Figure 1 any one of the implementation manners in the corresponding embodiment.
[0108] It should be noted that in the above embodiments, the descriptions of the various embodiments have their own emphases. For parts not described in detail in a certain embodiment, reference may be made to the relevant descriptions of other embodiments.
[0109] Those skilled in the art should understand that the embodiments of the present application can be provided as a method, a system, or a computer program product. Therefore, the present application can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present application can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code.
[0110] The present application is described with reference to the flowcharts and / or block diagrams of methods, apparatuses (systems), and computer program products according to the embodiments of the present application. It should be understood that each flow and / or block in the flowchart and / or block diagram can be implemented by computer program instructions, and the combination of the flows and / or blocks in the flowchart and / or block diagram can also be implemented by computer program instructions. These computer program instructions can be provided to the processor of a general-purpose computer, a special-purpose computer, an embedded computer, or other programmable data processing devices to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing devices generate a device for implementing the specified functions in Figure 1 one or more of the processes or multiple processes and / or blocks Figure 1 one or more of the blocks or multiple blocks.
[0111] These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer-readable memory generate a manufactured article including an instruction device, and the instruction device implements the specified functions in Figure 1 one or more of the processes or multiple processes and / or blocks Figure 1 one or more of the blocks or multiple blocks.
[0112] These computer program instructions can also be loaded onto a computer or other programmable data processing device, so that a series of operation steps are executed on the computer or other programmable device to generate a computer-implemented process, and thus the instructions executed on the computer or other programmable device provide steps for implementing the specified functions in Figure 1 one or more of the processes or multiple processes and / or blocks Figure 1 one or more of the blocks or multiple blocks.
[0113] The embodiments of the present application also provide a computer program product, which includes computer software instructions. When the computer software instructions run on a processing device, the processing device is caused to execute as Figure 1The process in the PDF keyword location method in the corresponding embodiment.
[0114] The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the processes or functions described in the embodiments of the present application are wholly or partially generated. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions may be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions may be transmitted from a website, computer, server, or data center to another website, computer, server, or data center by wire (such as coaxial cable, optical fiber, digital subscriber line (DSL)) or wirelessly (such as infrared, wireless, microwave, etc.). The computer-readable storage medium may be any available medium that a computer can store or a data storage device such as a server or data center that includes one or more integrated available media. The available medium may be a magnetic medium (such as a floppy disk, hard disk, magnetic tape), an optical medium (such as a DVD), or a semiconductor medium (such as a solid state disk (SSD)).
[0115] Those skilled in the art can clearly understand that for the convenience and brevity of description, the specific working processes of the systems, devices, and units described above can refer to the corresponding processes in the foregoing method embodiments and will not be elaborated here.
[0116] In several embodiments provided in the present application, it should be understood that the disclosed systems, devices, and methods can be implemented in other ways. For example, the device embodiments described above are merely illustrative. For example, the division of the units is only a logical function division, and there may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the displayed or discussed couplings or direct couplings or communication connections to each other can be through some interfaces, and the indirect couplings or communication connections of the devices or units can be in electrical, mechanical, or other forms.
[0117] The units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place or distributed to multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0118] In addition, in each embodiment of the present application, each functional unit may be integrated into one processing unit, may exist physically alone for each unit, or two or more units may be integrated into one unit. The above integrated unit may be implemented in the form of hardware or in the form of a software functional unit.
[0119] If the above integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it may be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the present application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, may be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in each embodiment of the present application. The foregoing storage medium includes: various media such as USB flash drives, mobile hard disks, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical discs that can store program codes.
[0120] As described above, the above embodiments are only used to illustrate the technical solutions of the present application and are not intended to limit them; although the present application has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that they can still modify the technical solutions described in the foregoing embodiments or perform equivalent replacements for some of the technical features; and these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of each embodiment of the present application.
Claims
1. A method for limiting the number of concurrent requests, characterized in that Including: Step 1: If a target request is received, determine whether N current limiters corresponding to the target request are matched according to the request conditions corresponding to the target request, where N is an integer greater than or equal to 1; Step 2: If the N current limiters are matched, determine whether a first target current limiter allows the target request to pass, where the first target current limiter is any one of the N current limiters; Step 3: If the first target current limiter allows the target request to pass, perform operation P on the target request to obtain a semaphore; Step 4: If the first target current limiter does not allow the target request to pass, add the target request to a first blocking queue; If the waiting duration of the target request in the first blocking queue does not time out, repeat steps 2 to 4 until all the N current limiters have been executed; Perform operation V on the target request through a target thread to release the semaphore, where the target thread is the running thread corresponding to the target request.
2. The method according to claim 1, characterized in that The performing operation V on the target request through a target thread to release the semaphore includes: Adding the target request to a request execution queue; Obtaining the target request from the request execution queue; Initializing the target thread to run the target request through the target thread; Judging whether the N current limiters that the target request successfully passes through to obtain a judgment result; Performing corresponding operations on the target request according to the judgment result.
3. The method according to claim 2, characterized in that The performing corresponding operations on the target request according to the judgment result includes: If the judgment result is that the target request successfully passes through the N current limiters, running the target request and releasing the semaphores corresponding to the N current limiters; If the judgment result is that the target current limiter successfully passes through some of the N current limiters, performing operation V on the target request to release the semaphores corresponding to the current limiters that are successfully executed among the N current limiters, and executing a failure function; If the judgment result is that the target request does not pass through any of the N current limiters, executing a failure function.
4. The method according to claim 2, characterized in that The method further includes: If the waiting duration of the target request in the first blocking queue times out, adding the target request to the request execution queue; Obtaining the target request from the request execution queue; Performing corresponding operations on the target request.
5. The method according to any one of claims 1 to 4, characterized in that The method further includes: Creating the N current limiters corresponding to the target request; Configuring current limiting parameters corresponding to the target request for the N current limiters, where the current limiting parameters include at least one of a request matching rule, a semaphore size, and a timeout waiting time.
6. The method according to any one of claims 1 to 4, characterized in that The method further includes: If N current limiters corresponding to the target request are not matched according to the request conditions corresponding to the target request, running the target request.
7. A device for limiting the number of concurrent requests, characterized in that Including: A first judgment unit, configured to execute step 1: If a target request is received, determine whether N current limiters corresponding to the target request are matched according to the request conditions corresponding to the target request, where N is an integer greater than or equal to 1; A second judgment unit, configured to execute step 2, if the N current limiters are matched, determine whether the first target current limiter allows the target request to pass, where the first target current limiter is any one of the N current limiters; An operation unit, configured to execute step 3, if the first target current limiter allows the target request to pass, perform a P operation on the target request to obtain a semaphore; A processing unit, configured to execute step 4, if the first target current limiter does not allow the target request to pass, add the target request to the first blocking queue; A repeated execution unit, configured to, if the waiting duration of the target request in the first blocking queue does not time out, repeatedly execute step 2 to step 4 until all of the N current limiters have been executed; The operation unit is further configured to perform a V operation on the target request through a target thread to release the semaphore, where the target thread is the running thread corresponding to the target request.
8. The device according to claim 7, characterized in that, The operation unit is specifically configured to: Add the target request to a request execution queue; Obtain the target request from the request execution queue; Initialize the target thread to run the target request through the target thread; Judge whether the target request successfully passes through the N current limiters to obtain a judgment result; Perform corresponding operations on the target request according to the judgment result.
9. A computer device, characterized in that, Comprising: At least one connected processor, a memory, and a transceiver, where the memory is used to store program code, and the processor is used to call the program code in the memory to execute the steps of the method for limiting the number of concurrent requests according to any one of claims 1 to 6.
10. A computer storage medium, characterized in that, Comprising: Instructions, when the instructions run on a computer, cause the computer to execute the steps of the method for limiting the number of concurrent requests according to any one of claims 1 to 6.
Citation Information
Patent Citations
A method and device for limiting traffic
CN110275780A
Concurrent request processing method and device, server and storage medium
CN113342498A