【发布时间】:2017-07-03 21:49:24
【问题描述】:
我在 ASP.NET MVC 中有一个 WebApi,我需要控制访问限制,此外我还需要在运行时更改限制的值。 我在这个网站上实现了这个例子WebApiThrottle(运行时更新速率限制部分)
这是我的 WebApiConfig 中的代码:
//trace provider
var traceWriter = new SystemDiagnosticsTraceWriter()
{
IsVerbose = true
};
config.Services.Replace(typeof(ITraceWriter), traceWriter);
config.EnableSystemDiagnosticsTracing();
//Web API throttling handler
config.MessageHandlers.Add(new ThrottlingHandler(
policy: new ThrottlePolicy(perMinute: 3, perHour: 30, perDay: 35, perWeek: 3000)
{
//scope to IPs
IpThrottling = true,
//scope to clients
ClientThrottling = true,
ClientRules = new Dictionary<string, RateLimits>
{
{ "client-key-1", new RateLimits { PerMinute = 1, PerHour = 60 } }
},
//scope to endpoints
EndpointThrottling = true
},
//replace with PolicyMemoryCacheRepository for Owin self-host
policyRepository: new PolicyCacheRepository(),
//replace with MemoryCacheRepository for Owin self-host
repository: new CacheRepository(),
logger: new TracingThrottleLogger(traceWriter)));
我定义了每分钟三个请求,就像默认值一样,每分钟一个请求到客户端,密钥为“client-key-1”。 但是当我使用 PostMan 进行测试时(我正在传递值为 client-key-1 的授权令牌),我注意到只使用了默认配置,因为只有在三个请求之后我才收到消息:
而且,即使我更新了速率限制,使用函数:
public void UpdateRateLimits()
{
//init policy repo
var policyRepository = new PolicyCacheRepository();
//get policy object from cache
var policy = policyRepository.FirstOrDefault(ThrottleManager.GetPolicyKey());
//update client rate limits
policy.ClientRules["client-key-1"] =
new RateLimits { PerMinute = 20 };
//apply policy updates
ThrottleManager.UpdatePolicy(policy, policyRepository);
}
消息“超出 API 调用配额!每分钟最多允许 3 个。”继续出现。
有人遇到过这个问题吗?
【问题讨论】:
-
什么是
ThrottlePolicy?那是 asp.net 的一部分吗? -
@spender 它来自 WebApiThrottle,我在其中配置 API 的速率限制。
标签: c# asp.net asp.net-web-api