/// <summary> /// Adds a new <see cref="ConcurrencyLimiter"/> with the given <see cref="ConcurrencyLimiterOptions"/> 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="ConcurrencyLimiterOptions"/> to be used for the limiter.</param> /// <returns>This <see cref="RateLimiterOptions"/>.</returns> public static RateLimiterOptions AddConcurrencyLimiter(this RateLimiterOptions options, string policyName, Action <ConcurrencyLimiterOptions> configureOptions) { ArgumentNullException.ThrowIfNull(configureOptions); var key = new PolicyNameKey() { PolicyName = policyName }; var concurrencyLimiterOptions = new ConcurrencyLimiterOptions(); configureOptions.Invoke(concurrencyLimiterOptions); return(options.AddPolicy(policyName, context => { return RateLimitPartition.GetConcurrencyLimiter(key, _ => concurrencyLimiterOptions); })); }
/// <summary> /// Adds a new <see cref="SlidingWindowRateLimiter"/> with the given <see cref="SlidingWindowRateLimiterOptions"/> 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="SlidingWindowRateLimiterOptions"/> to be used for the limiter.</param> /// <returns>This <see cref="RateLimiterOptions"/>.</returns> public static RateLimiterOptions AddSlidingWindowLimiter(this RateLimiterOptions options, string policyName, Action <SlidingWindowRateLimiterOptions> configureOptions) { ArgumentNullException.ThrowIfNull(configureOptions); var key = new PolicyNameKey() { PolicyName = policyName }; var slidingWindowRateLimiterOptions = new SlidingWindowRateLimiterOptions(); configureOptions.Invoke(slidingWindowRateLimiterOptions); // Saves an allocation in GetSlidingWindowLimiter, which would have created a new set of options if this was true. slidingWindowRateLimiterOptions.AutoReplenishment = false; return(options.AddPolicy(policyName, context => { return RateLimitPartition.GetSlidingWindowLimiter(key, _ => slidingWindowRateLimiterOptions); })); }