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

下载限速 #492

Closed
wants to merge 1 commit into from
Closed

下载限速 #492

wants to merge 1 commit into from

Conversation

xinyihl
Copy link

@xinyihl xinyihl commented Nov 13, 2023

添加下载限速功能
UI没写还有当 DowlondSpeed = 100 时是无限制 单位是Mb/s

@MoRanYue
Copy link

MoRanYue commented Nov 13, 2023

(抱歉,我已经很努力忍住不笑了(doge))

@@ -733,6 +733,7 @@ private async Task<List<DownloadTask>> GetPkgVersionsAsync(string url)
public async Task DownloadAsync(CancellationToken cancellationToken)
{
const int bufferSize = 1 << 16;
int speed = AppConfig.DowlondSpeed * 500;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

数字 500 含义不明

{
double tempData = ((speedPer - speed) < 1) ? 1 : speedPer - speed;
int sleepMS = Convert.ToInt32(tempData * (1000 / speed) + 100);
Thread.Sleep((sleepMS > 1000) ? 1000 : sleepMS);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用 Thread.Sleep 会阻塞当前线程

Thread.Sleep((sleepMS > 1000) ? 1000 : sleepMS);
}
}
}
await fs.WriteAsync(buffer.AsMemory(0, length), token).ConfigureAwait(false);
Interlocked.Add(ref progressBytes, length);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

因为这部分没有移动到 if (AppConfig.DowlondSpeed != 100) 的 else 分支中,实际下载速度大约是计算值的两倍

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你的方法中 DownloadSpeed 参数仅限制了单线程的下载速度,多线程的下载速度总值会远远超过设定值。

@Scighost
Copy link
Owner

首先感谢你愿意帮助 Starward 解决一些需要优化的痛点,并且做出了代码贡献。但是因为以上原因我不能接受你的 PR 请求。

@Scighost Scighost closed this Nov 13, 2023
@xinyihl
Copy link
Author

xinyihl commented Nov 13, 2023

很抱歉之前没看到消息,写的很烂

1. 使用 Thread.Sleep 会阻塞当前线程

  • 我了解这个情况,实现限速的一种方式罢了

2. 没有放到if (AppConfig.DowlondSpeed != 100) 的 else 分支中

  • 这个if判断仅作为优化手段,用于在不想限速时跳过时间判断提高速度
  • 和考虑某些人的网速可能会远远大于设置限制数值,虽然也可以自己设置大一点当对于这种功能而言显然限制到100Mb/s就足够了甚至没必要到100?

3. 多线程

  • 抱歉写的时候忘记考虑了

这个方法是判断每一个下载分段与由设置的下载速度计算出的预期分段时间进行比较,
如果实际下载速度大于预期时间则休眠该线程(预期时间 - 现实时间)时间。

@xinyihl xinyihl deleted the patch-1 branch November 14, 2023 05:11
@xinyihl xinyihl mentioned this pull request Nov 29, 2023
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.

3 participants