Locking cleaning (#15713)

This commit is contained in:
Mark Cilia Vincenti 2025-12-09 05:01:12 +01:00 committed by GitHub
parent 8fd59d6f33
commit c3a8734adf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 23 deletions

View File

@ -4,7 +4,7 @@
</PropertyGroup> </PropertyGroup>
<!-- Run "dotnet list package (dash,dash)outdated" to see the latest versions of each package.--> <!-- Run "dotnet list package (dash,dash)outdated" to see the latest versions of each package.-->
<ItemGroup Label="Package Dependencies"> <ItemGroup Label="Package Dependencies">
<PackageVersion Include="AsyncKeyedLock" Version="7.1.7" /> <PackageVersion Include="AsyncKeyedLock" Version="7.1.8" />
<PackageVersion Include="AutoFixture.AutoMoq" Version="4.18.1" /> <PackageVersion Include="AutoFixture.AutoMoq" Version="4.18.1" />
<PackageVersion Include="AutoFixture.Xunit2" Version="4.18.1" /> <PackageVersion Include="AutoFixture.Xunit2" Version="4.18.1" />
<PackageVersion Include="AutoFixture" Version="4.18.1" /> <PackageVersion Include="AutoFixture" Version="4.18.1" />

View File

@ -1,5 +1,5 @@
using System; using System;
using System.Threading; using AsyncKeyedLock;
using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
@ -17,7 +17,7 @@ internal sealed class CachingOpenApiProvider : ISwaggerProvider
private const string CacheKey = "openapi.json"; private const string CacheKey = "openapi.json";
private static readonly MemoryCacheEntryOptions _cacheOptions = new() { SlidingExpiration = TimeSpan.FromMinutes(5) }; private static readonly MemoryCacheEntryOptions _cacheOptions = new() { SlidingExpiration = TimeSpan.FromMinutes(5) };
private static readonly SemaphoreSlim _lock = new(1, 1); private static readonly AsyncNonKeyedLocker _lock = new(1);
private static readonly TimeSpan _lockTimeout = TimeSpan.FromSeconds(1); private static readonly TimeSpan _lockTimeout = TimeSpan.FromSeconds(1);
private readonly IMemoryCache _memoryCache; private readonly IMemoryCache _memoryCache;
@ -50,15 +50,13 @@ internal sealed class CachingOpenApiProvider : ISwaggerProvider
return AdjustDocument(openApiDocument, host, basePath); return AdjustDocument(openApiDocument, host, basePath);
} }
var acquired = _lock.Wait(_lockTimeout); using var acquired = _lock.LockOrNull(_lockTimeout);
try
{
if (_memoryCache.TryGetValue(CacheKey, out openApiDocument) && openApiDocument is not null) if (_memoryCache.TryGetValue(CacheKey, out openApiDocument) && openApiDocument is not null)
{ {
return AdjustDocument(openApiDocument, host, basePath); return AdjustDocument(openApiDocument, host, basePath);
} }
if (!acquired) if (acquired is null)
{ {
throw new InvalidOperationException("OpenApi document is generating"); throw new InvalidOperationException("OpenApi document is generating");
} }
@ -67,14 +65,6 @@ internal sealed class CachingOpenApiProvider : ISwaggerProvider
_memoryCache.Set(CacheKey, openApiDocument, _cacheOptions); _memoryCache.Set(CacheKey, openApiDocument, _cacheOptions);
return AdjustDocument(openApiDocument, host, basePath); return AdjustDocument(openApiDocument, host, basePath);
} }
finally
{
if (acquired)
{
_lock.Release();
}
}
}
private OpenApiDocument AdjustDocument(OpenApiDocument document, string? host, string? basePath) private OpenApiDocument AdjustDocument(OpenApiDocument document, string? host, string? basePath)
{ {

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<!-- ProjectGuid is only included as a requirement for SonarQube analysis --> <!-- ProjectGuid is only included as a requirement for SonarQube analysis -->
<PropertyGroup> <PropertyGroup>