diff options
| author | Mark Cilia Vincenti <markciliavincenti@gmail.com> | 2025-12-09 05:01:12 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-12-08 21:01:12 -0700 |
| commit | c3a8734adf00c85ff7676d2a7caad1f5aa8cd01a (patch) | |
| tree | e5ef81a8400e8fef0ddf428606f1022605819436 /Jellyfin.Server/Filters/CachingOpenApiProvider.cs | |
| parent | 8fd59d6f336a95782a13b0440a6cf5108e58a1f8 (diff) | |
Locking cleaning (#15713)
Diffstat (limited to 'Jellyfin.Server/Filters/CachingOpenApiProvider.cs')
| -rw-r--r-- | Jellyfin.Server/Filters/CachingOpenApiProvider.cs | 32 |
1 files changed, 11 insertions, 21 deletions
diff --git a/Jellyfin.Server/Filters/CachingOpenApiProvider.cs b/Jellyfin.Server/Filters/CachingOpenApiProvider.cs index 4169f2fb3..b560ec50e 100644 --- a/Jellyfin.Server/Filters/CachingOpenApiProvider.cs +++ b/Jellyfin.Server/Filters/CachingOpenApiProvider.cs @@ -1,5 +1,5 @@ using System; -using System.Threading; +using AsyncKeyedLock; using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Options; @@ -17,7 +17,7 @@ internal sealed class CachingOpenApiProvider : ISwaggerProvider private const string CacheKey = "openapi.json"; 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 readonly IMemoryCache _memoryCache; @@ -50,30 +50,20 @@ internal sealed class CachingOpenApiProvider : ISwaggerProvider return AdjustDocument(openApiDocument, host, basePath); } - var acquired = _lock.Wait(_lockTimeout); - try + using var acquired = _lock.LockOrNull(_lockTimeout); + 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); - } - - if (!acquired) - { - throw new InvalidOperationException("OpenApi document is generating"); - } - - openApiDocument = _swaggerGenerator.GetSwagger(documentName); - _memoryCache.Set(CacheKey, openApiDocument, _cacheOptions); return AdjustDocument(openApiDocument, host, basePath); } - finally + + if (acquired is null) { - if (acquired) - { - _lock.Release(); - } + throw new InvalidOperationException("OpenApi document is generating"); } + + openApiDocument = _swaggerGenerator.GetSwagger(documentName); + _memoryCache.Set(CacheKey, openApiDocument, _cacheOptions); + return AdjustDocument(openApiDocument, host, basePath); } private OpenApiDocument AdjustDocument(OpenApiDocument document, string? host, string? basePath) |
