Source/NFX/Throttling/SlidingWindowThrottle.cs
/// <summary>
/// Add value to the bucket associated with time
/// </summary>
/// <param name="time">Monotonically increasing time value</param>
/// <param name="value">The value to add to the bucket associated with time</param>
/// <returns>Current running sum</returns>
Add value to the bucket associated with time
Add value to the bucket associated with time
internal override bool Try(DateTime time, double value)
            {
                int nowTimeBucket = (int)(((double)time.Ticks / 10000000) * m_BucketsPerSec);
                if (m_LastTimeBucket == 0)
                    m_LastTimeBucket = nowTimeBucket;
                long bucket = nowTimeBucket & m_BucketMask;
                int  timeDiff = nowTimeBucket - m_LastTimeBucket;
                double origValue = value;
                if (nowTimeBucket < m_LastTimeBucket)
                {
                    if (value > m_Limit)
                        return false;
                    m_Buckets[bucket] = value;
                    m_Sum = value;
                }
                else if (timeDiff == 0)
                {
                    double newSum = m_Sum + value;
                    if (newSum > m_Limit)
                        return false;
                    m_Sum = newSum;
                    m_Buckets[bucket] += value;
                }
                else if (timeDiff >= m_BucketsInterval)
                {
                    int start = (nowTimeBucket - m_BucketsInterval + 1) & m_BucketMask;
                    int end = nowTimeBucket & m_BucketMask;
                    reset(start, end);
                    if (value > m_Limit)
                        return false;
                    m_Buckets[bucket] = value;
                    m_Sum = value;
                }
                else
                {
                    int start, end, validBuckets = m_BucketsInterval - timeDiff;
                    if (validBuckets <= (m_BucketsInterval >> 1))
                    {
                        start = (nowTimeBucket - m_BucketsInterval + 1) & m_BucketMask;
                        end = (m_LastTimeBucket + 1) & m_BucketMask;
                        m_Sum = 0;
                        Debug.Write(string.Format("Summing {0} through {1}", start, end));
                        for (long i = start; i != end; i = (i + 1) & m_BucketMask)
                            m_Sum += m_Buckets[i];
                        start = end;
                        end = nowTimeBucket & m_BucketMask;
                    }
                    else
                    {
                        start = (m_LastTimeBucket - m_BucketsInterval + 1) & m_BucketMask;
                        end = (nowTimeBucket - m_BucketsInterval + 1) & m_BucketMask;
                        Debug.Write(string.Format("Subtracting/resetting {0} through {1}", start, end));
                        for (long i = start; i != end; i = (i + 1) & m_BucketMask)
                        {
                            m_Sum -= m_Buckets[i];
                            m_Buckets[i] = 0;
                        }
                        if (m_Sum < 0) m_Sum = 0;
                        start = (m_LastTimeBucket + 1) & m_BucketMask;
                        end = nowTimeBucket & m_BucketMask;
                    }
                    Debug.Write(string.Format("Resetting {0} through {1}", start, end));
                    reset(start, end);
                    double newSum = m_Sum + value;
                    if (newSum > m_Limit)
                        return false;
                    m_Sum = newSum;
                    m_Buckets[bucket] = value;
                }
                m_LastTimeBucket = nowTimeBucket;
                Debug.Write(ToString(time));
                return true;
            }
internal override bool Try ( DateTime time , double value ) { int nowTimeBucket = ( int ) ( ( ( double ) time . Ticks / 10000000 ) * m_BucketsPerSec ) ; if ( m_LastTimeBucket == 0 ) m_LastTimeBucket = nowTimeBucket ; long bucket = nowTimeBucket & m_BucketMask ; int timeDiff = nowTimeBucket - m_LastTimeBucket ; double origValue = value ; if ( nowTimeBucket < m_LastTimeBucket ) { if ( value > m_Limit ) return false ; m_Buckets [ bucket ] = value ; m_Sum = value ; } else if ( timeDiff == 0 ) { double newSum = m_Sum + value ; if ( newSum > m_Limit ) return false ; m_Sum = newSum ; m_Buckets [ bucket ] += value ; } else if ( timeDiff >= m_BucketsInterval ) { int start = ( nowTimeBucket - m_BucketsInterval + 1 ) & m_BucketMask ; int end = nowTimeBucket & m_BucketMask ; reset ( start , end ) ; if ( value > m_Limit ) return false ; m_Buckets [ bucket ] = value ; m_Sum = value ; } else { int start , end , validBuckets = m_BucketsInterval - timeDiff ; if ( validBuckets <= ( m_BucketsInterval >> 1 ) ) { start = ( nowTimeBucket - m_BucketsInterval + 1 ) & m_BucketMask ; end = ( m_LastTimeBucket + 1 ) & m_BucketMask ; m_Sum = 0 ; Debug . Write ( string . Format ( " " , start , end ) ) ; for ( long i = start ; i != end ; i = ( i + 1 ) & m_BucketMask ) m_Sum += m_Buckets [ i ] ; start = end ; end = nowTimeBucket & m_BucketMask ; } else { start = ( m_LastTimeBucket - m_BucketsInterval + 1 ) & m_BucketMask ; end = ( nowTimeBucket - m_BucketsInterval + 1 ) & m_BucketMask ; Debug . Write ( string . Format ( " " , start , end ) ) ; for ( long i = start ; i != end ; i = ( i + 1 ) & m_BucketMask ) { m_Sum -= m_Buckets [ i ] ; m_Buckets [ i ] = 0 ; } if ( m_Sum < 0 ) m_Sum = 0 ; start = ( m_LastTimeBucket + 1 ) & m_BucketMask ; end = nowTimeBucket & m_BucketMask ; } Debug . Write ( string . Format ( " " , start , end ) ) ; reset ( start , end ) ; double newSum = m_Sum + value ; if ( newSum > m_Limit ) return false ; m_Sum = newSum ; m_Buckets [ bucket ] = value ; } m_LastTimeBucket = nowTimeBucket ; Debug . Write ( ToString ( time ) ) ; return true ; }
Add value to the bucket associated with time
Add value to the bucket associated with time
// (unlikely) Clock was adjusted // Most frequent case // Second most frequent case // Calculate sum of buckets from start to end // Reset values in intermediate buckets since there was no activity there
Current running sum
Current running sum
time
DateTime
Monotonically increasing time value
Monotonically increasing time value
value
double
The value to add to the bucket associated with time
The value to add to the bucket associated with time