Skip to content

Commit

Permalink
Use ConcurrentDictionary for storing HttpClients
Browse files Browse the repository at this point in the history
  • Loading branch information
skttl committed Feb 22, 2024
1 parent 53a6c0b commit 7d5fa41
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/ImageSharpCommunity.Providers.Remote/Helpers.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Concurrent;
using System.Text;
using System.Text.RegularExpressions;
using ImageSharpCommunity.Providers.Remote.Configuration;
Expand All @@ -10,7 +11,7 @@ namespace ImageSharpCommunity.Providers.Remote;

public static class Helpers
{
private static Dictionary<string, HttpClient> HttpClients { get; } = new Dictionary<string, HttpClient>();
private static ConcurrentDictionary<string, HttpClient> HttpClients { get; } = new ConcurrentDictionary<string, HttpClient>();

/// <summary>
/// Gets the remote URL for a given path, based on the specified options.
Expand All @@ -35,7 +36,7 @@ public static HttpClient GetRemoteImageProviderHttpClient(this IHttpClientFactor
httpClient.Timeout = TimeSpan.FromMilliseconds(setting.Timeout);
httpClient.MaxResponseContentBufferSize = setting.MaxBytes;

HttpClients.TryAdd(setting.ClientDictionaryKey, httpClient);
HttpClients.AddOrUpdate(setting.ClientDictionaryKey, httpClient, (_, _) => httpClient);

return httpClient;
}
Expand Down

0 comments on commit 7d5fa41

Please sign in to comment.