-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #160 from deanmarcussen/dm/zerolength
Fix Zero Length Images
- Loading branch information
Showing
5 changed files
with
214 additions
and
79 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/ImageSharp.Web/Middleware/ConcurrentDictionaryExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Threading.Tasks; | ||
|
||
namespace SixLabors.ImageSharp.Web.Middleware | ||
{ | ||
/// <summary> | ||
/// Extensions used to manage asynchronous access to the <see cref="ImageSharpMiddleware"/> | ||
/// https://gist.github.com/davidfowl/3dac8f7b3d141ae87abf770d5781feed | ||
/// </summary> | ||
public static class ConcurrentDictionaryExtensions | ||
{ | ||
/// <summary> | ||
/// Provides an alternative to <see cref="ConcurrentDictionary{TKey, TValue}.GetOrAdd(TKey, Func{TKey, TValue})"/> specifically for asynchronous values. The factory method will only run once. | ||
/// </summary> | ||
/// <typeparam name="TKey">The type of the key.</typeparam> | ||
/// <typeparam name="TValue">The value for the dictionary.</typeparam> | ||
/// <param name="dictionary">The <see cref="ConcurrentDictionary{TKey, TValue}"/>.</param> | ||
/// <param name="key">The key of the element to add.</param> | ||
/// <param name="valueFactory">The function used to generate a value for the key</param> | ||
/// <returns>The value for the key. This will be either the existing value for the key if the | ||
/// key is already in the dictionary, or the new value for the key as returned by valueFactory | ||
/// if the key was not in the dictionary.</returns> | ||
public static async Task<TValue> GetOrAddAsync<TKey, TValue>( | ||
this ConcurrentDictionary<TKey, Task<TValue>> dictionary, | ||
TKey key, | ||
Func<TKey, Task<TValue>> valueFactory) | ||
{ | ||
while (true) | ||
{ | ||
if (dictionary.TryGetValue(key, out var task)) | ||
{ | ||
return await task; | ||
} | ||
|
||
// This is the task that we'll return to all waiters. We'll complete it when the factory is complete | ||
var tcs = new TaskCompletionSource<TValue>(TaskCreationOptions.RunContinuationsAsynchronously); | ||
if (dictionary.TryAdd(key, tcs.Task)) | ||
{ | ||
try | ||
{ | ||
var value = await valueFactory(key); | ||
tcs.TrySetResult(value); | ||
return await tcs.Task; | ||
} | ||
catch (Exception ex) | ||
{ | ||
// Make sure all waiters see the exception | ||
tcs.SetException(ex); | ||
|
||
// We remove the entry if the factory failed so it's not a permanent failure | ||
// and future gets can retry (this could be a pluggable policy) | ||
dictionary.TryRemove(key, out _); | ||
throw; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using SixLabors.ImageSharp.Web.Resolvers; | ||
|
||
namespace SixLabors.ImageSharp.Web.Middleware | ||
{ | ||
/// <summary> | ||
/// Provides an asynchronous worker result. | ||
/// </summary> | ||
internal readonly struct ImageWorkerResult | ||
{ | ||
public ImageWorkerResult(ImageMetadata sourceImageMetadata) | ||
{ | ||
this.IsNewOrUpdated = true; | ||
this.SourceImageMetadata = sourceImageMetadata; | ||
this.CacheImageMetadata = default; | ||
this.Resolver = default; | ||
} | ||
|
||
public ImageWorkerResult(ImageMetadata sourceImageMetadata, ImageCacheMetadata cacheImageMetadata, IImageCacheResolver resolver) | ||
{ | ||
this.IsNewOrUpdated = false; | ||
this.SourceImageMetadata = sourceImageMetadata; | ||
this.CacheImageMetadata = cacheImageMetadata; | ||
this.Resolver = resolver; | ||
} | ||
|
||
public ImageWorkerResult(ImageCacheMetadata cacheImageMetadata, IImageCacheResolver resolver) | ||
{ | ||
this.IsNewOrUpdated = false; | ||
this.SourceImageMetadata = default; | ||
this.CacheImageMetadata = cacheImageMetadata; | ||
this.Resolver = resolver; | ||
} | ||
|
||
public bool IsNewOrUpdated { get; } | ||
|
||
public ImageMetadata SourceImageMetadata { get; } | ||
|
||
public ImageCacheMetadata CacheImageMetadata { get; } | ||
|
||
public IImageCacheResolver Resolver { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters