public TimeWindowQuantiles(IClock clock, ImmutableArray <Quantile> quantiles, Duration maxAge, int ageBuckets) { _clock = clock; _quantiles = quantiles; _ringBuffer = new CKMSQuantiles[ageBuckets]; for (int i = 0; i < ageBuckets; i++) { _ringBuffer[i] = new CKMSQuantiles(quantiles); } _currentBucket = 0; _lastRotateTimestamp = clock.GetCurrentInstant(); _durationBetweenRotates = maxAge / ageBuckets; }
CKMSQuantiles Rotate() { var currentTime = _clock.GetCurrentInstant(); lock (_lock) { var timeSinceLastRotate = currentTime - _lastRotateTimestamp; while (timeSinceLastRotate > _durationBetweenRotates) { _ringBuffer[_currentBucket] = new CKMSQuantiles(_quantiles); if (++_currentBucket >= _ringBuffer.Length) { _currentBucket = 0; } timeSinceLastRotate -= _durationBetweenRotates; _lastRotateTimestamp += _durationBetweenRotates; } return(_ringBuffer[_currentBucket]); } }