public void Create_FixedWindow() { var options = new FixedWindowRateLimiterOptions(1, QueueProcessingOrder.OldestFirst, 10, TimeSpan.FromMinutes(1), true); var partition = RateLimitPartition.GetFixedWindowLimiter(1, key => options); var limiter = partition.Factory(1); var fixedWindowLimiter = Assert.IsType <FixedWindowRateLimiter>(limiter); Assert.Equal(options.PermitLimit, fixedWindowLimiter.GetAvailablePermits()); Assert.Equal(options.Window, fixedWindowLimiter.ReplenishmentPeriod); Assert.False(fixedWindowLimiter.IsAutoReplenishing); }
/// <summary> /// Adds a new <see cref="FixedWindowRateLimiter"/> with the given <see cref="FixedWindowRateLimiterOptions"/> to the <see cref="RateLimiterOptions"/>. /// </summary> /// <param name="options">The <see cref="RateLimiterOptions"/> to add a limiter to.</param> /// <param name="policyName">The name that will be associated with the limiter.</param> /// <param name="configureOptions">A callback to configure the <see cref="FixedWindowRateLimiterOptions"/> to be used for the limiter.</param> /// <returns>This <see cref="RateLimiterOptions"/>.</returns> public static RateLimiterOptions AddFixedWindowLimiter(this RateLimiterOptions options, string policyName, Action <FixedWindowRateLimiterOptions> configureOptions) { ArgumentNullException.ThrowIfNull(configureOptions); var key = new PolicyNameKey() { PolicyName = policyName }; var fixedWindowRateLimiterOptions = new FixedWindowRateLimiterOptions(); configureOptions.Invoke(fixedWindowRateLimiterOptions); // Saves an allocation in GetFixedWindowLimiter, which would have created a new set of options if this was true. fixedWindowRateLimiterOptions.AutoReplenishment = false; return(options.AddPolicy(policyName, context => { return RateLimitPartition.GetFixedWindowLimiter(key, _ => fixedWindowRateLimiterOptions); })); }