示例#1
0
    /// <summary>
    /// Adds a new rate limiting policy with the given <paramref name="policyName"/>
    /// </summary>
    /// <param name="policyName">The name to be associated with the given <see cref="RateLimiter"/>.</param>
    /// <param name="partitioner">Method called every time an Acquire or WaitAsync call is made to determine what rate limiter to apply to the request.</param>
    public RateLimiterOptions AddPolicy <TPartitionKey>(string policyName, Func <HttpContext, RateLimitPartition <TPartitionKey> > partitioner)
    {
        ArgumentNullException.ThrowIfNull(policyName);
        ArgumentNullException.ThrowIfNull(partitioner);

        if (PolicyMap.ContainsKey(policyName) || UnactivatedPolicyMap.ContainsKey(policyName))
        {
            throw new ArgumentException($"There already exists a policy with the name {policyName}.", nameof(policyName));
        }

        PolicyMap.Add(policyName, new DefaultRateLimiterPolicy(ConvertPartitioner <TPartitionKey>(policyName, partitioner), null));

        return(this);
    }
示例#2
0
    /// <summary>
    /// Adds a new rate limiting policy with the given policyName.
    /// </summary>
    /// <param name="policyName">The name to be associated with the given <see cref="IRateLimiterPolicy{TPartitionKey}"/>.</param>
    /// <param name="policy">The <see cref="IRateLimiterPolicy{TPartitionKey}"/> to be applied.</param>
    public RateLimiterOptions AddPolicy <TPartitionKey>(string policyName, IRateLimiterPolicy <TPartitionKey> policy)
    {
        ArgumentNullException.ThrowIfNull(policyName);

        if (PolicyMap.ContainsKey(policyName) || UnactivatedPolicyMap.ContainsKey(policyName))
        {
            throw new ArgumentException($"There already exists a policy with the name {policyName}.", nameof(policyName));
        }

        ArgumentNullException.ThrowIfNull(policy);

        PolicyMap.Add(policyName, new DefaultRateLimiterPolicy(ConvertPartitioner <TPartitionKey>(policyName, policy.GetPartition), policy.OnRejected));

        return(this);
    }