Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: download limiter #1003

Closed
wants to merge 3 commits into from
Closed

Conversation

Eric-Joker
Copy link
Contributor

@Eric-Joker Eric-Joker commented Jul 31, 2024

使用令牌桶算法尽量避免在大额限速时限速失效
close #996

@Scighost
Copy link
Owner

在修复游戏的测试中,仍然会有超出限速的状况,稳定几秒触发一次,改成滑动窗口有没有可能改善。

超出限速后的等待的代码改成这一段更合适

RateLimitLease lease;
do
{
    lease = await InstallGameManager.rateLimiter.AcquireAsync(buffer.Length, cancellationToken).ConfigureAwait(false);
    if (!lease.IsAcquired)
    {
        if (lease.TryGetMetadata(MetadataName.RetryAfter, out TimeSpan retryAfter))
        {
            await Task.Delay(retry);
        }
        else
        {
            await Task.Delay(1, cancellationToken).ConfigureAwait(false);
        }
    }
 } while (!lease.IsAcquired);

使用 RateLimiter.GetStatistics() 能不能做到代替已有的速度统计功能

@Eric-Joker
Copy link
Contributor Author

Eric-Joker commented Jul 31, 2024

仍然会有超出限速的状况

因为补充令牌的逻辑需要一些运行时间,速度测算与限流器存在错位误差。不超出限速的时候也显示难以达到最高限速。
提高令牌补充时间的精度可以使测算出来的速度更接近于限速,但不能极小间隔,否则线程上下文切换的时间将大于期望的请求令牌所需时间,导致无论限速多少实际速度只会在1MB/s左右。
我实际测试了一点知名下载器软件并尝试搜索网上教程,均会一定时间后超出限速。

改成滑动窗口有没有可能改善。

不会,因为令牌桶设置的上限等同于单位时间内补货的数量,这和窗口类算法相似。

超出限速后的等待的代码改成这一段更合适

同样因为补充令牌需要时间,为了尽可能达到下载的最高限速,还是高频尝试获取令牌为好。

使用 RateLimiter.GetStatistics() 能不能做到代替已有的速度统计功能

不能,RateLimiterStatistics.TotalSuccessfulLeases统计的只是租约的次数

@Scighost
Copy link
Owner

Scighost commented Aug 3, 2024

怎么关了?

@Eric-Joker
Copy link
Contributor Author

Eric-Joker commented Aug 3, 2024

觉得这仨算法都不算完美,然后换了几种方案,都不咋地。
明天再给令牌桶做点优化吧。。

@Eric-Joker Eric-Joker mentioned this pull request Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Limit download speed not working?
2 participants