Method and device for processing request
By setting the processing time for thread processing requests based on the service identity, the problem of unfair processing of thread pool requests is solved, and fair processing and resource utilization are achieved under different types of requests.
Patent Information
- Application Number
- CN201910149945.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2019-02-28
- Publication Date
- 2025-08-19
- Estimated Expiration
- 2039-02-28
AI Technical Summary
In the prior art, when thread pools process requests, due to uneven service performance, the processing is unfair. In the case of high concurrency, some requests timed out or were rejected, and thread resources cannot be effectively utilized.
By setting the processing time for thread processing requests based on the service identity, using TP algorithm or maximum/minimum processing time to control thread processing requests, ensuring that each request is completed within a reasonable time range, avoiding affecting other requests under abnormal circumstances.
It realizes fair processing under different types of requests, improves the utilization rate and processing applicability of thread resources, and ensures that the request does not affect other requests under normal circumstances and does not delay the processing of other requests under abnormal circumstances.
Smart Images

Figure CN111625324B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a method and device for processing a request. Background Art
[0002] Currently, there are two main ways to process requests in the existing technology:
[0003] like Figure 1 As shown in the figure, the first method of processing a request includes: a thread in the thread pool retrieves a request from the request queue, sends the request to service 1, waits for service 1 to respond to the request and return a result, and sends the result to the request sender. All requests are processed by the thread pool and responded to by the corresponding service.
[0004] like Figure 2 As shown, the second method of processing requests includes: a thread in thread pool 1 obtains a request from request queue 1, sends the request to service 1, waits for service 1 to respond to the request and return a result, and sends the result to the sender of the request; a thread in thread pool 2 obtains a request from request queue 2, sends the request to service 2, waits for service 2 to respond to the request and return a result, and sends the result to the sender of the request; a thread in thread pool 3 obtains a request from request queue 3, sends the request to service 3, waits for service 3 to respond to the request and return a result, and sends the result to the sender of the request. Thread pools 1, 2, and 3 all have the same resources.
[0005] In the process of implementing the present invention, the inventors discovered that the prior art has at least the following problems:
[0006] Existing method 1 is only suitable for scenarios where the performance of services 1 to N is similar, as it is designed to ensure fair request processing. However, in reality, the performance of each service varies greatly. Because they share a thread pool, this can easily lead to the thread pool constantly processing one type of request, while other types of requests are unfairly processed. In high-concurrency scenarios, all threads in the thread pool are processing one type of request, causing other types of requests to time out or be rejected. This results in unfair request processing.
[0007] In existing method 2, the types of requests in request queue 1, request queue 2, and request queue 3 are all different. For example, if request queue 1 contains one type of request, thread pool 1 only processes one type of request in request queue 1; for another example, if request queue 2 contains another type of request, thread pool 2 only processes another type of request in request queue 2. This solves the problem of unfair processing in existing method 1. In addition, requests of the same type are processed by threads in the same thread pool, and the processing processes of different types of requests are isolated from each other. However, since the processing of one type of request takes a short time, thread pool 2 will not be fully utilized; while the processing of another type of request takes a long time, thread pool 1 is insufficient. This leads to unfair processing of requests. Summary of the Invention
[0008] In view of this, an embodiment of the present invention provides a method and apparatus for processing requests, which can fairly process each request.
[0009] To achieve the above objective, according to one aspect of an embodiment of the present invention, a method for processing a request is provided.
[0010] The method for processing a request according to an embodiment of the present invention includes:
[0011] Setting a processing time of a thread processing request according to a service identifier; wherein the request carries the service identifier;
[0012] The thread is controlled to process the request according to the processing duration.
[0013] In one embodiment, setting the processing time of a thread processing request according to the service identifier includes:
[0014] Determine whether to use the TP algorithm to set the processing time of the thread processing request based on the service identifier;
[0015] If so, the TP algorithm is used to set the processing time of the thread processing request;
[0016] If not, the maximum processing time corresponding to the service identifier is set as the processing time of the thread processing request;
[0017] The processing duration is the sum of the duration of the thread sending the request to the service, the duration of the thread waiting for the service to respond to the request and return the result, and the duration of the thread sending the result;
[0018] The service identifier includes information assigned by the gateway to the service, or information provided by the service to the gateway.
[0019] In one embodiment, determining whether to use the TP algorithm to set the processing time of the thread processing request according to the service identifier includes:
[0020] According to the name of the sampling queue, a sampling queue having the same service identifier as the service identifier is selected from the plurality of sampling queues and used as the target queue;
[0021] Determine whether the number of historical durations in the target queue is equal to the dimension of the TP algorithm;
[0022] The historical duration is the duration during which the thread processes historical requests with the same service identifier.
[0023] In one embodiment, using the TP algorithm to set the processing time of a thread processing request includes:
[0024] Sort the historical durations in the target queue in ascending order, and multiply the number of historical durations by the index number of the TP algorithm to obtain a sampling value;
[0025] Filtering the historical duration that is at the position of the sampling value from the sorted historical durations according to the sampling value;
[0026] The processing duration of the thread processing request is set according to the historical duration of the sampling value position.
[0027] In one embodiment, setting the processing duration of the thread processing request according to the historical duration of the sampling value position includes:
[0028] Determine whether the historical duration of the sampling value position is greater than the minimum processing duration corresponding to the service identifier;
[0029] If so, setting the historical duration at the sampling value position as the processing duration of the thread processing request;
[0030] If not, the minimum processing duration is set as the processing duration of the thread processing request.
[0031] In one embodiment, after controlling the thread to process the request according to the processing duration, the method further includes:
[0032] The duration of processing the request by the thread is inserted into the target queue according to a first-in-first-out method.
[0033] In one embodiment, controlling the thread to process the request according to the processing duration includes:
[0034] When the thread starts processing the request, the timing is started, and it is determined whether the timing duration is equal to the processing duration;
[0035] If so, the thread is terminated from processing the request; if not, the thread is controlled to continue processing the request.
[0036] To achieve the above objective, according to another aspect of an embodiment of the present invention, a device for processing a request is provided.
[0037] The device for processing a request according to an embodiment of the present invention includes:
[0038] A setting unit, configured to set a processing time of a thread processing request according to a service identifier; wherein the request carries the service identifier;
[0039] A control unit is used to control the thread to process the request according to the processing duration.
[0040] In one embodiment, the setting unit is specifically configured to:
[0041] Determine whether to use the TP algorithm to set the processing time of the thread processing request based on the service identifier;
[0042] If so, the TP algorithm is used to set the processing time of the thread processing request;
[0043] If not, the maximum processing time corresponding to the service identifier is set as the processing time of the thread processing request;
[0044] The processing duration is the sum of the duration of the thread sending the request to the service, the duration of the thread waiting for the service to respond to the request and return the result, and the duration of the thread sending the result;
[0045] The service identifier includes information assigned by the gateway to the service, or information provided by the service to the gateway.
[0046] In one embodiment, the setting unit is further configured to:
[0047] According to the name of the sampling queue, a sampling queue having the same service identifier as the service identifier is selected from the plurality of sampling queues and used as the target queue;
[0048] Determine whether the number of historical durations in the target queue is equal to the dimension of the TP algorithm;
[0049] The historical duration is the duration during which the thread processes historical requests with the same service identifier.
[0050] In one embodiment, the setting unit is further configured to:
[0051] Sort the historical durations in the target queue in ascending order, and multiply the number of historical durations by the index number of the TP algorithm to obtain a sampling value;
[0052] Filtering the historical duration that is at the position of the sampling value from the sorted historical durations according to the sampling value;
[0053] The processing duration of the thread processing request is set according to the historical duration of the sampling value position.
[0054] In one embodiment, the setting unit is further configured to:
[0055] Determine whether the historical duration of the sampling value position is greater than the minimum processing duration corresponding to the service identifier;
[0056] If so, setting the historical duration at the sampling value position as the processing duration of the thread processing request;
[0057] If not, the minimum processing duration is set as the processing duration of the thread processing request.
[0058] In one embodiment, the control unit is specifically configured to:
[0059] It is used to insert the time duration for the thread to process the request into the target queue according to a first-in-first-out method after controlling the thread to process the request according to the processing time duration.
[0060] In one embodiment, the control unit is further configured to:
[0061] When the thread starts processing the request, the timing is started, and it is determined whether the timing duration is equal to the processing duration;
[0062] If so, the thread is terminated from processing the request; if not, the thread is controlled to continue processing the request.
[0063] To achieve the above objective, according to another aspect of the embodiments of the present invention, an electronic device is provided.
[0064] An electronic device according to an embodiment of the present invention includes: one or more processors; a storage device for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors implement the method for processing requests provided by an embodiment of the present invention.
[0065] To achieve the above objective, according to another aspect of an embodiment of the present invention, a computer-readable medium is provided.
[0066] A computer-readable medium according to an embodiment of the present invention stores a computer program, which, when executed by a processor, implements the method for processing a request provided by an embodiment of the present invention.
[0067] An embodiment of the above invention has the following advantages or beneficial effects: by setting the processing time of the thread processing request and controlling the thread processing request according to the processing time, the processing of each request is limited to a reasonable time range, ensuring that under normal circumstances, the thread processing request is not affected; under abnormal circumstances, the thread processing of other requests is not delayed, and each request is processed fairly. The processing time is set through the service identifier, so that various types of requests are processed according to the processing time, thereby improving the applicability of the processing.
[0068] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with specific embodiments. BRIEF DESCRIPTION OF THE DRAWINGS
[0069] The accompanying drawings are provided for a better understanding of the present invention and are not intended to limit the present invention.
[0070] Figure 1 This is a schematic diagram of the main process of the first method of processing requests in the prior art;
[0071] Figure 2 This is a schematic diagram of the main process of the second method for processing requests in the prior art;
[0072] Figure 3 is a schematic diagram of the main process of a method for processing a request according to an embodiment of the present invention;
[0073] Figure 4 is a schematic diagram of the main process of a method for processing a request according to another embodiment of the present invention;
[0074] Figure 5 2 is a schematic diagram of an application scenario of a method for processing a request according to an embodiment of the present invention;
[0075] Figure 6 is a schematic diagram of inserting a target queue in a method for processing a request according to an embodiment of the present invention;
[0076] Figure 7 is a schematic diagram of an application scenario of a method for processing a request according to yet another embodiment of the present invention;
[0077] Figure 8 is a schematic diagram of main units of an apparatus for processing a request according to an embodiment of the present invention;
[0078] Figure 9 is an exemplary system architecture diagram in which embodiments of the present invention may be applied;
[0079] Figure 10 It is a schematic diagram of the structure of a computer system of a terminal device or a server suitable for implementing an embodiment of the present invention. DETAILED DESCRIPTION
[0080] The following description of exemplary embodiments of the present invention is made in conjunction with the accompanying drawings, in which various details of the embodiments of the present invention are included to facilitate understanding. These details should be considered as merely exemplary. Therefore, it should be appreciated by those skilled in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for the sake of clarity and conciseness, descriptions of well-known functions and structures are omitted in the following description.
[0081] It should be noted that, in the absence of conflict, the embodiments of the present invention and the features in the embodiments may be combined with each other.
[0082] In order to solve the problems existing in the prior art, an embodiment of the present invention provides a method for processing a request, such as Figure 3 As shown, the method includes:
[0083] Step S301: Setting a processing duration of a thread processing request according to a service identifier; wherein the request carries the service identifier.
[0084] In this step, during specific implementation, the processing duration can be set using the maximum processing duration or the minimum processing duration based on the service identifier. Alternatively, historical durations can be filtered from the target queue based on the TP algorithm and used to set the processing duration. Furthermore, the service identifier includes information assigned by the gateway to the service, or information provided by the service to the gateway. The information assigned by the gateway to the service can be an ID assigned by the gateway to the service; the information provided by the service to the gateway can be at least one of the service name, service creation time, service provider name, and the IP address of the device where the service is located.
[0085] Different request types, different response services, and different set processing times result in different request processing, thus achieving request isolation. Setting a processing time and controlling the thread processing requests according to the processing time is equivalent to limiting the processing of requests to a reasonable time range, ensuring that under normal circumstances, the thread processing request is not affected; under abnormal circumstances, the thread processing other requests is not delayed.
[0086] Step S302: Control the thread to process the request according to the processing duration.
[0087] In this step, when it is specifically implemented, when the thread starts to process the request, the timing is started. When the timing duration is equal to the processing duration, the thread processing request is terminated, thereby limiting the processing of the request to a reasonable time range, releasing the thread, and the thread can process other requests, and the thread resources are fully utilized. It should be understood that if the gateway implementing the embodiment of the present invention is a single process, each request is received by the process, the processing duration is set, and the thread processing request is controlled according to the processing duration. If the gateway is multi-process, any one of the multi-processes receives the request (which process receives the request can be controlled by the load balancer), the processing duration is set, and the thread processing request is controlled according to the processing duration.
[0088] In an embodiment of the present invention, by setting the processing time of the thread processing request and controlling the thread processing request according to the processing time, the processing of each request is limited to a reasonable time range, ensuring that under normal circumstances, the thread processing request is not affected; under abnormal circumstances, the thread processing other requests is not delayed, and each request is processed fairly. The processing time is set through the service identifier, so that various types of requests are processed according to the processing time, thereby improving the applicability of the processing.
[0089] In order to solve the problems existing in the prior art, another embodiment of the present invention provides a method for processing a request, such as Figure 4 As shown, the method includes:
[0090] Step S4001: According to the name of the sampling queue, a sampling queue with the same service ID is screened out from multiple sampling queues and used as the target queue.
[0091] In this step, a specific example is used to illustrate the step. Figure 5 As shown, there are two requests in the request queue: Request 1 (a lightweight request) and Request 2 (a heavyweight request). The name of Sample Queue 1 is the same as Service ID 1 carried by Request 1, and the name of Sample Queue 2 is the same as Service ID 2 carried by Request 2. Therefore, Sample Queue 1 serves as the target queue for Service ID 1, and Sample Queue 2 serves as the target queue for Service ID 2. Then, based on Sample Queue 1, a processing duration 1 is set for a thread in the thread pool to process Request 1, and based on Sample Queue 2, a processing duration 2 is set for a thread in the thread pool to process Request 2. Finally, the thread's processing of Request 1 is controlled according to Processing Duration 1, and the thread's processing of Request 2 is controlled according to Processing Duration 2. Therefore, the request processing process is isolated. Furthermore, the service ID includes information assigned by the gateway to the service, or information provided by the service to the gateway. The information assigned by the gateway to the service can be an ID assigned by the gateway to the service; the information provided by the service to the gateway can be at least one of the service name, service creation time, service provider name, and IP address of the device where the service is located.
[0092] It should be noted that the sampling queue stores historical durations. The maximum number of historical durations is the dimension of the TP algorithm, which is also the length of the sampling queue. The historical duration in the sampling queue is the duration of the thread processing historical requests of the same type. Historical requests of the same type are responded to by the same service. Therefore, the sampling queue can be named after the service identifier of the service, and each sampling queue is independent of each other. In addition, the lengths of each sampling queue can be different, that is, using TP algorithms with different dimensions to set the processing time for threads to process different types of requests. For example, the TP99 algorithm (99 is the dimension of the TP99 algorithm and the length of the target queue) is used to set the processing time for a thread to process type A requests, while the TP50 algorithm (50 is the dimension of the TP50 algorithm and the length of the target queue) is used to set the processing time for processing type B requests. Furthermore, the service access gateway assigns a unique service identifier to the service, and the sender of the request encapsulates the service identifier within the request. The service identifier can be a service ID.
[0093] In addition, if the gateway has multiple processes, you can set multiple sampling queues for each process. The historical duration in the process sampling queue represents the performance of the process. Setting the processing time of the thread processing request according to the process sampling queue will make the processing time setting more accurate.
[0094] Furthermore, different request types and service identifiers result in different services responding to the requests. As a result, different target queues are selected, the processing time is set differently, and the request processing is different, thereby improving the applicability of the processing. It is not only applicable to requests with a single processing type, but also to requests with a variety of processing types, ensuring the isolation of request processing.
[0095] Step S4002: Determine whether the number of historical durations in the target queue is equal to the dimension of the TP algorithm, where the historical duration is the duration of the thread processing historical requests with the same service identifier.
[0096] In this step, it is determined whether the number of historical durations in the target queue is equal to the dimension of the TP algorithm. If so, step S4004 is executed; if not, step S4003 is executed.
[0097] The TP algorithm (TP = Top Percentile, Top percentage is a term in statistics) can be the TP50 algorithm, TP99 algorithm and TP999 algorithm, etc., among which 50, 99 and 999 are the dimensions of the TP algorithm. It should be noted that the TP algorithm can only be used to set the processing duration when the number of historical durations in the target queue is equal to the dimension of the TP algorithm. This is determined by the TP algorithm itself. In addition, the historical duration in the target queue is the duration of the thread processing historical requests of the same request type. Setting the processing duration based on the target queue makes the setting of the processing duration more accurate, thereby limiting the processing of the request to a more reasonable time range, ensuring that under normal circumstances, it does not affect the thread processing of the request; under abnormal circumstances, it does not affect the thread processing of other requests.
[0098] Step S4003: Set the maximum processing time corresponding to the service identifier as the processing time for the thread to process the request, where the processing time is the sum of the time it takes for the thread to send the request to the service, the time it takes for the thread to wait for the service to respond to the request and return the result, and the time it takes for the thread to send the result.
[0099] In this step, different request types, different service identifiers carried by the requests, different services responding to the requests, and different response times. Therefore, the maximum processing time should be set according to the service identifier, so as to limit the processing of various types of requests to a more reasonable time range and further improve the applicability of the processing.
[0100] It should be noted that no matter whether the maximum processing time, the minimum processing time or the historical time ranked at the sampling value position is set as the processing time, the time for the thread to process the request must be inserted into the target queue. As the number of processed requests increases, the number of historical times in the target queue also increases. When the number of historical times increases to the same dimension as the TP algorithm, the TP algorithm can be used to set the processing time.
[0101] In addition, by setting the maximum processing time to the processing time, the impact of heavyweight requests on lightweight requests will be reduced. Because heavyweight requests are subject to the maximum processing time, they will not be processed all the time. This improves processing fairness and ensures gateway performance. This effect is particularly evident in customer service systems, because the value-added services connected to the gateway are more complex and diverse.
[0102] Step S4004: Sort the historical durations in the target queue in ascending order.
[0103] Step S4005: Multiply the number of historical durations by the number of indicators of the TP algorithm to obtain a sampling value.
[0104] In this step, a specific example is used to illustrate the step: the number of historical durations is 99, the index number of the TP99 algorithm is 99%, 99×99%=98.01, and 98 is used as the sampling value.
[0105] Step S4006: Filter out the historical duration that ranks at the position of the sampling value from the sorted historical durations according to the sampling value.
[0106] In this step, based on the example given in step S4005, a specific example is used to illustrate this step: based on 98, the historical duration ranked 98th is selected from the sorted historical durations.
[0107] Step S4007: Determine whether the historical duration at the sampling value position is greater than the minimum processing duration corresponding to the service identifier.
[0108] In this step, it is determined whether the historical duration at the sampling value position is greater than the minimum processing duration corresponding to the service identifier. If so, step S4008 is executed; if not, step S4009 is executed.
[0109] Step S4008: Set the historical duration at the sampling value position as the processing duration of the thread processing request.
[0110] In this step, based on the example given in step S4006, this step is explained with a specific example: if the historical duration of the 98th position is 10 seconds, 10 seconds is set as the processing duration of the thread processing the request.
[0111] In addition, since the TP algorithm is an important indicator for measuring performance, the historical duration calculated by the TP algorithm can better represent the processing performance of the thread. When it is set as the processing duration, the setting accuracy is higher, and the processing of requests is limited to a more reasonable time range. This ensures that under normal circumstances, the thread processing of requests is not affected; under abnormal circumstances, the thread processing of other requests is not delayed, and each request is processed fairly.
[0112] Step S4009: Set the minimum processing duration as the processing duration of the thread processing request.
[0113] In this step, different request types, different service identifiers carried in the requests, different services responding to the requests, and different response times. Therefore, the minimum processing time must be set according to the service identifier.
[0114] In addition, the time it takes for a thread to process a lightweight request is much shorter than the time it takes for a thread to process a heavyweight request. Taking into account the performance fluctuations of services that respond to lightweight requests, when the historical time at the sampling value position is less than the minimum processing time, the minimum processing time is used to set the processing time, thereby maximizing the processing of lightweight requests and ensuring the performance of the gateway.
[0115] Step S4010: Start timing when the thread starts processing the request, and determine whether the timing duration is equal to the processing duration.
[0116] In this step, it is determined whether the timing duration is equal to the processing duration. If so, step S4011 is executed; if not, step S4012 is executed.
[0117] It should be noted that when the timing duration is equal to the processing duration, an exception occurs in the processing of the confirmation request. The exception may be a network exception from the gateway to the service or an exception in the service response request.
[0118] Step S4011: terminate the thread processing the request.
[0119] In this step, the request could be a request to obtain a user's profile picture, a request for a weekly consultation session, or a request for a user's order within a month. If the timing duration equals the processing duration, the request processing is abnormal. The thread processing the request is terminated, freeing up thread resources and allowing the thread to process other requests. This fully utilizes thread resources, allowing other requests to be processed promptly, minimizing the impact of the thread processing the request on other requests, and ensuring fair processing of each request.
[0120] Step S4012: Control the thread to continue processing the request.
[0121] In this step, it should be noted that the thread processing the request includes: sending the request to the service, waiting for the service to respond to the request and return a result, and sending the result. Therefore, the thread continuing to process the request includes: continuing to send the request to the service, continuing to wait for the service to respond to the request and return a result, or continuing to send the result. In addition, the service can be a value-added service of the customer service system, etc.
[0122] Step S4013: insert the time duration of the thread processing the request into the target queue according to a first-in-first-out method.
[0123] In this step, when it is implemented, the target queue is a first-in-first-out circular queue. The time it takes for the thread to process the request is inserted into the target queue, and the starting position (head), end position (tail) and insertion position (pos) of the target queue are changed accordingly. Figure 6As shown, the insertion process is explained below with a specific example: Assume that the length of the target queue is 3, so the dimension of the TP algorithm is also 3.
[0124] Initially, tail = head = pos = 0;
[0125] Insert A into the target queue:
[0126] pos = original tail = 0;
[0127] Tail = original tail + 1 = 0 + 1 = 1;
[0128] New tail = Tail MOD (remainder) 3 (the dimension of the TP algorithm is 3) = 1 MOD 3 = 1;
[0129] new tail(1)>original head(0);
[0130] new head=original head=0;
[0131] The target queue changes to:
[0132] list[pos=0]=A;
[0133] tail=1;
[0134] head = 0;
[0135] Insert B into the target queue again:
[0136] pos = original tail = 1;
[0137] Tail = original tail + 1 = 1 + 1 = 2;
[0138] New tail = Tail MOD (remainder) 3 (the dimension of the TP algorithm is 3) = 2MOD3 = 2;
[0139] new tail(2)>original head(0);
[0140] new head=original head=0;
[0141] The target queue changes to:
[0142] list[pos=0]=A, list[pos=1]=B;
[0143] tail=2;
[0144] head = 0;
[0145] Insert C into the target queue again:
[0146] pos = original tail = 2;
[0147] Tail = original tail + 1 = 2 + 1 = 3;
[0148] New tail = Tail MOD (remainder) 3 (the dimension of the TP algorithm is 3) = 3MOD3 = 0;
[0149] new tail(0) = original head(0);
[0150] New head = original head + 1 = 0 + 1 = 1;
[0151] The target queue changes to:
[0152] list[pos=0]=A, list[pos=1]=B, list[pos=2]=C;
[0153] tail=0;
[0154] head=1.
[0155] At this point, the target queue is full and can be used to set the processing duration. The process of inserting the duration of the thread processing the request (the duration of the thread processing the request is represented by D) into the target queue is as follows:
[0156] pos = original tail = 0;
[0157] Tail = original tail + 1 = 0 + 1 = 1;
[0158] New tail = Tail MOD (remainder) 3 (the dimension of the TP algorithm is 3) = 1 MOD 3 = 1;
[0159] new tail(1) = original head(1);
[0160] New head = original head + 1 = 1 + 1 = 2;
[0161] The target queue changes to:
[0162] list[pos=0]=B, list[pos=1]=C, list[pos=2]=D;
[0163] tail=1;
[0164] head=2.
[0165] It should be noted that when inserting into the target queue according to the first-in-first-out method, A, which was inserted first, is removed, B occupies A's position, C occupies B's position, and D occupies C's position.
[0166] In addition, the time it takes for a thread to process a request is inserted into the target queue according to the first-in-first-out method, so that the historical time in the target queue is always the most recent historical time. The most recent historical time reflects the thread's most recent performance. When the processing time is set by reflecting the historical time of the most recent performance, the setting accuracy is higher, and the processing of the request is limited to a more reasonable time range, ensuring that under normal circumstances, the thread processing request is not affected; under abnormal circumstances, the thread processing of other requests is not delayed, and each request is processed fairly.
[0167] It should be noted that the present invention can be applied to a server or a gateway, and specifically to a gateway of a customer service system. Since there are many user-oriented services in the customer service system, in order to provide a good interactive experience, many value-added services are set up in the customer service system. If each value-added service has an external interface, the scale of the interface will be uncontrollable, and the implementation of general functions will be redundant. These problems will become more prominent when the microservice framework (a microservice framework is a design method for a software system that divides a complete software system into small systems that run independently and complete specific functions. These small systems jointly complete the functions that originally required a large system to complete) continues to deepen. Therefore, a gateway is built between the sending end of the request and the value-added service, and the gateway provides a unified interface to the outside world. The gateway is mainly used to send requests to the service, wait for the service to respond to the request and return the result, and send the result. The gateway is also used for security verification, information filtering, etc.
[0168] It should be understood that in this embodiment of the present invention, there is a corresponding relationship between the request type, the requested service identifier, the name of the sampling queue, the service responding to the request, and the dimensions of the TP algorithm used to set the processing duration. This relationship is utilized to set the processing time of newly received requests based on historical durations, thereby achieving request processing isolation.
[0169] In order to solve the problems existing in the prior art, another embodiment of the present invention provides a method for processing a request. In the embodiment of the present invention, Figure 7 As shown, the method includes:
[0170] The gateway receives the request.
[0171] The gateway saves the universal unique identifier (UUID) in the request into a waiting queue preset by the gateway. After saving, the control thread sends the request and the universal unique identifier to the service, and stops the thread from processing the request after sending.
[0172] In this step, it should be noted that the thread processing the request is stopped, the thread is released, and the thread can process other requests.
[0173] The service needs to maintain a receiving queue and save the received requests and UUIDs to the receiving queue. The service responds to the request and gets the result. The service sends the result and UUID to the gateway by calling the gateway's interface.
[0174] In this step, the receiving queue saves the requests to ensure the order of request responses.
[0175] The gateway receives the result and the UUID sent by the service.
[0176] The gateway deletes the universally unique identifier in the waiting queue, and after deletion, controls the thread to send the result to the sending end of the request.
[0177] In addition, the control thread only sends requests and results without waiting for the service to respond to the request and return the result, which makes the request processing restrictive and reduces the impact of the service response time on the fairness of the thread processing the request, thereby processing each request fairly.
[0178] It should be noted that the method of the embodiment of the present invention has the following problems:
[0179] First, the gateway needs to provide an interface to the service so that the service can provide the results to the gateway in a timely manner. The gateway also needs to allocate additional resources to maintain the waiting queue.
[0180] Secondly, the process of service access gateway will become very complicated.
[0181] Thirdly, the performance of the gateway will be affected by the service. While the UUID is stored in the waiting queue, the sender of the request and the gateway must maintain a connection. If the service does not return a result for a long time, the gateway will reject the request.
[0182] Combined with the above Figure 3-Figure 7 Describes how to process requests. Figure 8 Describes the device that handles the request.
[0183] In order to solve the problems existing in the prior art, the embodiment of the present invention provides a device for processing requests, such as Figure 8 As shown, the device includes:
[0184] The setting unit 801 is configured to set a processing duration of a thread processing request according to a service identifier; wherein the request carries the service identifier.
[0185] The control unit 802 is configured to control the thread to process the request according to the processing duration.
[0186] In this embodiment of the present invention, the setting unit 801 is specifically configured to:
[0187] Determine whether to use the TP algorithm to set the processing time of the thread processing request based on the service identifier;
[0188] If so, the TP algorithm is used to set the processing time of the thread processing request;
[0189] If not, the maximum processing time corresponding to the service identifier is set as the processing time of the thread processing request;
[0190] The processing duration is the sum of the duration of the thread sending the request to the service, the duration of the thread waiting for the service to respond to the request and return the result, and the duration of the thread sending the result;
[0191] The service identifier includes information assigned by the gateway to the service, or information provided by the service to the gateway.
[0192] In this embodiment of the present invention, the setting unit 801 is further configured to:
[0193] According to the name of the sampling queue, a sampling queue having the same service identifier as the service identifier is selected from the plurality of sampling queues and used as the target queue;
[0194] Determine whether the number of historical durations in the target queue is equal to the dimension of the TP algorithm;
[0195] The historical duration is the duration during which the thread processes historical requests with the same service identifier.
[0196] In this embodiment of the present invention, the setting unit 801 is further configured to:
[0197] Sort the historical durations in the target queue in ascending order, and multiply the number of historical durations by the index number of the TP algorithm to obtain a sampling value;
[0198] Filtering the historical duration that is at the position of the sampling value from the sorted historical durations according to the sampling value;
[0199] The processing duration of the thread processing request is set according to the historical duration of the sampling value position.
[0200] In this embodiment of the present invention, the setting unit 801 is further configured to:
[0201] Determine whether the historical duration of the sampling value position is greater than the minimum processing duration corresponding to the service identifier;
[0202] If so, setting the historical duration at the sampling value position as the processing duration of the thread processing request;
[0203] If not, the minimum processing time is set to the processing time of the thread processing request. In the embodiment of the present invention, the control unit 802 is specifically used to:
[0204] It is used to insert the time duration for the thread to process the request into the target queue according to a first-in-first-out method after controlling the thread to process the request according to the processing time duration.
[0205] In this embodiment of the present invention, the control unit 802 is further configured to:
[0206] When the thread starts processing the request, the timing is started, and it is determined whether the timing duration is equal to the processing duration;
[0207] If so, the thread is terminated from processing the request; if not, the thread is controlled to continue processing the request.
[0208] It should be understood that the functions performed by the various components of the apparatus for processing requests provided in the embodiment of the present invention have been described in detail in the method for processing requests in the above embodiment, and will not be repeated here.
[0209] Figure 9 An exemplary system architecture 900 is shown to which the method for processing a request or the apparatus for processing a request according to an embodiment of the present invention can be applied.
[0210] like Figure 9 As shown, system architecture 900 may include terminal devices 901, 902, 903, a network 904, and a server 905. Network 904 is used to provide a medium for communication links between terminal devices 901, 902, 903 and server 905. Network 904 may include various connection types, such as wired or wireless communication links or fiber optic cables.
[0211] Users can use terminal devices 901, 902, and 903 to interact with server 905 via network 904 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 901, 902, and 903, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc. (only as examples).
[0212] The terminal devices 901 , 902 , and 903 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers.
[0213] Server 905 may be a server that provides various services, such as a backend management server (for example only) that supports shopping websites browsed by users using terminal devices 901, 902, and 903. The backend management server may analyze and process received data such as product information query requests, and feed back processing results (for example, target push information and product information—for example only) to the terminal device.
[0214] It should be noted that the method for processing requests provided in the embodiment of the present invention is generally executed by the server 905 , and accordingly, the device for processing requests is generally set in the server 905 .
[0215] It should be understood that Figure 9 The number of terminal devices, networks and servers in the embodiment is merely illustrative. Any number of terminal devices, networks and servers may be provided as required.
[0216] Reference below Figure 10 , which shows a schematic structural diagram of a computer system 1000 of a terminal device suitable for implementing an embodiment of the present invention. Figure 10 The terminal device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.
[0217] like Figure 10 As shown, the computer system 1000 includes a central processing unit (CPU) 1001, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 1002 or a program loaded from a storage unit 1008 into a random access memory (RAM) 1003. Various programs and data required for the operation of the system 1000 are also stored in the RAM 1003. The CPU 1001, the ROM 1002, and the RAM 1003 are connected to each other via a bus 1004. An input / output (I / O) interface 1005 is also connected to the bus 1004.
[0218] The following components are connected to the I / O interface 1005: an input section 1006 including a keyboard, a mouse, and the like; an output section 1007 including devices such as a cathode ray tube (CRT), a liquid crystal display (LCD), and a speaker; a storage section 1008 including a hard disk; and a communication section 1009 including a network interface card such as a LAN card or a modem. The communication section 1009 performs communication processing via a network such as the Internet. A drive 1010 is also connected to the I / O interface 1005 as needed. A removable medium 1011, such as a magnetic disk, an optical disk, a magneto-optical disk, or a semiconductor memory, is installed in the drive 1010 as needed, so that computer programs read therefrom can be installed into the storage section 1008 as needed.
[0219] In particular, according to the embodiments disclosed herein, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, the embodiments disclosed herein include a computer program product comprising a computer program carried on a computer-readable medium, the computer program comprising program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network via the communication section 1009, and / or installed from a removable medium 1011. When the computer program is executed by the central processing unit (CPU) 1001, the above-mentioned functions defined in the system of the present invention are performed.
[0220] It should be noted that the computer-readable medium described in the present invention can be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of computer-readable storage media can include, but are not limited to, an electrical connection having one or more conductors, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof. In the present invention, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. In the present invention, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. This propagated data signal can take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A computer-readable signal medium may also be any computer-readable medium other than a computer-readable storage medium that can transmit, propagate, or transport a program for use by or in conjunction with an instruction execution system, apparatus, or device. Program code embodied on a computer-readable medium may be transmitted using any suitable medium, including but not limited to wireless, wireline, optical fiber cable, RF, or any suitable combination thereof.
[0221] The flowcharts and block diagrams in the accompanying drawings illustrate the possible implementation architecture, functions and operations of the systems, methods and computer program products according to various embodiments of the present invention. In this regard, each box in the flowchart or block diagram can represent a unit, program segment, or a part of code, and the above-mentioned unit, program segment, or a part of code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order than that marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flowchart, and the combination of boxes in the block diagram or flowchart, can be implemented with a dedicated hardware-based system that performs the specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0222] The units involved in the embodiments of the present invention may be implemented in software or hardware. The units described may also be provided in a processor. For example, they may be described as: a processor including a receiving unit, a setting unit, and a control unit. The names of these units do not, in some cases, constitute limitations on the units themselves. For example, the setting unit may also be described as a "unit for setting the processing time of a thread processing request according to a service identifier."
[0223] As another aspect, the present invention further provides a computer-readable medium, which may be included in the device described in the above embodiments, or may exist independently and not incorporated into the device. The computer-readable medium carries one or more programs, which, when executed by the device, causes the device to: set a processing duration for a thread to process a request based on a service identifier; wherein the request carries the service identifier; and control the thread to process the request based on the processing duration.
[0224] According to the technical solution of the embodiment of the present invention, by setting the processing time of the thread processing request and controlling the thread processing request according to the processing time, the processing of each request is limited to a reasonable time range, ensuring that under normal circumstances, the thread processing request is not affected; under abnormal circumstances, the thread processing of other requests is not delayed, and each request is processed fairly. The processing time is set through the service identifier, so that various types of requests are processed according to the processing time, thereby improving the applicability of the processing.
[0225] The above specific embodiments do not limit the scope of protection of the present invention. Those skilled in the art will appreciate that various modifications, combinations, sub-combinations, and substitutions may occur depending on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention are intended to be included within the scope of protection of the present invention.
Claims
1. A method for processing a request, characterized in that: include: Setting a processing time of a thread processing request according to a service identifier; wherein the request carries the service identifier; controlling the thread to process the request according to the processing duration; Set the processing time of thread processing requests based on the service identifier, including: Determining whether to use a TP algorithm to set the processing time of a thread processing request based on the service identifier; wherein the TP algorithm is a top percentile algorithm, and the number of historical durations in a target queue is equal to the dimension of the TP algorithm; the target queue is a sampling queue with the same service identifier as a plurality of sampling queues selected based on the name of the sampling queue; If so, the TP algorithm is used to set the processing time of the thread processing request; If not, the maximum processing time corresponding to the service identifier is set as the processing time of the thread processing request; The processing duration is the sum of the duration of the thread sending the request to the service, the duration of the thread waiting for the service to respond to the request and return the result, and the duration of the thread sending the result; The service identifier includes information assigned by the gateway to the service, or information provided by the service to the gateway.
2. The method according to claim 1, characterized in that Determine whether to use the TP algorithm to set the processing time of the thread processing request based on the service identifier, including: According to the name of the sampling queue, a sampling queue having the same service identifier as the target queue is selected from the plurality of sampling queues; Determine whether the number of historical durations in the target queue is equal to the dimension of the TP algorithm; The historical duration is the duration during which the thread processes historical requests with the same service identifier.
3. The method according to claim 2, characterized in that Use the TP algorithm to set the processing time of a thread processing request, including: Sort the historical durations in the target queue in ascending order, and multiply the number of historical durations by the index number of the TP algorithm to obtain a sampling value; Filtering the historical duration that is at the position of the sampling value from the sorted historical durations according to the sampling value; The processing duration of the thread processing request is set according to the historical duration of the sampling value position.
4. The method according to claim 3, characterized in that The processing time of the thread processing request is set according to the historical time of the sampling value position, including: Determine whether the historical duration of the sampling value position is greater than the minimum processing duration corresponding to the service identifier; If so, setting the historical duration at the sampling value position as the processing duration of the thread processing request; If not, the minimum processing duration is set as the processing duration of the thread processing request.
5. The method according to claim 2, characterized in that After controlling the thread to process the request according to the processing duration, the method further includes: The duration of processing the request by the thread is inserted into the target queue according to a first-in-first-out method.
6. The method according to claim 1, characterized in that Controlling the thread to process the request according to the processing duration includes: When the thread starts processing the request, the timing is started, and it is determined whether the timing duration is equal to the processing duration; If so, the thread is terminated from processing the request; if not, the thread is controlled to continue processing the request.
7. A device for processing a request, characterized in that: include: A setting unit, configured to set a processing time of a thread processing request according to a service identifier; wherein the request carries the service identifier; a control unit, configured to control the thread to process the request according to the processing duration; The setting unit is specifically configured to determine, based on the service identifier, whether to use a TP algorithm to set a processing time for a thread processing request; wherein the TP algorithm is a top percentile algorithm, and the number of historical durations in a target queue is equal to a dimension of the TP algorithm; and the target queue is a sampling queue having the same service identifier as a plurality of sampling queues, selected based on the name of the sampling queue; If so, the TP algorithm is used to set the processing time of the thread processing request; If not, the maximum processing time corresponding to the service identifier is set as the processing time of the thread processing request; The processing duration is the sum of the duration of the thread sending the request to the service, the duration of the thread waiting for the service to respond to the request and return the result, and the duration of the thread sending the result; The service identifier includes information assigned by the gateway to the service, or information provided by the service to the gateway.
8. The device according to claim 7, characterized in that The settings unit is also specifically used to: According to the name of the sampling queue, a sampling queue having the same service identifier as the target queue is selected from the plurality of sampling queues; Determine whether the number of historical durations in the target queue is equal to the dimension of the TP algorithm; The historical duration is the duration during which the thread processes historical requests with the same service identifier.
9. The device according to claim 8, characterized in that The settings unit is also specifically used to: Sort the historical durations in the target queue in ascending order, and multiply the number of historical durations by the index number of the TP algorithm to obtain a sampling value; Filtering the historical duration that is at the position of the sampling value from the sorted historical durations according to the sampling value; The processing duration of the thread processing request is set according to the historical duration of the sampling value position.
10. The device according to claim 9, characterized in that The settings unit is also specifically used to: Determine whether the historical duration of the sampling value position is greater than the minimum processing duration corresponding to the service identifier; If so, setting the historical duration at the sampling value position as the processing duration of the thread processing request; If not, the minimum processing duration is set as the processing duration of the thread processing request.
11. The device according to claim 7, characterized in that The control unit is also specifically used for: When the thread starts processing the request, the timing is started, and it is determined whether the timing duration is equal to the processing duration; If so, the thread is terminated from processing the request; if not, the thread is controlled to continue processing the request.
12. An electronic device, characterized in that: include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 6.
13. A computer-readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 6 is implemented.
Citation Information
Patent Citations
Thread pool management method and system
CN101599027A
Method, device and system of server performances testing
CN103064778A