public BasePolicy(IOptions <QueuePolicyOptions> options, QueueProcessingOrder order) { var queuePolicyOptions = options.Value; var maxConcurrentRequests = queuePolicyOptions.MaxConcurrentRequests; if (maxConcurrentRequests <= 0) { throw new ArgumentException("MaxConcurrentRequests must be a positive integer.", nameof(options)); } var requestQueueLimit = queuePolicyOptions.RequestQueueLimit; if (requestQueueLimit < 0) { throw new ArgumentException("The RequestQueueLimit cannot be a negative number.", nameof(options)); } _limiter = new Limiter(new LimiterOptions { PermitLimit = maxConcurrentRequests, QueueProcessingOrder = order, QueueLimit = requestQueueLimit }); }
/// <summary> /// Initializes the <see cref="TokenBucketRateLimiterOptions"/>. /// </summary> /// <param name="tokenLimit">Maximum number of tokens that can be in the token bucket.</param> /// <param name="queueProcessingOrder"></param> /// <param name="queueLimit">Maximum number of unprocessed tokens waiting via <see cref="RateLimiter.WaitAsync(int, CancellationToken)"/>.</param> /// <param name="replenishmentPeriod"> /// Specifies how often tokens can be replenished. Replenishing is triggered either by an internal timer if <paramref name="autoReplenishment"/> is true, or by calling <see cref="TokenBucketRateLimiter.TryReplenish"/>. /// </param> /// <param name="tokensPerPeriod">Specified how many tokens can be added to the token bucket on a successful replenish. Available token count will not exceed <paramref name="tokenLimit"/>.</param> /// <param name="autoReplenishment"> /// Specifies whether token replenishment will be handled by the <see cref="TokenBucketRateLimiter"/> or by another party via <see cref="TokenBucketRateLimiter.TryReplenish"/>. /// </param> /// <exception cref="ArgumentOutOfRangeException">When <paramref name="tokenLimit"/>, <paramref name="queueLimit"/>, or <paramref name="tokensPerPeriod"/> are less than 0 /// or when <paramref name="replenishmentPeriod"/> is more than 49 days.</exception> public TokenBucketRateLimiterOptions( int tokenLimit, QueueProcessingOrder queueProcessingOrder, int queueLimit, TimeSpan replenishmentPeriod, int tokensPerPeriod, bool autoReplenishment = true) { if (tokenLimit < 0) { throw new ArgumentOutOfRangeException(nameof(tokenLimit)); } if (queueLimit < 0) { throw new ArgumentOutOfRangeException(nameof(queueLimit)); } if (tokensPerPeriod < 0) { throw new ArgumentOutOfRangeException(nameof(tokensPerPeriod)); } if (replenishmentPeriod.TotalDays > 49) { throw new ArgumentOutOfRangeException(nameof(replenishmentPeriod), "Over 49 days is not supported"); } TokenLimit = tokenLimit; QueueProcessingOrder = queueProcessingOrder; QueueLimit = queueLimit; ReplenishmentPeriod = replenishmentPeriod; TokensPerPeriod = tokensPerPeriod; AutoReplenishment = autoReplenishment; }
/// <summary> /// Initializes the <see cref="TokenBucketRateLimiterOptions"/>. /// </summary> /// <param name="tokenLimit">Maximum number of tokens that can be in the token bucket.</param> /// <param name="queueProcessingOrder"></param> /// <param name="queueLimit">Maximum number of unprocessed tokens waiting via <see cref="RateLimiter.WaitAsync(int, CancellationToken)"/>.</param> /// <param name="replenishmentPeriod"> /// Specifies how often tokens can be replenished. Replenishing is triggered either by an internal timer if <paramref name="autoReplenishment"/> is true, or by calling <see cref="TokenBucketRateLimiter.TryReplenish"/>. /// </param> /// <param name="tokensPerPeriod">Specified how many tokens can be added to the token bucket on a successful replenish. Available token count will not exceed <paramref name="tokenLimit"/>.</param> /// <param name="autoReplenishment"> /// Specifies whether token replenishment will be handled by the <see cref="TokenBucketRateLimiter"/> or by another party via <see cref="TokenBucketRateLimiter.TryReplenish"/>. /// </param> /// <exception cref="ArgumentOutOfRangeException">When <paramref name="tokenLimit"/>, <paramref name="queueLimit"/>, or <paramref name="tokensPerPeriod"/> are less than 0 /// or when <paramref name="replenishmentPeriod"/> is more than 49 days.</exception> public TokenBucketRateLimiterOptions( int tokenLimit, QueueProcessingOrder queueProcessingOrder, int queueLimit, TimeSpan replenishmentPeriod, int tokensPerPeriod, bool autoReplenishment = true) { if (tokenLimit < 0) { throw new ArgumentOutOfRangeException(nameof(tokenLimit)); } if (queueLimit < 0) { throw new ArgumentOutOfRangeException(nameof(queueLimit)); } if (tokensPerPeriod <= 0) { throw new ArgumentOutOfRangeException(nameof(tokensPerPeriod)); } if (replenishmentPeriod.TotalDays > 49) { // Environment.TickCount is an int and represents milliseconds since system started // it has a range of -2B - +2B, we cast it to a uint to get a range of 0 - 4B which is 49.7 days before the value will repeat throw new ArgumentOutOfRangeException(nameof(replenishmentPeriod), replenishmentPeriod, SR.ReplenishmentLimitTooHigh); } TokenLimit = tokenLimit; QueueProcessingOrder = queueProcessingOrder; QueueLimit = queueLimit; ReplenishmentPeriod = replenishmentPeriod; TokensPerPeriod = tokensPerPeriod; AutoReplenishment = autoReplenishment; }
/// <summary> /// Initializes the <see cref="SlidingWindowRateLimiterOptions"/>. /// </summary> /// <param name="permitLimit">Maximum number of request counters that can be served in a window.</param> /// <param name="queueProcessingOrder"></param> /// <param name="queueLimit">Maximum number of unprocessed request counters waiting via <see cref="RateLimiter.WaitAndAcquireAsync(int, CancellationToken)"/>.</param> /// <param name="window"> /// Specifies how often requests can be replenished. Replenishing is triggered either by an internal timer if <paramref name="autoReplenishment"/> is true, or by calling <see cref="SlidingWindowRateLimiter.TryReplenish"/>. /// </param> /// <param name="segmentsPerWindow">Specified how many segments a window can be divided into. The total requests a segment can serve cannot exceed the max limit.<paramref name="permitLimit"/>.</param> /// <param name="autoReplenishment"> /// Specifies whether request replenishment will be handled by the <see cref="SlidingWindowRateLimiter"/> or by another party via <see cref="SlidingWindowRateLimiter.TryReplenish"/>. /// </param> /// <exception cref="ArgumentOutOfRangeException">When <paramref name="permitLimit"/>, <paramref name="queueLimit"/>, or <paramref name="segmentsPerWindow"/> are less than 0. </exception> public SlidingWindowRateLimiterOptions( int permitLimit, QueueProcessingOrder queueProcessingOrder, int queueLimit, TimeSpan window, int segmentsPerWindow, bool autoReplenishment = true) { if (permitLimit < 0) { throw new ArgumentOutOfRangeException(nameof(permitLimit)); } if (queueLimit < 0) { throw new ArgumentOutOfRangeException(nameof(queueLimit)); } if (segmentsPerWindow <= 0) { throw new ArgumentOutOfRangeException(nameof(segmentsPerWindow)); } PermitLimit = permitLimit; QueueProcessingOrder = queueProcessingOrder; QueueLimit = queueLimit; Window = window; SegmentsPerWindow = segmentsPerWindow; AutoReplenishment = autoReplenishment; }
/// <summary> /// Initializes the <see cref="ConcurrencyLimiterOptions"/>. /// </summary> /// <param name="permitLimit">Maximum number of permits that can be leased concurrently.</param> /// <param name="queueProcessingOrder">Determines the behaviour of <see cref="RateLimiter.WaitAndAcquireAsync"/> when not enough resources can be leased.</param> /// <param name="queueLimit">Maximum number of permits that can be queued concurrently.</param> /// <exception cref="ArgumentOutOfRangeException">When <paramref name="permitLimit"/> or <paramref name="queueLimit"/> are less than 0.</exception> public ConcurrencyLimiterOptions(int permitLimit, QueueProcessingOrder queueProcessingOrder, int queueLimit) { if (permitLimit < 0) { throw new ArgumentOutOfRangeException(nameof(permitLimit)); } if (queueLimit < 0) { throw new ArgumentOutOfRangeException(nameof(queueLimit)); } PermitLimit = permitLimit; QueueProcessingOrder = queueProcessingOrder; QueueLimit = queueLimit; }