.净速率限制。增加分区数量

问题描述 投票:0回答:1

如何在 .Net 速率限制器中处理分区,特别是当这些分区是通过 IP 地址创建时(例如 https://www.milanjovanovic.tech/blog/advanced-rate-limiting-use-cases-in-dotnet#rate -按 IP 地址限制用户)?

似乎当通过IP/用户身份(或任何其他数据,我们无法控制的变体数量)等创建分区时,分区的数量(以及相应的速率限制器)可能会不受控制地增长并保留在内存中永远。我是否缺少一些控制分区寿命的机制或类似的东西?

c# .net rate-limiting
1个回答
1
投票

快速扫描源代码表明分区在上次使用后 10 秒被删除。

private static readonly TimeSpan s_idleTimeLimit = TimeSpan.FromSeconds(10);

...

// In private async Task Heartbeat():
if (rateLimiter.Value.Value.IdleDuration is TimeSpan idleDuration && idleDuration > s_idleTimeLimit)
{
    lock (Lock)
    {
        // Check time again under lock to make sure no one calls Acquire or WaitAsync after checking the time and removing the limiter
        idleDuration = rateLimiter.Value.Value.IdleDuration ?? TimeSpan.Zero;
        if (idleDuration > s_idleTimeLimit)
        {
            // Remove limiter from the lookup table and mark cache as invalid
            // If a request for this partition comes in it will have to create a new limiter now
            // And the next time the timer runs the cache needs to be updated to no longer have a reference to this limiter
            _cacheInvalid = true;
            _limiters.Remove(rateLimiter.Key);

            // We don't want to dispose inside the lock so we need to defer it
            _limitersToDispose.Add(rateLimiter.Value.Value);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.