From f61913d59b1696bce41a0b095f7df6a24ea1dce4 Mon Sep 17 00:00:00 2001 From: Damon Tivel Date: Wed, 24 May 2017 11:49:39 -0700 Subject: [PATCH] Apply review feedback. --- .../Providers/PluginResourceProvider.cs | 64 +++++++++++++++---- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/src/NuGet.Core/NuGet.Protocol/Providers/PluginResourceProvider.cs b/src/NuGet.Core/NuGet.Protocol/Providers/PluginResourceProvider.cs index 80465aaf4e0..5c43e873be5 100644 --- a/src/NuGet.Core/NuGet.Protocol/Providers/PluginResourceProvider.cs +++ b/src/NuGet.Core/NuGet.Protocol/Providers/PluginResourceProvider.cs @@ -13,6 +13,7 @@ using NuGet.Common; using NuGet.Packaging; using NuGet.Protocol.Plugins; +using NuGet.Shared; namespace NuGet.Protocol.Core.Types { @@ -28,7 +29,7 @@ public class PluginResourceProvider : ResourceProvider, IDisposable private Lazy _discoverer; private bool _isDisposed; private IPluginFactory _pluginFactory; - private ConcurrentDictionary>>>> _pluginOperationClaims; + private ConcurrentDictionary>>> _pluginOperationClaims; private ConcurrentDictionary> _pluginUtilities; private string _rawPluginPaths; private TimeSpan _requestTimeout; @@ -164,8 +165,7 @@ public void Reinitialize(IEnvironmentVariableReader reader, _requestTimeout = GetRequestTimeout(requestTimeoutInSeconds); _discoverer = pluginDiscoverer; _pluginFactory = pluginFactory; - _pluginOperationClaims = new ConcurrentDictionary>>>>( - StringComparer.OrdinalIgnoreCase); + _pluginOperationClaims = new ConcurrentDictionary>>>(); _pluginUtilities = new ConcurrentDictionary>( StringComparer.OrdinalIgnoreCase); } @@ -210,24 +210,20 @@ await utilities.Value.DoOncePerPluginLifetimeAsync( () => InitializePluginAsync(plugin, _requestTimeout, cancellationToken), cancellationToken); - var pluginOperationClaims = _pluginOperationClaims.GetOrAdd( - result.PluginFile.Path, - filePath => new ConcurrentDictionary>>>(StringComparer.OrdinalIgnoreCase)); - - var lazyOperationClaimsForPackageSource = pluginOperationClaims.GetOrAdd( - packageSourceRepository, - source => new Lazy>>(() => GetPluginOperationClaimsAsync( + var lazyOperationClaims = _pluginOperationClaims.GetOrAdd( + new PluginPackageSourceKey(result.PluginFile.Path, packageSourceRepository), + key => new Lazy>>(() => GetPluginOperationClaimsAsync( plugin, packageSourceRepository, serviceIndexJson, cancellationToken))); - await lazyOperationClaimsForPackageSource.Value; + await lazyOperationClaims.Value; pluginCreationResult = new PluginCreationResult( plugin, utilities.Value, - lazyOperationClaimsForPackageSource.Value.Result); + lazyOperationClaims.Value.Result); } else { @@ -327,5 +323,49 @@ private static async Task InitializePluginAsync( plugin.Connection.Options.SetRequestTimeout(requestTimeout); } + + private sealed class PluginPackageSourceKey : IEquatable + { + internal string PluginFilePath { get; } + internal string PackageSourceRepository { get; } + + internal PluginPackageSourceKey(string pluginFilePath, string packageSourceRepository) + { + PluginFilePath = pluginFilePath; + PackageSourceRepository = packageSourceRepository; + } + + public override bool Equals(object obj) + { + return Equals(obj as PluginPackageSourceKey); + } + + public override int GetHashCode() + { + return HashCodeCombiner.GetHashCode(PluginFilePath, PackageSourceRepository); + } + + public bool Equals(PluginPackageSourceKey other) + { + if (ReferenceEquals(this, other)) + { + return true; + } + + if (ReferenceEquals(null, other)) + { + return false; + } + + return string.Equals( + PluginFilePath, + other.PluginFilePath, + StringComparison.OrdinalIgnoreCase) + && string.Equals( + PackageSourceRepository, + other.PackageSourceRepository, + StringComparison.OrdinalIgnoreCase); + } + } } } \ No newline at end of file