You can override the global ThrottlePolicy with specific limits for known entities using IpRules, ClientRules, or EndpointRules.
- IpRules/ClientRules: Use a
Dictionary<string, RateLimits> where the key is the IP or Client Key. - EndpointRules: Use a
Dictionary<string, RateLimits> where the key is a relative route (e.g., api/search) or a URL segment (e.g., /entry/).
Note: Custom limits only work if a global counterpart is defined. If multiple endpoint rules match a URI, the lower limit is applied.
// Custom IP and Client Rules
config.MessageHandlers.Add(new ThrottlingHandler()
{
Policy = new ThrottlePolicy(perSecond: 1, perMinute: 20, perHour: 200, perDay: 1500)
{
IpThrottling = true,
IpRules = new Dictionary<string, RateLimits>
{
"192.168.1.1", new RateLimits { PerSecond = 2 }
},
ClientThrottling = true,
ClientRules = new Dictionary<string, RateLimits>
{
"api-client-key-1", new RateLimits { PerMinute = 40, PerHour = 400 }
}
},
Repository = new CacheRepository()
});
// Custom Endpoint Rules
config.MessageHandlers.Add(new ThrottlingHandler()
{
Policy = new ThrottlePolicy(perSecond: 1, perMinute: 20, perHour: 200)
{
IpThrottling = true,
ClientThrottling = true,
EndpointThrottling = true,
EndpointRules = new Dictionary<string, RateLimits>
{
"api/search", new RateLimits { PerSecond = 10, PerMinute = 100, PerHour = 1000 }
}
},
Repository = new CacheRepository()
});