Background
Sudden high concurrency scenarios often occur in daily life, such as: the reduced price promotion of some popular merchandise during the course of a shopping session, resulting in hundreds of thousands, millions, or even tens of millions of accesses to the platform's service interface, creates a large volume of traffic, where each query inventory request is focused on the database, resulting in inefficiency. In the process of implementing the present invention, the inventor finds that the current limiting measures cannot perform smooth current limiting, and a spike phenomenon can occur, so that network congestion is caused, and user experience is low. The following defects exist in the current limiting measures:
the counter current limiting method has the defects that:
the counter current limit is typically controlled by controlling the number of requests that can pass one second, for example qps is 1000, the algorithm is implemented by counting from the first request, and in the next second, the counter value is incremented by 1 for each request, and if the accumulated number reaches 1000, the subsequent requests are rejected. After one second is finished, the counter value is restored to 0, and counting is restarted. The disadvantage of this algorithm is that if 1000 requests are made for the first 10ms in a second of unit time, then the request will be denied for the next 990ms, resulting in "spurs" and no smooth restriction.
The leakage bucket current limiting method has the defects that:
the leakage Bucket (leakage Bucket) algorithm realizes the thought that water (request) firstly enters the leakage Bucket, the leakage Bucket outputs water at a certain speed (the interface has response rate), when the water flowing speed is too high, the water directly overflows (the access frequency exceeds the interface response rate), then the request is refused, and the leakage Bucket algorithm can be seen to forcibly limit the data transmission rate. Since the leakage bucket always discharges water at a constant speed, it is impossible to cope with a burst flow in a short time.
The token bucket throttling method has the defects that:
the Token Bucket algorithm (Token Bucket) is more easily understood than an algorithm with the same effect but opposite direction as the leak Bucket. Over time, the system will add Token to the bucket at constant 1/QPS intervals (1 ms if qps=1000), and will not add if the bucket is full. The new requests are made temporarily, each take one Token away, and block or deny service if no Token is available. The token bucket algorithm is an improvement to the leaky bucket algorithm, and the bucket algorithm can limit the rate of request calls, and the token bucket algorithm can also allow a certain degree of burst calls while limiting the average rate of calls. The Rate Limit in the tool library provided by Google guava is implemented based on the token bucket algorithm, but this is limited to a single node, and if the system is a distributed system, the QPS of each node is the same, and if the request amount reaches the service interface, the QPS is the same. This approach is not applicable in the distributed case.
The current limiting scheme of the Redis-based String data structure has the defects that:
the interface name can be used as key, the initial value is 1, and the expiration time is set for current limiting; i.e. (key, expire time, 1)) when accessed, if there is the key, the value is increased by 1; (i.e., incr (key)) if not present, a default value of 1 is set; although this method solves the problem of current limiting in the distributed mode, current limiting is performed for a period of time, so that there are two periods of time that do not exceed a threshold value, respectively, but there may be a case where a period of time between the two periods of time exceeds a threshold value, and precise current limiting cannot be performed.
Disclosure of Invention
In order to solve the technical problems or at least partially solve the technical problems, the application provides a service interface-based current limiting method, a service interface-based current limiting device, an electronic device and a storage medium.
In a first aspect, the present application provides a service interface-based current limiting method, including:
determining a calling threshold value of an interface to be called in a sliding time window;
determining a first time interval according to the current time stamp and the sliding time window;
counting the first calling times of the interface to be called in the first time interval;
and when the first calling times are larger than the calling threshold value, executing the current limiting operation.
Optionally, the method further comprises:
acquiring an ordered set in a cache;
and determining the sliding time window according to the weight values in the ordered set.
Optionally, the determining the call threshold of the interface to be called in the sliding time window includes:
acquiring an interface identifier of the interface to be called;
and inquiring the calling threshold value of the interface to be called in each sliding time window from the cache according to the interface identification.
Optionally, the method further comprises:
when the calling threshold value of the interface to be called does not exist in the cache, reading the calling threshold value of the interface to be called from a database according to the interface identifier;
synchronizing the calling threshold value to the cache, and distributing expiration time for the calling threshold value.
Optionally, the counting the first call times of the interface to be called in the first time interval includes:
determining a second time interval from the start time to the current timestamp;
acquiring second calling times of the interface to be called in the second time interval;
calculating a third time interval obtained by subtracting the first time interval from the second time interval;
counting the third calling times of the interface to be called in the third time interval;
and calculating the difference value between the second calling times and the third calling times, and determining the first calling times according to the difference value.
Optionally, the performing a current limiting operation includes:
refusing the call request;
or alternatively, the first and second heat exchangers may be,
and placing the call request into a queue, and sequencing according to the priority of the call request.
Optionally, the method further comprises:
when the first calling times are smaller than or equal to the calling threshold value, the interface identification of the interface to be called is used as a key name to be added to an ordered set in a cache, and a weight value and a key value corresponding to the interface identification are updated to be the current timestamp in the ordered set.
In a second aspect, the present application provides a service interface-based current limiting device, including:
the determining module is used for determining a calling threshold value of the interface to be called in the sliding time window;
the acquisition module is used for determining a first time interval according to the current time stamp and the sliding time window;
the statistics module is used for counting the first calling times of the interface to be called in the first time interval;
and the execution module is used for executing the current limiting operation when the first calling times are larger than the calling threshold value.
In a third aspect, the present application provides an electronic device, including: the device comprises a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory are communicated with each other through the communication bus;
the memory is used for storing a computer program;
the processor is configured to implement the above-mentioned method steps when executing the computer program.
In a fourth aspect, the present application provides a computer-readable storage medium having stored thereon a computer program which, when executed by a processor, performs the above-mentioned method steps.
Compared with the prior art, the technical scheme provided by the embodiment of the application has the following advantages: by adopting the design of the sliding time window, the calling times of interfaces in two adjacent time periods can not exceed a preset calling threshold, and the calling times of any time interval in the two time periods can not exceed the preset calling threshold, so that smooth current limiting is achieved. In addition, the current limiting method provided by the application can also be used for coping with burst flow and accurately limiting the current.
Detailed Description
For the purposes of making the objects, technical solutions and advantages of the embodiments of the present application more clear, the technical solutions of the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application, and it is apparent that the described embodiments are some embodiments of the present application, but not all embodiments. All other embodiments, which can be made by one of ordinary skill in the art without undue burden from the present disclosure, are within the scope of the present application based on the embodiments herein.
The application provides a service interface-based current limiting method, a service interface-based current limiting device, electronic equipment and a storage medium. The method provided by the embodiment of the invention can be applied to any needed electronic equipment, for example, the electronic equipment can be a server, a terminal and the like, is not particularly limited, and is convenient to describe and is called as the electronic equipment for short hereinafter.
The following first describes a service interface-based current limiting method provided by an embodiment of the present invention.
Fig. 1 is a flowchart of a service interface-based current limiting method according to an embodiment of the present application. As shown in fig. 1, the method comprises the steps of:
step S11, determining a calling threshold value of an interface to be called in a sliding time window;
step S12, determining a first time interval according to the current time stamp and the sliding time window;
step S13, counting first calling times of interfaces to be called in a first time interval;
step S14, when the first calling times are larger than the calling threshold value, the current limiting operation is executed.
In this embodiment, by adopting the design of the sliding time window, the number of calls of the interface in two adjacent time periods is not more than the preset call threshold, and the number of calls in any time period in the two time periods is not more than the preset call threshold.
For example: the first time period is 0-1min, the second time period is 1-2min, the calling threshold value of the interface in each time period is 100 times/min, the calling times are 20 times and the calling times are 70 times when the first time period is 0-0.5min, and the calling times are 0.5-1 min. And when the second time period is 1-1.5min, the calling times are 60 times, and the calling times of 0.5-1 min are 30 times. Therefore, the calling times in two adjacent time periods do not exceed the calling threshold, but the calling times in the time period of 0.5-1.5min are larger than the calling threshold, so that the database is congested in a short time. However, by designing the sliding time window, smooth flow restriction can be performed in any time period, burst flow can be handled, and accurate flow restriction can be performed.
In this embodiment, a call request is received, and the call request may be initiated by the client. And determining a service interface to be called according to the call request, and determining a call threshold value of the interface to be called in each sliding time window.
The ordered set (zset) data structure in the Redis cache (Remote Dictionary Server remote dictionary service) utilized by the present embodiment includes: a weight value (score), an interface identifier (key) and a key value (value), and setting a sliding time window corresponding to each interface through the weight value (score).
In this embodiment, an interface identifier of an interface to be invoked is determined, and an invocation threshold value of the interface to be invoked in each sliding time window is queried from a cache according to the interface identifier. Specifically, fig. 2 is a schematic diagram of a storage structure of an interface identifier and a call threshold provided in an embodiment of the present application, as shown in fig. 2, in this embodiment, the interface identifier and the call threshold are stored in a list form, first, according to an interface to be called, a matched interface identifier is queried from a header of a list, and then, the call threshold corresponding to the interface identifier is used as a call threshold of the interface to be called in a sliding time window.
For example: m1 is the interface identifier of the payment interface, the call threshold value n1 of which in the sliding time window is 500 times, m2 is the interface identifier of the order generation interface, the call threshold value n2 in the sliding time window is 550 times, m3 is the interface identifier of the store collection interface, and the call threshold value n3 in the sliding time window is 300 times.
In this embodiment, when there is no call threshold of the interface to be called in the cache, the call threshold of the interface to be called is read from the database according to the interface identifier, the call threshold of the interface to be called is synchronized to the cache, and an expiration time is allocated to the call threshold. According to the embodiment of the application, the pressure of the database can be reduced by accessing the cache first, and the database is prevented from being jammed due to the fact that the database is accessed simultaneously. In addition, assigning an expiration time to the call threshold can prevent the cached content from being excessively occupied.
In order to perform accurate current limiting, the current time stamp is acquired by the method and the device in a microsecond unit, and then a first time interval between the current time stamp and a current time window is determined, wherein the time length of the first time interval is equal to that of the sliding time window.
And then counting the calling times of the interface to be called in the first time interval. Fig. 3 is a schematic diagram of a sliding time window provided in the embodiment of the present application, as shown in fig. 3, the current timestamp is now Tu, and a first time interval between the current timestamp and the sliding time window is now Tu-period 1000×1000—now Tu.
Optionally, counting the number of times of calling the interface to be called in the first time interval is specifically implemented in the following manner: determining a second time interval from the start time to the current timestamp; acquiring second calling times of interfaces to be called in a second time interval; calculating a third time interval obtained by subtracting the first time interval from the second time interval; counting the third calling times of the interface to be called in a third time interval; and calculating the difference value between the second calling times and the third calling times, and determining the first calling times according to the difference value.
Taking a payment interface as an example, a second time interval from the start time to the current time stamp is 0-now Tu, a second call number (i.e. a total call number) of the payment interface in the second time interval is 1600, and then a third time interval obtained by subtracting the first time interval from the second time interval is calculated to be 0-now Tu-period 1000. Counting to obtain the third calling times of the payment interface in the third time interval as 1000 times, wherein the difference between the second calling times and the third calling times is 1600-1000 times, and determining the obtained difference as the first calling times of the payment interface in the first time interval, namely 600 times.
As in the example above, 600 times greater than 500 times, i.e., when the first number of invocations is confirmed to be greater than the invocation threshold, the current limit operation is performed.
Optionally, the current limiting operation is performed, which may be: and obtaining a third calling number which is larger than a calling threshold value in the first calling number, and rejecting the calling requests with the number corresponding to the third calling number. For example: the call request corresponding to the call times of 501-600 is rejected.
Or, putting the call requests with the number corresponding to the third call times into a queue, and sequencing according to the priority of the call requests. As in the above example, call requests corresponding to call times 501-600 are placed in a queue, and the priority of the call requests is determined, where the priority may be ordered by time, or it may be determined that the call requests correspond to member information, such as a member level, etc.
In this embodiment, when the first call number is less than or equal to the call threshold, the interface identifier of the interface to be called is added as a key name to the ordered set in the cache, and the weight value and the key value corresponding to the interface identifier are updated to be the current timestamp in the ordered set. And adding expiration time for the interface identification of the interface to be called. The interface is prevented from being not called for a period of time, so that the memory of the cache is prevented from being too high.
In this embodiment, the interface identifier of the interface to be called is added as a key name to the ordered set in the cache, and the weight value and the key value corresponding to the interface identifier are updated to be the current timestamp in the ordered set. The time interval during which the current limit is performed and the time interval during which the current limit is not performed can be counted according to the ordered set (zset) later. Specifically, the weight value in the ordered set may be determined to be the time interval in which the current limiting operation is not performed and the time interval in which the current limiting operation is performed.
As one example, the call threshold for some service interfaces to slide a time window over a particular period of time may be determined by counting the weight values (score) of the ordered set (zset).
For example: through counting the weight value (score) of the ordered set (zset), it is determined that 90% of the sliding time window corresponding to the payment interface in 21:00-22:00 is subjected to the current limiting operation, so that the calling threshold corresponding to the sliding time window of the payment interface in 21:00-22:00 can be increased, and the user experience is ensured.
As an example, it may also be determined by counting the weight values (score) in the ordered set (zset) that certain service interfaces do not set the sliding time window for a certain period of time, or increase the length of time of the sliding time window during that period of time.
For example: by counting the weight values (score) in the ordered set (zset) over 24 hours, it can be determined that the commodity collection interface does not perform a current limit operation for 98% of the sliding time windows in the range of 2:00am-11:00 am. Thus, the length of the sliding time window of the commodity collection interface in the period of 2:00am-11:00am can be increased, or the sliding time window is not set for the commodity collection interface in the period of 2:00am-11:00 am.
As one example, it may also be determined that some service interfaces shorten the length of the sliding time window for a particular period of time by analyzing the weight values (score) in the ordered set (zset).
For example: the method has the advantages that the time length of a sliding time window of interfaces such as commodity collection, shopping cart addition, payment, order generation and the like is shortened in the early morning of shopping, and under the condition that the calling threshold value is unchanged, so that burst flow can be dealt with, and effective flow limiting is performed.
Fig. 4 is a block diagram of a service interface-based current limiting device according to an embodiment of the present application, where the device may be implemented as part or all of an electronic device by software, hardware, or a combination of both. As shown in fig. 4, the apparatus includes:
a determining module 41, configured to determine a call threshold of the interface to be called within the sliding time window;
an obtaining module 42, configured to determine a first time interval according to the current timestamp and the sliding time window;
a statistics module 43, configured to count a first number of calls of the interface to be called in a first time interval;
and an execution module 44, configured to execute the current limiting operation when the first call number is greater than the call threshold.
The embodiment of the application further provides an electronic device, as shown in fig. 5, the electronic device may include: the device comprises a processor 1501, a communication interface 1502, a memory 1503 and a communication bus 1504, wherein the processor 1501, the communication interface 1502 and the memory 1503 are in communication with each other through the communication bus 1504.
A memory 1503 for storing a computer program;
the processor 1501, when executing the computer program stored in the memory 1503, implements the steps of the above embodiments.
The communication bus mentioned above for the electronic devices may be a peripheral component interconnect standard (Peripheral Component Interconnect, pi) bus or an extended industry standard architecture (Extended Industry Standard Architecture, EISA) bus, etc. The communication bus may be classified as an address bus, a data bus, a control bus, or the like. For ease of illustration, the figures are shown with only one bold line, but not with only one bus or one type of bus.
The communication interface is used for communication between the electronic device and other devices.
The Memory may include random access Memory (Random Access Memory, RAM) or may include Non-Volatile Memory (NVM), such as at least one disk Memory. Optionally, the memory may also be at least one memory device located remotely from the aforementioned processor.
The processor may be a general-purpose processor, including a central processing unit (Central Processing Unit, CPU), a network processor (Network Processor, NP), etc.; but also Digital signal processors (Digital SignalProcessing, DSP), application specific integrated circuits (Application Specific Integrated Circuit, ASIC), field-programmable gate arrays (Field-Programmable Gate Array, FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
The present application also provides a computer readable storage medium having stored thereon a computer program which, when executed by a processor, performs the above steps.
It should be noted that, with respect to the apparatus, electronic device, and computer-readable storage medium embodiments described above, since they are substantially similar to the method embodiments, the description is relatively simple, and reference should be made to the description of the method embodiments for relevant points.
It is further noted that relational terms such as "first" and "second", and the like, are used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Moreover, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one … …" does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises the element.
The foregoing is only a specific embodiment of the invention to enable those skilled in the art to understand or practice the invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the generic principles defined herein may be applied to other embodiments without departing from the spirit or scope of the invention. Thus, the present invention is not intended to be limited to the embodiments shown herein but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.