/// <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); }
/// <summary> /// Adds the specified rate limiting policy to the endpoint(s). /// </summary> /// <param name="builder">The endpoint convention builder.</param> /// <param name="policy">The rate limiting policy to add to the endpoint.</param> /// <returns>The original convention builder parameter.</returns> public static TBuilder RequireRateLimiting <TBuilder, TPartitionKey>(this TBuilder builder, IRateLimiterPolicy <TPartitionKey> policy) where TBuilder : IEndpointConventionBuilder { ArgumentNullException.ThrowIfNull(builder); ArgumentNullException.ThrowIfNull(policy); builder.Add(endpointBuilder => { endpointBuilder.Metadata.Add(new EnableRateLimitingAttribute(new DefaultRateLimiterPolicy(RateLimiterOptions.ConvertPartitioner <TPartitionKey>(null, policy.GetPartition), policy.OnRejected))); }); return(builder); }