Skip to content

Commit

Permalink
Merge pull request #4046 from MarkCiliaVincenti/AsyncLock
Browse files Browse the repository at this point in the history
Switched to a more performant locking library
  • Loading branch information
baywet authored Jan 24, 2024
2 parents 0f2be1e + f804e31 commit 56c42d0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/Kiota.Builder/Caching/DocumentCachingProvider.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using AsyncKeyedLock;
using Microsoft.Extensions.Logging;
using NeoSmart.AsyncLock;

namespace Kiota.Builder.Caching;

Expand Down Expand Up @@ -39,8 +38,7 @@ private async Task<Stream> GetDocumentInternalAsync(Uri documentUri, string inte
{
var hashedUrl = BitConverter.ToString((HashAlgorithm.Value ?? throw new InvalidOperationException("unable to get hash algorithm")).ComputeHash(Encoding.UTF8.GetBytes(documentUri.ToString()))).Replace("-", string.Empty, StringComparison.OrdinalIgnoreCase);
var target = Path.Combine(Path.GetTempPath(), Constants.TempDirectoryName, "cache", intermediateFolderName, hashedUrl, fileName);
var currentLock = _locks.GetOrAdd(target, _ => new AsyncLock());
using (await currentLock.LockAsync(token).ConfigureAwait(false))
using (await _locks.LockAsync(target, token).ConfigureAwait(false))
{// if multiple clients are being updated for the same description, we'll have concurrent download of the file without the lock
if (!File.Exists(target) || couldNotDelete)
return await DownloadDocumentFromSourceAsync(documentUri, target, accept, token).ConfigureAwait(false);
Expand All @@ -67,7 +65,11 @@ private async Task<Stream> GetDocumentInternalAsync(Uri documentUri, string inte
return await GetDocumentInternalAsync(documentUri, intermediateFolderName, fileName, couldNotDelete, accept, token).ConfigureAwait(false);
}
}
private static readonly ConcurrentDictionary<string, AsyncLock> _locks = new(StringComparer.OrdinalIgnoreCase);
private static readonly AsyncKeyedLocker<string> _locks = new(o =>
{
o.PoolSize = 20;
o.PoolInitialFill = 1;
});
private async Task<Stream> DownloadDocumentFromSourceAsync(Uri documentUri, string target, string? accept, CancellationToken token)
{
Logger.LogDebug("cache file {CacheFile} not found, downloading from {Url}", target, documentUri);
Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Kiota.Builder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<NoWarn>$(NoWarn);CS8785;NU5048;NU5104;CA1724;CA1055;CA1848;CA1308;CA1822</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AsyncKeyedLock" Version="6.3.4" />
<PackageReference Include="DotNet.Glob" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.7.5" />
Expand All @@ -47,7 +48,6 @@
<PackageReference Include="Microsoft.OpenApi.ApiManifest" Version="0.5.4-preview" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.12" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="NeoSmart.AsyncLock" Version="3.2.1" />
<PackageReference Include="YamlDotNet" Version="15.1.0" />
<ProjectReference Include="..\Kiota.Generated\KiotaGenerated.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
Expand Down

0 comments on commit 56c42d0

Please sign in to comment.