Ejemplo n.º 1
0
 public TokenBucket(long capacity, IRefillStrategy refillStrategy, ISleepStrategy sleepStrategy)
 {
     _capacity       = capacity;
     _refillStrategy = refillStrategy;
     _sleepStrategy  = sleepStrategy;
     _size           = 0;
 }
Ejemplo n.º 2
0
 /// <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;
 }
Ejemplo n.º 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);
 }
#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));
        }