|
| 1 | +WebApiThrottle |
| 2 | +============== |
| 3 | + |
| 4 | +ASP.NET Web API Throttling handler is designed for controlling the rate of requests that clients |
| 5 | +can make to an Web API based on IP address, client API key and request route. |
| 6 | + |
| 7 | +Web API throttling can be configured using the built-in ThrottlePolicy, you can set multiple limits |
| 8 | +for different scenarios like allowing an IP or Client to make a maximum number of calls per second, per minute, per hour or even per day. |
| 9 | +You can define these limits to address all requests made to an API or you can scope the limits to each API route. |
| 10 | + |
| 11 | +###Global throttling based on IP |
| 12 | + |
| 13 | +The setup bellow will limit the number of requests originated from the same IP. |
| 14 | +If from the same IP, in same second, you'll make a call to <code>api/values</code> and <code>api/values/1</code> the last call will get blocked. |
| 15 | + |
| 16 | +``` cs |
| 17 | +public static class WebApiConfig |
| 18 | +{ |
| 19 | + public static void Register(HttpConfiguration config) |
| 20 | + { |
| 21 | + config.MessageHandlers.Add(new ThrottlingHandler() |
| 22 | + { |
| 23 | + Policy = new ThrottlePolicy(perSecond: 1, perMinute: 20, perHour: 200, perDay: 1500) |
| 24 | + { |
| 25 | + IpThrottling = true |
| 26 | + }, |
| 27 | + Repository = new CacheRepository() |
| 28 | + }); |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +###Endpoint throttling based on IP |
| 34 | + |
| 35 | +If from the same IP, in same second, you'll make two calls to <code>api/values</code> the last call will get blocked. |
| 36 | +But if in the same second you'll call <code>api/values/1</code> too, the request will get throw because it's a different route. |
| 37 | + |
| 38 | +``` cs |
| 39 | +config.MessageHandlers.Add(new ThrottlingHandler() |
| 40 | +{ |
| 41 | + Policy = new ThrottlePolicy(perSecond: 1, perMinute: 30) |
| 42 | + { |
| 43 | + IpThrottling = true, |
| 44 | + EndpointThrottling = true |
| 45 | + }, |
| 46 | + Repository = new CacheRepository() |
| 47 | +}); |
| 48 | +``` |
| 49 | + |
| 50 | +###Endpoint throttling based on IP and Client Key |
| 51 | + |
| 52 | +If a client (identified by an unique API key) from the same IP, in same second, makes two calls to <code>api/values</code>, then the last call will get blocked. |
| 53 | +If you want to apply limits to clients regarding of their IPs then you should set IpThrottling to false. |
| 54 | + |
| 55 | +``` cs |
| 56 | +config.MessageHandlers.Add(new ThrottlingHandler() |
| 57 | +{ |
| 58 | + Policy = new ThrottlePolicy(perSecond: 1, perMinute: 30) |
| 59 | + { |
| 60 | + IpThrottling = true, |
| 61 | + ClientThrottling = true, |
| 62 | + EndpointThrottling = true |
| 63 | + }, |
| 64 | + Repository = new CacheRepository() |
| 65 | +}); |
| 66 | +``` |
| 67 | + |
| 68 | +###IP and/or Client Key White-listing |
| 69 | + |
| 70 | +If requests are initiated from an white-listed IP or Client, then the throttling policy will not be applied and the requests will not get stored. |
| 71 | + |
| 72 | +``` cs |
| 73 | +config.MessageHandlers.Add(new ThrottlingHandler() |
| 74 | +{ |
| 75 | + Policy = new ThrottlePolicy(perSecond: 2, perMinute: 60) |
| 76 | + { |
| 77 | + IpThrottling = true, |
| 78 | + IpWhitelist = new List<string> { "::1", "10.0.0.1" }, |
| 79 | + |
| 80 | + ClientThrottling = true, |
| 81 | + ClientWhitelist = new List<string> { "admin-key" } |
| 82 | + }, |
| 83 | + Repository = new CacheRepository() |
| 84 | +}); |
| 85 | +``` |
| 86 | + |
| 87 | +###IP and/or Client Key custom rate limits |
| 88 | + |
| 89 | +You can define custom limits for known IPs or Client Keys, these limits will override the default ones. Be aware that a custom limit will work only if you have defined a global counterpart. |
| 90 | + |
| 91 | +``` cs |
| 92 | +config.MessageHandlers.Add(new ThrottlingHandler() |
| 93 | +{ |
| 94 | + Policy = new ThrottlePolicy(perSecond: 1, perMinute: 20, perHour: 200, perDay: 1500) |
| 95 | + { |
| 96 | + IpThrottling = true, |
| 97 | + IpRules = new Dictionary<string, RateLimits> |
| 98 | + { |
| 99 | + { "192.168.0.1", new RateLimits { PerSecond = 2 } }, |
| 100 | + { "192.168.1.2", new RateLimits { PerMinute = 30, PerHour = 30*60, PerDay = 30*60*24 } } |
| 101 | + }, |
| 102 | + |
| 103 | + ClientThrottling = true, |
| 104 | + ClientRules = new Dictionary<string, RateLimits> |
| 105 | + { |
| 106 | + { "api-client-key-1", new RateLimits { PerMinute = 40, PerHour = 400 } }, |
| 107 | + { "api-client-key-9", new RateLimits { PerDay = 2000 } } |
| 108 | + } |
| 109 | + }, |
| 110 | + Repository = new CacheRepository() |
| 111 | +}); |
| 112 | +``` |
0 commit comments