Sentinel series
How Sentinel fuse current limiter works
Sentinel Cloud native K8S deployment combat
Sentinel core source code parsing
Time window flow limiting algorithm
In the time window shown in Figure 10-20, 60 requests are smaller than the threshold 100, and 60 requests can pass. In the time window 30-40, 120 requests are larger than the threshold 100, and 20 requests cannot passCopy the code
disadvantages
So 10 to 16 tons 10 requests 16 to 20 tons 50 requests 20 to 26 tons 60 requests 26 to 30 tons 20 requests 16 to 26 tons and we have 110 requests that are over the threshold but this fixed time window algorithm doesn't limit it and it doesn't limit it for any period of timeCopy the code
Sliding time window
Advance a time window from the yellow line to check whether the number of requests in the time window is greater than the threshold. If the number of requests greater than the threshold is smaller than the threshold, access is allowedCopy the code
This is a situation where you have 8 requests that you can't do through a sliding time window and you can't exceed the threshold in any time period but it also introduces a new problem of "double counting wastes system resources"Copy the code
The two time Windows corresponding to analysis point 1 and analysis point 2 have overlapping statistical partsCopy the code
Improved sliding time window algorithm
As shown in the figure, each time window is 100T divided into 4 segments 25T each segment corresponds to a statistical array element. For example, the segment 100-125 corresponds to A0. This array element records the number of requests within the unit time window 100-125 If the number of requests is 125T-150T, the number of requests counted in this window will be added to a1Copy the code
Determine whether the request at 180T can be approved
Calculate the number of requests between 175T and 180T. If the time window corresponds to A3, obtain a0 statistics + A1 statistics + A2 statistics + (number of requests between 175T and 180T) and check whether the number exceeds the threshold. 100 If the number exceeds the threshold, the number of requests cannot passCopy the code
Sliding time window source code analysis
- The statistics of data
- The use of statistical data
Analyze the method
ArrayMetric
LeapArray
The WindowWrap instance paradigm T is MetricBucket
WindowLengthInMs Sample window length windowStart Start timestamp of the sample window Value Indicates the statistics of the current sample window. The type is MetricBucketCopy the code
MetricBucket
MetricEvent Dimensions of data statistics
The data of six dimensions such as request volume, intercept request volume, abnormal request volume and successful request volume in this time window are collected in LongAddr arrayCopy the code
Gets the sample window of the current point in time
Calculate the id of the sample window where the current time is
Gets the start time of the current time window
First, calculate the time window in which 27T is located: 27/10=2, subscript 0 falls at the position with subscript 2. 2. Calculate the LeapArray element in which the request statistics at 27T accumulate 2%, 4=2, that is, the position of A2Copy the code
Replace the old time window
The object itself hasn't changedCopy the code
Adds the request data for the current window to the statistics for the current sample window
Through dimensional data
How are statistics used
Flow control failed quickly
The past plus the presentCopy the code
Get the previous statistics
Traversal data retrieves and sums data from the Pass dimensionCopy the code
Record the sample window statistics currently traversed into ResultCopy the code
Obsolete to judge
The current time-window start time is outdated if it is larger than the time windowCopy the code