Skip to content

Commit

Permalink
Make DID resolution cache thread-safe (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
alnkesq authored Jan 10, 2025
1 parent 14539fd commit be04961
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
6 changes: 2 additions & 4 deletions src/FishyFlip/ATProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@ public async Task<string> GenerateOAuth2AuthenticationUrlAsync(string clientId,
/// <returns>String of Host URI if it could be resolved, null if it could not.</returns>
public async Task<Result<string?>> ResolveATHandleHostAsync(ATHandle handle, CancellationToken? token = default)
{
string? host = this.options.DidCache.FirstOrDefault(n => n.Key == handle.ToString()).Value;
if (!string.IsNullOrEmpty(host))
if (this.options.DidCache.TryGetValue(handle.ToString(), out var host) && !string.IsNullOrEmpty(host))
{
this.options.Logger?.LogDebug($"Resolved handle from cache: {handle} to {host}");
return host;
Expand Down Expand Up @@ -344,8 +343,7 @@ await result.Content.ReadAsStringAsync(),
/// <returns>String of Host URI if it could be resolved, null if it could not.</returns>
public async Task<Result<string?>> ResolveATDidHostAsync(ATDid did, CancellationToken? token = default)
{
string? host = this.options.DidCache.FirstOrDefault(n => n.Key == did.ToString()).Value;
if (!string.IsNullOrEmpty(host))
if (this.options.DidCache.TryGetValue(did.ToString(), out var host) && !string.IsNullOrEmpty(host))
{
this.options.Logger?.LogDebug($"Resolved DID from cache: {did} to {host}");
return host;
Expand Down
3 changes: 2 additions & 1 deletion src/FishyFlip/ATProtocolOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// </copyright>

using FishyFlip.Tools.Json;
using System.Collections.Concurrent;
using System.Net;

namespace FishyFlip;
Expand Down Expand Up @@ -78,7 +79,7 @@ public ATProtocolOptions()
/// <summary>
/// Gets the Did Cache.
/// </summary>
internal Dictionary<string, string> DidCache { get; } = new Dictionary<string, string>();
internal ConcurrentDictionary<string, string> DidCache { get; } = new();

/// <summary>
/// Gets the source generation context.
Expand Down

0 comments on commit be04961

Please sign in to comment.