Skip to content

Commit 3f0268e

Browse files
Move write lock outside of tcf.
1 parent ec731a1 commit 3f0268e

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

src/ImageSharp.Web/Middleware/ImageSharpMiddleware.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,12 @@ private async Task ProcessRequestAsync(
241241

242242
// Not cached? Let's get it from the image resolver.
243243
RecyclableMemoryStream outStream = null;
244-
try
244+
245+
// Enter a write lock which locks writing and any reads for the same request.
246+
// This reduces the overheads of unnecessary processing plus avoids file locks.
247+
using (await AsyncLock.WriterLockAsync(key))
245248
{
246-
// Enter a write lock which locks writing and any reads for the same request.
247-
// This reduces the overheads of unnecessary processing plus avoids file locks.
248-
using (await AsyncLock.WriterLockAsync(key))
249+
try
249250
{
250251
ImageCacheMetadata cachedImageMetadata = default;
251252
outStream = new RecyclableMemoryStream(this.options.MemoryStreamManager);
@@ -305,17 +306,17 @@ private async Task ProcessRequestAsync(
305306
await this.cache.SetAsync(key, outStream, cachedImageMetadata);
306307
await this.SendResponseAsync(imageContext, key, cachedImageMetadata, outStream, null);
307308
}
308-
}
309-
catch (Exception ex)
310-
{
311-
// Log the error internally then rethrow.
312-
// We don't call next here, the pipeline will automatically handle it
313-
this.logger.LogImageProcessingFailed(imageContext.GetDisplayUrl(), ex);
314-
throw;
315-
}
316-
finally
317-
{
318-
await this.StreamDisposeAsync(outStream);
309+
catch (Exception ex)
310+
{
311+
// Log the error internally then rethrow.
312+
// We don't call next here, the pipeline will automatically handle it
313+
this.logger.LogImageProcessingFailed(imageContext.GetDisplayUrl(), ex);
314+
throw;
315+
}
316+
finally
317+
{
318+
await this.StreamDisposeAsync(outStream);
319+
}
319320
}
320321
}
321322

@@ -389,23 +390,24 @@ private async Task SendResponseAsync(
389390
if (imageContext.IsHeadRequest())
390391
{
391392
await imageContext.SendStatusAsync(ResponseConstants.Status200Ok, metadata);
393+
return;
392394
}
393395

394396
this.logger.LogImageServed(imageContext.GetDisplayUrl(), key);
395397

396398
// When stream is null we're sending from the cache.
397399
await imageContext.SendAsync(stream ?? await cacheResolver.OpenReadAsync(), metadata);
398-
399-
break;
400+
return;
400401

401402
case ImageContext.PreconditionState.NotModified:
402403
this.logger.LogImageNotModified(imageContext.GetDisplayUrl());
403404
await imageContext.SendStatusAsync(ResponseConstants.Status304NotModified, metadata);
404-
break;
405+
return;
406+
405407
case ImageContext.PreconditionState.PreconditionFailed:
406408
this.logger.LogImagePreconditionFailed(imageContext.GetDisplayUrl());
407409
await imageContext.SendStatusAsync(ResponseConstants.Status412PreconditionFailed, metadata);
408-
break;
410+
return;
409411
default:
410412
var exception = new NotImplementedException(imageContext.GetPreconditionState().ToString());
411413
Debug.Fail(exception.ToString());

0 commit comments

Comments
 (0)