I have configured like the below.
Startup: ConfigureServices Service Method
//API throattling
// needed to load configuration from appsettings.json
services.AddOptions();
// needed to store rate limit counters and ip rules
services.AddMemoryCache();
//load general configuration from appsettings.json
services.Configure(Configuration.GetSection("ClientRateLimiting"));
//load client rules from appsettings.json
services.Configure(Configuration.GetSection("ClientRateLimitPolicies"));
// inject counter and rules stores
services.AddSingleton<IClientPolicyStore, MemoryCacheClientPolicyStore>();
services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
// inject counter and rules distributed cache stores
//services.AddSingleton<IClientPolicyStore, DistributedCacheClientPolicyStore>();
//services.AddSingleton<IRateLimitCounterStore, DistributedCacheRateLimitCounterStore>();
var opt = new ClientRateLimitOptions();
ConfigurationBinder.Bind(Configuration.GetSection("ClientRateLimiting"), opt);
Startup: Configure method
app.UseClientRateLimiting();
appsettings.json
"ClientRateLimiting": {
"EnableEndpointRateLimiting": false,
"StackBlockedRequests": false,
"ClientIdHeader": "X-ClientId",
"HttpStatusCode": 429,
"EndpointWhitelist": [ "get:/solr/xxx", "get:/entity/yyy", "get:/entity/zzz" ],
"ClientWhitelist": [ "cMGAysfu_LfJ7HU8lYFlWtTyWMj85WdbmPShMWbBSgw" ],
"GeneralRules": [
{
"Endpoint": "",
"Period": "1s",
"Limit": 2
},
{
"Endpoint": "",
"Period": "15m",
"Limit": 2
},
{
"Endpoint": "",
"Period": "12h",
"Limit": 4
},
{
"Endpoint": "",
"Period": "7d",
"Limit": 10
}
]
},
"ClientRateLimitPolicies": {
"ClientRules": [
{
"ClientId": "cMGAysfu_LfJ7HU8lYFlWtTyWMj85WdbmPShMWbBSgw",
"Rules": [
{
"Endpoint": "/entity/yyy",
"Period": "1s",
"Limit": 1
},
{
"Endpoint": "/entity/yyy",
"Period": "15m",
"Limit": 5
}
]
}
]
}
Calling the service from PostMan tool below
http://localhost:57439/entity/yyy
and passing the auth token in the header and hit the same service 10 times but not received 429 error code.
Expected 6th time should get 429 error code.
Please see if you can help me on this.
Regards,
Ram