如何改变速率限制,而使用RateGate? [英] How to change the rate limit whilst using a RateGate?

查看:163
本文介绍了如何改变速率限制,而使用RateGate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是简洁 RateGate类限制要求我发送到服务器的数量。

I am using the concise RateGate class to limit the number of requests I send to a server.

我的code看起来是这样的:

My code looks something like this:

var RateLimit = 35;

using(var RateGate = new RateGate(RateLimit, TimeSpan.FromSeconds(1)))
{
    for(var Run = 1; Run <= 50; Run++)
    {
        for(var Batch = 0; Batch < 200; Batch++)
        {
            // Do some work, then...

            MyClass MyClass;

            if(MyClass.RateLimitHit)
            {
                RateLimit--;
            }

            RateGate.WaitToProceed();
        }
    }
}

如果(MyClass.RateLimitHit),我需要通过1.降低限速不只是变量 RateLimit ,但在运行限值的实际 RateGate

Inside the if(MyClass.RateLimitHit), I need to lower the rate limit by 1. Not just the variable RateLimit, but the limit running in the actual RateGate.

在RateGate类,我看到这样的:

In the RateGate class, I see this:

/// <summary>
/// Number of occurrences allowed per unit of time.
/// </summary>
public int Occurrences { get; private set; }

我的问题是:如果我改变私定; 设置; 并添加 RateGate.Occurrences = RateLimit; RateLimit - ; 将在做我想做的。

My question is: if I change private set; to set; and add RateGate.Occurrences = RateLimit; after RateLimit--; will this do what I want?

我试过,但它看起来像 RateGate 继续以一个最大速率执行35 / S。

I tried it, but it looks like the RateGate continues to execute at a max rate of 35/s.

推荐答案

我想做到这一点,我找到了一个很好的解决方案通过转换的时间和事件再度发生。 这是什么意思:

I wanted to do that too and I found a nice solution by inverting the time and occurences. What does it mean:

而不是前pressing我的问题是:我想每个第二N事件再度发生,我倒为我希望每1 / N秒1 occurence。通过这种方式,而不是修改出现的次数(这将始终为1),可以容易地改变的时间单位。我说这方法的类(你可以得到太):

Instead of expressing my problem as "I want N occurences per second", I inversed it as "I want 1 occurence per 1/N seconds". That way, instead of modifying the number of occurences (which will always be 1), I could easily change the time unit. I added this method to the class (you could derive too):

private object _updateTimeUnitLock = new object();
private int nextUpdateTime = 0;
public bool UpdateTimeUnit(TimeSpan timeUnit, TimeSpan dontUpdateBefore)
{
    lock (_updateTimeUnitLock)
    {
        if ((nextUpdateTime == 0) || (nextUpdateTime <= Environment.TickCount))
        {
            TimeUnitMilliseconds = (int)timeUnit.TotalMilliseconds;
            nextUpdateTime = Environment.TickCount + (int)dontUpdateBefore.TotalMilliseconds;

            return true;
        }

        return false;
    }
}

矿井必须是线程安全的,我需要一种方法来在某些时期prevent的变化,所以在你身边,你可能想要删除锁定和 dontUpdateBefore 参数,这意味着你可以只设置TimeUnitMilliseconds并且这个值将有所回升,在接下来的计时器滴答。 现在,调用这个,你只需要计算你要根据你要出现的次数新时间。

Mine had to be threadsafe and I needed a way to prevent changes during certain periods, so on your side you may want to remove the lock and dontUpdateBefore param, which means you can just set TimeUnitMilliseconds and this value will be picked up at the next timer tick. Now, to call this, you just need to calculate the new time you want based on the number of occurences you want.

希望它能够满足你的需求。

Hope it could fit your needs.

ñ。

这篇关于如何改变速率限制,而使用RateGate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆