Skip to content

More http download fixes #3193

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

Merged
merged 7 commits into from
May 19, 2025
Merged
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
4 changes: 2 additions & 2 deletions src/NexusMods.Collections/CollectionDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using NexusMods.Abstractions.NexusWebApi;
using NexusMods.Abstractions.NexusWebApi.Types;
using NexusMods.Abstractions.NexusWebApi.Types.V2;
using NexusMods.Abstractions.Settings;
using NexusMods.Abstractions.Telemetry;
using NexusMods.CrossPlatform.Process;
using NexusMods.Extensions.BCL;
Expand Down Expand Up @@ -256,7 +257,6 @@ public async ValueTask DownloadItems(
CollectionRevisionMetadata.ReadOnly revisionMetadata,
ItemType itemType,
IDb db,
int maxDegreeOfParallelism = -1,
CancellationToken cancellationToken = default)
{
var job = new DownloadCollectionJob
Expand All @@ -266,7 +266,7 @@ public async ValueTask DownloadItems(
RevisionMetadata = revisionMetadata,
Db = db,
ItemType = itemType,
MaxDegreeOfParallelism = maxDegreeOfParallelism,
MaxDegreeOfParallelism = _serviceProvider.GetRequiredService<ISettingsManager>().Get<DownloadSettings>().MaxParallelDownloads,
};

await _jobMonitor.Begin<DownloadCollectionJob, R3.Unit>(job);
Expand Down
2 changes: 1 addition & 1 deletion src/NexusMods.Collections/DownloadCollectionJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class DownloadCollectionJob : IJobDefinitionWithStart<DownloadCollectionJ
public required CollectionDownloader.ItemType ItemType { get; init; }
public required CollectionDownloader Downloader { get; init; }
public required IDb Db { get; init; }
public int MaxDegreeOfParallelism { get; init; } = -1;
public required int MaxDegreeOfParallelism { get; init; }

public async ValueTask<R3.Unit> StartAsync(IJobContext<DownloadCollectionJob> context)
{
Expand Down
24 changes: 24 additions & 0 deletions src/NexusMods.Collections/DownloadSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using NexusMods.Abstractions.Settings;

namespace NexusMods.Collections;

public class DownloadSettings : ISettings
{
public int MaxParallelDownloads { get; set; } = Environment.ProcessorCount;

public static ISettingsBuilder Configure(ISettingsBuilder settingsBuilder)
{
return settingsBuilder.AddToUI<DownloadSettings>(builder => builder
.AddPropertyToUI(x => x.MaxParallelDownloads, propertyBuilder => propertyBuilder
.AddToSection(Sections.General)
.WithDisplayName("Max Parallel Downloads")
.WithDescription("Set the maximum number of downloads that can happen in parallel when downloading collections")
.UseSingleValueMultipleChoiceContainer(
valueComparer: EqualityComparer<int>.Default,
allowedValues: Enumerable.Range(start: 1, Environment.ProcessorCount).ToArray(),
valueToDisplayString: static i => i.ToString()
)
)
);
}
}
4 changes: 3 additions & 1 deletion src/NexusMods.Collections/Services.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using NexusMods.Abstractions.Collections;
using NexusMods.Abstractions.Loadouts;
using NexusMods.Abstractions.NexusModsLibrary;
using NexusMods.Abstractions.Settings;

namespace NexusMods.Collections;

Expand All @@ -16,6 +17,7 @@ public static IServiceCollection AddNexusModsCollections(this IServiceCollection
.AddNexusCollectionItemLoadoutGroupModel()
.AddNexusCollectionReplicatedLoadoutGroupModel()
.AddCollectionVerbs()
.AddSingleton<CollectionDownloader>();
.AddSingleton<CollectionDownloader>()
.AddSettings<DownloadSettings>();
}
}
87 changes: 50 additions & 37 deletions src/NexusMods.Networking.HttpDownloader/HttpDownloadJob.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
using System.Diagnostics.CodeAnalysis;
using System.Collections.Immutable;
using System.Net;
using System.Net.Http.Headers;
using System.Net.Sockets;
using DynamicData.Kernel;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Http.Resilience;
using Microsoft.Extensions.Logging;
using NexusMods.Abstractions.HttpDownloads;
using NexusMods.Abstractions.Jobs;
using NexusMods.Abstractions.Library.Models;
using NexusMods.App.BuildInfo;
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.Paths;
using Polly;
using Polly.Retry;

namespace NexusMods.Networking.HttpDownloader;

Expand All @@ -22,9 +22,12 @@ namespace NexusMods.Networking.HttpDownloader;
[PublicAPI]
public record HttpDownloadJob : IJobDefinitionWithStart<HttpDownloadJob, AbsolutePath>, IHttpDownloadJob
{
#pragma warning disable EXTEXP0001
private static readonly HttpClient Client = BuildClient();
#pragma warning restore EXTEXP0001
private static readonly ResiliencePipeline<AbsolutePath> ResiliencePipeline = BuildResiliencePipeline();

/// <summary>
/// Client.
/// </summary>
public required HttpClient Client { get; init; }

/// <summary>
/// Logger.
Expand Down Expand Up @@ -76,15 +79,29 @@ public static IJobTask<HttpDownloadJob, AbsolutePath> Create(
DownloadPageUri = downloadPage,
Destination = destination,
Logger = provider.GetRequiredService<ILogger<HttpDownloadJob>>(),
Client = provider.GetRequiredService<HttpClient>(),
};

return monitor.Begin<HttpDownloadJob, AbsolutePath>(job);
}

/// <summary>
/// Execute the job
/// </summary>
/// <inheritdoc/>
public async ValueTask<AbsolutePath> StartAsync(IJobContext<HttpDownloadJob> context)
{
var result = await ResiliencePipeline.ExecuteAsync(
callback: static (tuple, _) =>
{
var (self, context) = tuple;
return self.StartAsyncImpl(context);
},
state: (this, context),
cancellationToken: context.CancellationToken
);

return result;
}

private async ValueTask<AbsolutePath> StartAsyncImpl(IJobContext<HttpDownloadJob> context)
{
await context.YieldAsync();
await FetchMetadata(context);
Expand Down Expand Up @@ -168,6 +185,11 @@ public async ValueTask<AbsolutePath> StartAsync(IJobContext<HttpDownloadJob> con
{
await response.Content.CopyToAsync(outputStream, context.CancellationToken);
}
catch (Exception e)
{
Logger.LogWarning(e, "Exception while downloading from `{PageUri}`, downloaded `{DownloadedBytes}` from `{TotalBytes}` bytes", DownloadPageUri, outputStream.Position, outputStream.Length);
throw;
}
finally
{
TotalBytesDownloaded = Size.FromLong(outputStream.Position);
Expand Down Expand Up @@ -244,36 +266,27 @@ private async ValueTask FetchMetadata(IJobContext context)
var contentLength = response.Content.Headers.ContentLength;
ContentLength = contentLength is not null ? Size.FromLong(contentLength.Value) : Optional<Size>.None;
}

[Experimental("EXTEXP0001")]
private static HttpClient BuildClient()
{
// TODO: get values from settings, probably make this a singleton

var pipeline = new ResiliencePipelineBuilder<HttpResponseMessage>()
.AddRetry(new HttpRetryStrategyOptions())
.Build();

HttpMessageHandler handler = new ResilienceHandler(pipeline)
{
InnerHandler = new SocketsHttpHandler
private static ResiliencePipeline<AbsolutePath> BuildResiliencePipeline()
{
ImmutableArray<Type> networkExceptions =
[
typeof(HttpIOException),
typeof(HttpRequestException),
typeof(SocketException),
];

var pipeline = new ResiliencePipelineBuilder<AbsolutePath>()
.AddRetry(new RetryStrategyOptions<AbsolutePath>
{
ConnectTimeout = TimeSpan.FromSeconds(30),
KeepAlivePingPolicy = HttpKeepAlivePingPolicy.WithActiveRequests,
KeepAlivePingDelay = TimeSpan.FromSeconds(5),
KeepAlivePingTimeout = TimeSpan.FromSeconds(20),
},
};

var client = new HttpClient(handler)
{
Timeout = TimeSpan.FromSeconds(20),
DefaultRequestVersion = HttpVersion.Version11,
DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrHigher,
};

client.DefaultRequestHeaders.UserAgent.ParseAdd(ApplicationConstants.UserAgent);
ShouldHandle = args => ValueTask.FromResult(args.Outcome.Exception is not null && networkExceptions.Contains(args.Outcome.Exception.GetType())),
BackoffType = DelayBackoffType.Exponential,
UseJitter = true,
MaxRetryAttempts = 3,
Delay = TimeSpan.FromSeconds(3),
})
.Build();

return client;
return pipeline;
}
}
34 changes: 32 additions & 2 deletions src/NexusMods.Networking.HttpDownloader/Services.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Net;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Http.Resilience;
using NexusMods.App.BuildInfo;
using Polly;

namespace NexusMods.Networking.HttpDownloader;

Expand All @@ -12,9 +15,36 @@ public static IServiceCollection AddHttpDownloader(this IServiceCollection servi
{
return services.AddSingleton<HttpClient>(_ =>
{
var client = new HttpClient();
client.DefaultRequestHeaders.UserAgent.ParseAdd(ApplicationConstants.UserAgent);
var client = BuildClient();
return client;
});
}

private static HttpClient BuildClient()
{
var pipeline = new ResiliencePipelineBuilder<HttpResponseMessage>()
.AddRetry(new HttpRetryStrategyOptions
{
BackoffType = DelayBackoffType.Exponential,
MaxRetryAttempts = 3,
Delay = TimeSpan.FromSeconds(3),
UseJitter = true,
})
.Build();

HttpMessageHandler handler = new ResilienceHandler(pipeline)
{
InnerHandler = new SocketsHttpHandler(),
};

var client = new HttpClient(handler)
{
DefaultRequestVersion = HttpVersion.Version11,
DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrHigher,
};

client.DefaultRequestHeaders.UserAgent.ParseAdd(ApplicationConstants.UserAgent);

return client;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static IJobTask<ExternalDownloadJob, AbsolutePath> Create(IServiceProvide
DownloadPageUri = uri,
Destination = tempFileManager.CreateFile(),
Uri = uri,
Client = provider.GetRequiredService<HttpClient>(),
};

return monitor.Begin<ExternalDownloadJob, AbsolutePath>(job);
Expand Down
Loading