Skip to content

Commit

Permalink
opt: reduce downloader writes
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Joker committed Aug 8, 2024
1 parent 86d2fc2 commit 05208d3
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/Starward/Services/Download/InstallGameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,25 +1211,18 @@ protected async Task DownloadItemAsync(InstallGameItem item, CancellationToken c
{
if (InstallGameManager.IsEnableSpeedLimit)
{
int totalWritten = 0;
while (totalWritten < length)
int totalTokens = 0;
while (totalTokens < length)
{
int remaining = length - totalWritten;
int remaining = length - totalTokens;
if (TokenBucketRateLimiterExtension.TryAcquire(InstallGameManager.RateLimiter, remaining, out int tokensAcquired, out _))
await Task.Delay(1, cancellationToken).ConfigureAwait(false);
else
{
await fs.WriteAsync(buffer.AsMemory(totalWritten, tokensAcquired), cancellationToken).ConfigureAwait(false);
totalWritten += tokensAcquired;
Interlocked.Add(ref _finishBytes, tokensAcquired);
}
totalTokens += tokensAcquired;
}
}
else
{
await fs.WriteAsync(buffer.AsMemory(0, length), cancellationToken).ConfigureAwait(false);
Interlocked.Add(ref _finishBytes, length);
}
await fs.WriteAsync(buffer.AsMemory(0, length), cancellationToken).ConfigureAwait(false);
Interlocked.Add(ref _finishBytes, length);
}
}
}
Expand Down

0 comments on commit 05208d3

Please sign in to comment.