簡體   English   中英

WebApiThrottle中運行時的更新速率限制無效

[英]Update rate limits at runtime in WebApiThrottle is not working

我在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個。” 繼續出現。

有人有這個問題嗎?

在嘗試了很多事情后,我可以配置我的課程以考慮我的自定義速率限制。

我做的是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM