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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Starward/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,11 @@ public static ILogger<T> GetLogger<T>()

#region Static Setting


public static int DowlondSpeed
{
get => GetValue<int>();
set => SetValue(value);
}

public static int ApiCDNIndex
{
Expand Down
50 changes: 50 additions & 0 deletions src/Starward/Services/DownloadGameService.cs
Copy link
Owner

Choose a reason for hiding this comment

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -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 含义不明

try
{
State = DownloadGameState.Downloading;
Expand Down Expand Up @@ -778,8 +779,32 @@ public async Task DownloadAsync(CancellationToken cancellationToken)

var buffer = new byte[bufferSize];
int length;
long total_read = 0;
DateTime begin = DateTime.Now;
while ((length = await hs.ReadAsync(buffer, token).ConfigureAwait(false)) != 0)
{
if (AppConfig.DowlondSpeed != 100)
{
total_read += bufferSize;
double totalSeconds = DateTime.Now.Subtract(begin).TotalSeconds;
double byteper = total_read / totalSeconds;
if (double.IsInfinity(byteper))
{
await fs.WriteAsync(buffer.AsMemory(0, length), token).ConfigureAwait(false);
Interlocked.Add(ref progressBytes, length);
continue;
}
else
{
double speedPer = byteper / 1024;
if (speedPer >= speed)
{
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 会阻塞当前线程

}
}
}
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 分支中,实际下载速度大约是计算值的两倍

}
Expand Down Expand Up @@ -837,6 +862,7 @@ public async Task DownloadAsync(CancellationToken cancellationToken)
public async Task DownloadSeparateFilesAsync(CancellationToken cancellationToken)
{
const int bufferSize = 1 << 16;
int speed = AppConfig.DowlondSpeed * 500;
try
{
State = DownloadGameState.Downloading;
Expand Down Expand Up @@ -883,8 +909,32 @@ public async Task DownloadSeparateFilesAsync(CancellationToken cancellationToken

var buffer = new byte[bufferSize];
int length;
long total_read = 0;
DateTime begin = DateTime.Now;
while ((length = await hs.ReadAsync(buffer, token).ConfigureAwait(false)) != 0)
{
if (AppConfig.DowlondSpeed != 100)
{
total_read += bufferSize;
double totalSeconds = DateTime.Now.Subtract(begin).TotalSeconds;
double byteper = total_read / totalSeconds;
if (double.IsInfinity(byteper))
{
await fs.WriteAsync(buffer.AsMemory(0, length), token).ConfigureAwait(false);
Interlocked.Add(ref progressBytes, length);
continue;
}
else
{
double speedPer = byteper / 1024;
if (speedPer >= speed)
{
double tempData = ((speedPer - speed) < 1) ? 1 : speedPer - speed;
int sleepMS = Convert.ToInt32(tempData * (1000 / speed) + 100);
Thread.Sleep((sleepMS > 1000) ? 1000 : sleepMS);
}
}
}
await fs.WriteAsync(buffer.AsMemory(0, length), token).ConfigureAwait(false);
Interlocked.Add(ref progressBytes, length);
}
Expand Down
Loading