mirror of
https://github.com/Cleanuparr/Cleanuparr.git
synced 2026-01-24 05:39:14 -05:00
fixed download clients not being invalidated on config change
This commit is contained in:
@@ -12,15 +12,18 @@ public class DynamicHttpClientFactory : IDynamicHttpClientFactory
|
||||
{
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
private readonly IHttpClientConfigStore _configStore;
|
||||
private readonly IHttpClientOptionsInvalidator _optionsInvalidator;
|
||||
private readonly ILogger<DynamicHttpClientFactory> _logger;
|
||||
|
||||
public DynamicHttpClientFactory(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
IHttpClientConfigStore configStore,
|
||||
IHttpClientOptionsInvalidator optionsInvalidator,
|
||||
ILogger<DynamicHttpClientFactory> logger)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_configStore = configStore;
|
||||
_optionsInvalidator = optionsInvalidator;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
@@ -99,6 +102,11 @@ public class DynamicHttpClientFactory : IDynamicHttpClientFactory
|
||||
public void UnregisterConfiguration(string clientName)
|
||||
{
|
||||
_configStore.RemoveConfiguration(clientName);
|
||||
|
||||
// Also invalidate the cached options for this client
|
||||
_optionsInvalidator.InvalidateClient(clientName);
|
||||
|
||||
_logger.LogDebug("Unregistered and invalidated HTTP client configuration: {ClientName}", clientName);
|
||||
}
|
||||
|
||||
public void UpdateAllClientsFromGeneralConfig(GeneralConfig generalConfig)
|
||||
@@ -128,10 +136,15 @@ public class DynamicHttpClientFactory : IDynamicHttpClientFactory
|
||||
return new KeyValuePair<string, HttpClientConfig>(kvp.Key, config);
|
||||
}).ToList();
|
||||
|
||||
// Apply all updates
|
||||
// Apply all updates to our configuration store
|
||||
_configStore.UpdateConfigurations(updatedConfigurations);
|
||||
|
||||
_logger.LogInformation("Updated {Count} HTTP client configurations with new general settings: " +
|
||||
// CRITICAL: Invalidate IHttpClientFactory's cached configurations
|
||||
// This forces the factory to call our Configure() method again with updated settings
|
||||
var clientNames = updatedConfigurations.Select(kvp => kvp.Key).ToList();
|
||||
_optionsInvalidator.InvalidateClients(clientNames);
|
||||
|
||||
_logger.LogInformation("Updated and invalidated {Count} HTTP client configurations with new general settings: " +
|
||||
"Timeout={Timeout}s, MaxRetries={MaxRetries}, CertificateValidation={CertValidation}",
|
||||
updatedConfigurations.Count,
|
||||
generalConfig.HttpTimeout,
|
||||
@@ -143,4 +156,10 @@ public class DynamicHttpClientFactory : IDynamicHttpClientFactory
|
||||
{
|
||||
return _configStore.GetAllConfigurations().Select(kvp => kvp.Key);
|
||||
}
|
||||
|
||||
public void InvalidateAllCachedConfigurations()
|
||||
{
|
||||
_optionsInvalidator.InvalidateAllClients();
|
||||
_logger.LogInformation("Force invalidated all HTTP client option caches");
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,9 @@ public static class DynamicHttpClientServiceCollectionExtensions
|
||||
services.AddSingleton<IConfigureOptions<HttpClientFactoryOptions>, DynamicHttpClientConfiguration>();
|
||||
services.AddSingleton<IDynamicHttpClientFactory, DynamicHttpClientFactory>();
|
||||
|
||||
// Register the cache invalidation service
|
||||
services.AddSingleton<IHttpClientOptionsInvalidator, HttpClientOptionsInvalidator>();
|
||||
|
||||
// Add base HttpClient factory
|
||||
services.AddHttpClient();
|
||||
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
using Microsoft.Extensions.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Infrastructure.Http.DynamicHttpClientSystem;
|
||||
|
||||
/// <summary>
|
||||
/// Implementation of HTTP client options invalidator using cache manipulation
|
||||
/// </summary>
|
||||
public class HttpClientOptionsInvalidator : IHttpClientOptionsInvalidator
|
||||
{
|
||||
private readonly IOptionsMonitorCache<HttpClientFactoryOptions> _optionsCache;
|
||||
private readonly ILogger<HttpClientOptionsInvalidator> _logger;
|
||||
|
||||
public HttpClientOptionsInvalidator(
|
||||
IOptionsMonitorCache<HttpClientFactoryOptions> optionsCache,
|
||||
ILogger<HttpClientOptionsInvalidator> logger)
|
||||
{
|
||||
_optionsCache = optionsCache;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void InvalidateClient(string clientName)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Remove the cached configuration for this specific client
|
||||
_optionsCache.TryRemove(clientName);
|
||||
|
||||
_logger.LogDebug("Invalidated HTTP client options cache for client: {ClientName}", clientName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to invalidate HTTP client options cache for client: {ClientName}", clientName);
|
||||
}
|
||||
}
|
||||
|
||||
public void InvalidateAllClients()
|
||||
{
|
||||
try
|
||||
{
|
||||
// Clear the entire options cache
|
||||
_optionsCache.Clear();
|
||||
|
||||
_logger.LogDebug("Invalidated all HTTP client options cache entries");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to invalidate all HTTP client options cache entries");
|
||||
}
|
||||
}
|
||||
|
||||
public void InvalidateClients(IEnumerable<string> clientNames)
|
||||
{
|
||||
var clientNamesList = clientNames.ToList();
|
||||
|
||||
try
|
||||
{
|
||||
foreach (var clientName in clientNamesList)
|
||||
{
|
||||
_optionsCache.TryRemove(clientName);
|
||||
}
|
||||
|
||||
_logger.LogDebug("Invalidated HTTP client options cache for {Count} clients: {ClientNames}",
|
||||
clientNamesList.Count, string.Join(", ", clientNamesList));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to invalidate HTTP client options cache for multiple clients");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,4 +52,9 @@ public interface IDynamicHttpClientFactory
|
||||
/// Gets all currently registered client names
|
||||
/// </summary>
|
||||
IEnumerable<string> GetRegisteredClientNames();
|
||||
|
||||
/// <summary>
|
||||
/// Forces cache invalidation for all registered clients (for debugging/testing)
|
||||
/// </summary>
|
||||
void InvalidateAllCachedConfigurations();
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
namespace Infrastructure.Http.DynamicHttpClientSystem;
|
||||
|
||||
/// <summary>
|
||||
/// Service for invalidating cached HTTP client configurations
|
||||
/// </summary>
|
||||
public interface IHttpClientOptionsInvalidator
|
||||
{
|
||||
/// <summary>
|
||||
/// Invalidates the cached configuration for a specific client name
|
||||
/// </summary>
|
||||
/// <param name="clientName">The name of the client to invalidate</param>
|
||||
void InvalidateClient(string clientName);
|
||||
|
||||
/// <summary>
|
||||
/// Invalidates all cached HTTP client configurations
|
||||
/// </summary>
|
||||
void InvalidateAllClients();
|
||||
|
||||
/// <summary>
|
||||
/// Invalidates multiple client configurations
|
||||
/// </summary>
|
||||
/// <param name="clientNames">The names of the clients to invalidate</param>
|
||||
void InvalidateClients(IEnumerable<string> clientNames);
|
||||
}
|
||||
Reference in New Issue
Block a user