コード例 #1
0
 public TokenBucket(long capacity, IRefillStrategy refillStrategy, ISleepStrategy sleepStrategy)
 {
     _capacity       = capacity;
     _refillStrategy = refillStrategy;
     _sleepStrategy  = sleepStrategy;
     _size           = 0;
 }
コード例 #2
0
ファイル: LeakyBucket.cs プロジェクト: useric/FinanceSharp
 /// <summary>
 ///      Initializes a new instance of the <see cref="LeakyBucket"/> class
 /// </summary>
 /// <param name="capacity">The maximum number of tokens this bucket can hold</param>
 /// <param name="sleep">Defines the <see cref="ISleepStrategy"/> used when <see cref="Consume"/> is invoked
 ///      but the bucket does not have enough tokens yet</param>
 /// <param name="refill">Defines the <see cref="IRefillStrategy"/> that computes how many tokens to add
 ///      back to the bucket each time consumption is attempted</param>
 /// <param name="timeProvider">Defines the <see cref="ITimeProvider"/> used to enforce timeouts when
 ///      invoking <see cref="Consume"/></param>
 public LeakyBucket(long capacity, ISleepStrategy sleep, IRefillStrategy refill, ITimeProvider timeProvider = null)
 {
     _sleep        = sleep;
     _refill       = refill;
     Capacity      = capacity;
     _available    = capacity;
     _timeProvider = timeProvider ?? RealTimeProvider.Instance;
 }
コード例 #3
0
 /// <summary>Use a user defined refill strategy.</summary>
 public Builder WithRefillStrategy(IRefillStrategy refillStrategy)
 {
     if (refillStrategy == null)
     {
         throw new ArgumentNullException("refillStrategy");
     }
     _refillStrategy = refillStrategy;
     return(this);
 }
コード例 #4
0
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
        public static async Task <ITokenBucket> BucketWithRefillStrategy
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
            (long capacity, IRefillStrategy refillStrategy)
        {
            if (capacity <= 0)
            {
                throw new ArgumentOutOfRangeException("capacity", "Must specify a positive number of tokens");
            }
            if (refillStrategy == null)
            {
                throw new ArgumentNullException("refillStrategy");
            }

            return(new TokenBucket(capacity, refillStrategy));
        }