Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
/ NuGet.Jobs Public archive

Commit

Permalink
Larger buffer for file and network operations. (#704)
Browse files Browse the repository at this point in the history
* larger buffer for stream copy

* Argument checks.

* configuration for package downloader.

* Using the default value for buffer size.

* Removed explicit package downloader configuration

* Comment on buffer size for file streams.
  • Loading branch information
agr authored Jan 23, 2019
1 parent 4de0859 commit 60fcca6
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/Validation.Common.Job/FileStreamUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ namespace NuGet.Jobs.Validation
{
public static class FileStreamUtility
{
public const int BufferSize = 8192;
/// <summary>
/// The buffer size to use for file operations.
/// </summary>
/// <remarks>
/// The value is chosen to align with the default Stream buffer size:
/// https://github.com/dotnet/corefx/blob/master/src/Common/src/CoreLib/System/IO/Stream.cs#L32-L35
/// </remarks>
private const int BufferSize = 80 * 1024;

public static FileStream GetTemporaryFile()
{
Expand Down
24 changes: 23 additions & 1 deletion src/Validation.Common.Job/PackageDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,49 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace NuGet.Jobs.Validation
{
public class PackageDownloader : IFileDownloader
{
private readonly HttpClient _httpClient;
private readonly ICommonTelemetryService _telemetryService;
private readonly PackageDownloaderConfiguration _configuration;
private readonly ILogger<PackageDownloader> _logger;

public PackageDownloader(
HttpClient httpClient,
ICommonTelemetryService telemetryService,
IOptionsSnapshot<PackageDownloaderConfiguration> downloaderConfigurationAccessor,
ILogger<PackageDownloader> logger)
{
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
_telemetryService = telemetryService ?? throw new ArgumentNullException(nameof(telemetryService));
if (downloaderConfigurationAccessor == null)
{
throw new ArgumentNullException(nameof(downloaderConfigurationAccessor));
}
if (downloaderConfigurationAccessor.Value.BufferSize <= 0)
{
throw new ArgumentException($"{nameof(downloaderConfigurationAccessor.Value.BufferSize)} cannot be less than 1", nameof(downloaderConfigurationAccessor));
}
_configuration = downloaderConfigurationAccessor.Value;
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public async Task<Stream> DownloadAsync(Uri packageUri, CancellationToken cancellationToken)
{
if (packageUri == null)
{
throw new ArgumentNullException(nameof(packageUri));
}

if (cancellationToken == null)
{
throw new ArgumentNullException(nameof(cancellationToken));
}

_logger.LogInformation("Attempting to download package from {PackageUri}...", packageUri);

Stream packageStream = null;
Expand All @@ -56,7 +78,7 @@ public async Task<Stream> DownloadAsync(Uri packageUri, CancellationToken cancel
{
packageStream = FileStreamUtility.GetTemporaryFile();

await networkStream.CopyToAsync(packageStream, FileStreamUtility.BufferSize, cancellationToken);
await networkStream.CopyToAsync(packageStream, _configuration.BufferSize, cancellationToken);
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/Validation.Common.Job/PackageDownloaderConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace NuGet.Jobs.Validation
{
public class PackageDownloaderConfiguration
{
/// <summary>
/// The size of the buffer used to copy the network stream.
/// </summary>
/// <remarks>Implementation uses the <see cref="System.IO.Stream.CopyToAsync(System.IO.Stream, int, System.Threading.CancellationToken)"/>
/// passing that value as the second argument.
///
/// Setting the package downloader default buffer size to the buffer size used for copying streams
/// see https://github.com/dotnet/corefx/blob/master/src/Common/src/CoreLib/System/IO/Stream.cs#L32-L35
/// </remarks>
public int BufferSize { get; set; } = 80 * 1024;
}
}
1 change: 1 addition & 0 deletions src/Validation.Common.Job/Validation.Common.Job.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<Compile Include="LoggerDiagnosticsService.cs" />
<Compile Include="LoggerDiagnosticsSource.cs" />
<Compile Include="PackageDownloader.cs" />
<Compile Include="PackageDownloaderConfiguration.cs" />
<Compile Include="PathUtility.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\AssemblyInfo.*.cs" />
Expand Down

0 comments on commit 60fcca6

Please sign in to comment.