From e1fb9de9770e5f56d463d1d5f41b9d54e4375e04 Mon Sep 17 00:00:00 2001 From: Dean Marcussen Date: Thu, 13 Jan 2022 10:10:48 +0000 Subject: [PATCH] Tidy up unused ConcurrentDictionary Extensions --- .../ConcurrentDictionaryExtensions.cs | 63 ------------------- 1 file changed, 63 deletions(-) delete mode 100644 src/ImageSharp.Web/Middleware/ConcurrentDictionaryExtensions.cs diff --git a/src/ImageSharp.Web/Middleware/ConcurrentDictionaryExtensions.cs b/src/ImageSharp.Web/Middleware/ConcurrentDictionaryExtensions.cs deleted file mode 100644 index 09b77544..00000000 --- a/src/ImageSharp.Web/Middleware/ConcurrentDictionaryExtensions.cs +++ /dev/null @@ -1,63 +0,0 @@ -// 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 -{ - /// - /// Extensions used to manage asynchronous access to the - /// https://gist.github.com/davidfowl/3dac8f7b3d141ae87abf770d5781feed - /// - public static class ConcurrentDictionaryExtensions - { - /// - /// Provides an alternative to specifically for asynchronous values. The factory method will only run once. - /// - /// The type of the key. - /// The value for the dictionary. - /// The . - /// The key of the element to add. - /// The function used to generate a value for the key - /// 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. - public static async Task GetOrAddAsync( - this ConcurrentDictionary> dictionary, - TKey key, - Func> 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(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; - } - } - } - } - } -}