Features
- Update rate limits at runtime with
PolicyCacheRepository
andThrottleManage
- [documentation] (https://github.com/stefanprodan/WebApiThrottle/blob/master/README.md#update-rate-limits-at-runtime) - New interface
IPolicyRepository
used for for storing and retrieving of policy data (global limits, clients rate limits and white-lists) - documentation - New helper class
ThrottleManager
used for customizing the cache keys with prefix/suffix and for policy cache refresh - Attribute-based rate limiting with
ThrottlingFilter
andEnableThrottlingAttribute
- [documentation] (https://github.com/stefanprodan/WebApiThrottle/blob/master/README.md#attribute-based-rate-limiting-with-throttlingfilter-and-enablethrottlingattribute)
Upgrade from older versions
There are no breaking changes in v1.2, you can safely update via NuGet.
If you want to use the rate limits update feature, you’ll need to change the ThrottlingHandler
initialization code and use the new constructor ThrottlingHandler(ThrottlePolicy policy, IPolicyRepository policyRepository, IThrottleRepository repository, IThrottleLogger logger)
.
Register message handler (IIS hosting)
config.MessageHandlers.Add(new ThrottlingHandler(
policy: new ThrottlePolicy(perSecond: 1, perMinute: 20, perHour: 100, perDay: 1500)
{
IpThrottling = true,
ClientThrottling = true,
EndpointThrottling = true
},
policyRepository: new PolicyCacheRepository(),
repository: new CacheRepository(),
logger: null));
Register action filter with rate limits loaded from app.config (IIS hosting)
config.Filters.Add(new ThrottlingFilter(
policy: ThrottlePolicy.FromStore(new PolicyConfigurationProvider()),
policyRepository: new PolicyCacheRepository(),
repository: new CacheRepository(),
logger: null));
Update policy from code (IIS hosting)
//init policy repo
var policyRepository = new PolicyCacheRepository();
//get policy object from cache
var policy = policyRepository.FirstOrDefault(ThrottleManager.GetPolicyKey());
//update client rate limits
policy.ClientRules["api-client-key-1"] =
new RateLimits { PerMinute = 80, PerHour = 800 };
//add new client rate limits
policy.ClientRules.Add("api-client-key-3",
new RateLimits { PerMinute = 60, PerHour = 600 });
//apply policy updates
ThrottleManager.UpdatePolicy(policy, policyRepository);
Register message handler with rate limits loaded from app.config (Owin self-hosting)
config.MessageHandlers.Add(new ThrottlingHandler(
policy: ThrottlePolicy.FromStore(new PolicyConfigurationProvider()),
policyRepository: new PolicyMemoryCacheRepository(),
repository: new MemoryCacheRepository(),
logger: null));