using System; using System.Collections.Generic; namespace RateLimiter { /// /// that is able to save own state. /// public sealed class PersistentCountByIntervalAwaitableConstraint : CountByIntervalAwaitableConstraint { private readonly Action _SaveStateAction; /// /// Create an instance of . /// /// Maximum actions allowed per time interval. /// Time interval limits are applied for. /// Action is used to save state. /// Initial timestamps. public PersistentCountByIntervalAwaitableConstraint(int count, TimeSpan timeSpan, Action saveStateAction, IEnumerable initialTimeStamps) : base(count, timeSpan) { _SaveStateAction = saveStateAction; if (initialTimeStamps == null) return; foreach (var timeStamp in initialTimeStamps) { _TimeStamps.Push(timeStamp); } } /// /// Save state /// protected override void OnEnded(DateTime now) { _SaveStateAction(now); } } }