Example #1
0
    public void AddPolicy_Generic_ThrowsOnDuplicateName()
    {
        var options = new RateLimiterOptions();

        options.AddPolicy <string>("myKey", context => RateLimitPartition.GetNoLimiter <string>("myKey"));
        Assert.Throws <ArgumentException>(() => options.AddPolicy <string, TestRateLimiterPolicy>("myKey"));
    }
    /// <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);
        }));
    }
Example #4
0
    public void AddPolicy_ThrowsOnNullPolicy()
    {
        var options = new RateLimiterOptions();

        Assert.Throws <ArgumentNullException>(() => options.AddPolicy <string>("myKey", policy: null));
    }
Example #5
0
    public void AddPolicy_ThrowsOnNullPolicyName()
    {
        var options = new RateLimiterOptions();

        Assert.Throws <ArgumentNullException>(() => options.AddPolicy <string>(null, context => RateLimitPartition.GetNoLimiter <string>("myKey")));
    }