WebApiThrottle中运行时的更新速率限制无效

时间:2017-06-27 20:46:20

标签: c# asp.net asp.net-web-api

我在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传递授权令牌),我注意到只使用了默认配置,因为只有在三次请求之后才收到消息:enter image description here

而且,即使我使用以下功能更新速率限制:

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个。”继续出现。

有人有这个问题吗?

1 个答案:

答案 0 :(得分:0)

在尝试了很多事情后,我可以配置我的课程以考虑我的自定义速率限制。

我做的是:

1)我创建了一个类来覆盖方法SetIdentity,就像在网站WebApiThrottle中一样:

public class CustomThrottlingHandler : ThrottlingHandler
{
    public CustomThrottlingHandler(ThrottlePolicy policy, IPolicyRepository policyRepository, IThrottleRepository repository, IThrottleLogger logger, IIpAddressParser ipAddressParser = null)
        : base(policy, policyRepository, repository, logger, ipAddressParser)
    {

    }

    protected override RequestIdentity SetIdentity(HttpRequestMessage request)
    {
        return new RequestIdentity
        {
            ClientKey = request.Headers.Contains("my-header") ? request.Headers.GetValues("my-header").First() : "anon",
            ClientIp = base.GetClientIp(request).ToString(),
            Endpoint = request.RequestUri.AbsolutePath.ToLowerInvariant()
        };
    }
}

2)我更改了MessageHandlers.Add以使用我的自定义类(CustomThrottlingHandler)而不是类ThrottlingHandler:

public static void Register(HttpConfiguration config)
{
    //trace provider
    var traceWriter = new SystemDiagnosticsTraceWriter()
    {
        IsVerbose = true
    };
    config.Services.Replace(typeof(ITraceWriter), traceWriter);
    config.EnableSystemDiagnosticsTracing();

    //Web API throttling handler
    config.MessageHandlers.Add(new CustomThrottlingHandler(
        policy: new ThrottlePolicy(perMinute: 20, perHour: 30, perDay: 35, perWeek: 3000)
        {
            //scope to IPs
            IpThrottling = true,

            //scope to clients
            ClientThrottling = true,
            ClientRules = new Dictionary<string, RateLimits>
            { 
                { "api-client-key-1", new RateLimits { PerMinute = 60, PerHour = 600 } },
                { "api-client-key-2", new RateLimits { PerDay = 5000 } }
            },

            //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)));
}