-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathServiceIndexResourceV3Provider.cs
More file actions
232 lines (203 loc) · 10 KB
/
ServiceIndexResourceV3Provider.cs
File metadata and controls
232 lines (203 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable disable
using System;
using System.Collections.Concurrent;
using System.Globalization;
using System.IO;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Protocol.Core.Types;
using NuGet.Protocol.Model;
using NuGet.Protocol.Utility;
using NuGet.Versioning;
namespace NuGet.Protocol
{
/// <summary>
/// Retrieves and caches service index.json files
/// ServiceIndexResourceV3 stores the json, all work is done in the provider
/// </summary>
public class ServiceIndexResourceV3Provider : ResourceProvider
{
private static readonly TimeSpan DefaultCacheDuration = TimeSpan.FromMinutes(40);
private readonly ConcurrentDictionary<string, ServiceIndexCacheInfo> _cache;
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
private readonly EnhancedHttpRetryHelper _enhancedHttpRetryHelper;
/// <summary>
/// Maximum amount of time to store index.json
/// </summary>
public TimeSpan MaxCacheDuration { get; protected set; }
public ServiceIndexResourceV3Provider() : this(EnvironmentVariableWrapper.Instance) { }
internal ServiceIndexResourceV3Provider(IEnvironmentVariableReader environmentVariableReader)
: base(typeof(ServiceIndexResourceV3),
nameof(ServiceIndexResourceV3Provider),
NuGetResourceProviderPositions.Last)
{
_cache = new ConcurrentDictionary<string, ServiceIndexCacheInfo>(StringComparer.OrdinalIgnoreCase);
MaxCacheDuration = DefaultCacheDuration;
_enhancedHttpRetryHelper = new EnhancedHttpRetryHelper(environmentVariableReader);
}
public override async Task<Tuple<bool, INuGetResource>> TryCreate(SourceRepository source, CancellationToken token)
{
ServiceIndexResourceV3 index = null;
ServiceIndexCacheInfo cacheInfo = null;
var url = source.PackageSource.Source;
// the file type can easily rule out if we need to request the url
if (source.PackageSource.ProtocolVersion == 3 ||
(source.PackageSource.IsHttp &&
url.EndsWith(".json", StringComparison.OrdinalIgnoreCase)))
{
var utcNow = DateTime.UtcNow;
var entryValidCutoff = utcNow.Subtract(MaxCacheDuration);
// check the cache before downloading the file
if (!_cache.TryGetValue(url, out cacheInfo) ||
entryValidCutoff > cacheInfo.CachedTime)
{
await _semaphore.WaitAsync(token);
try
{
// check the cache again, another thread may have finished this one waited for the lock
if (!_cache.TryGetValue(url, out cacheInfo) ||
entryValidCutoff > cacheInfo.CachedTime)
{
index = await GetServiceIndexResourceV3(source, utcNow, NullLogger.Instance, token);
// cache the value even if it is null to avoid checking it again later
var cacheEntry = new ServiceIndexCacheInfo
{
CachedTime = utcNow,
Index = index
};
// If the cache entry has expired it will already exist
_cache.AddOrUpdate(url, cacheEntry, (key, value) => cacheEntry);
}
}
finally
{
_semaphore.Release();
}
}
}
if (index == null && cacheInfo != null)
{
index = cacheInfo.Index;
}
return new Tuple<bool, INuGetResource>(index != null, index);
}
/// <summary>
/// Read the source's end point to get the index json.
/// Retries are logged to any provided <paramref name="log"/> as LogMinimal.
/// </summary>
/// <param name="source"></param>
/// <param name="utcNow"></param>
/// <param name="log"></param>
/// <param name="token"></param>
/// <exception cref="OperationCanceledException">Logged to any provided <paramref name="log"/> as LogMinimal prior to throwing.</exception>
/// <exception cref="FatalProtocolException">Encapsulates all other exceptions.</exception>
/// <returns></returns>
private async Task<ServiceIndexResourceV3> GetServiceIndexResourceV3(
SourceRepository source,
DateTime utcNow,
ILogger log,
CancellationToken token)
{
var url = source.PackageSource.Source;
var httpSourceResource = await source.GetResourceAsync<HttpSourceResource>(token);
var client = httpSourceResource.HttpSource;
int maxRetries = _enhancedHttpRetryHelper.RetryCountOrDefault;
for (var retry = 1; retry <= maxRetries; retry++)
{
using (var sourceCacheContext = new SourceCacheContext())
{
var cacheContext = HttpSourceCacheContext.Create(sourceCacheContext, isFirstAttempt: retry == 1);
try
{
return await client.GetAsync(
new HttpSourceCachedRequest(
url,
"service_index",
cacheContext)
{
EnsureValidContents = stream => HttpStreamValidation.ValidateJObject(url, stream),
MaxTries = 1,
IsRetry = retry > 1,
IsLastAttempt = retry == maxRetries
},
async httpSourceResult =>
{
var result = await ConsumeServiceIndexStreamAsync(httpSourceResult.Stream, utcNow, source.PackageSource, token);
return result;
},
log,
token);
}
catch (OperationCanceledException ex)
{
var message = ExceptionUtilities.DisplayMessage(ex);
log.LogMinimal(message);
throw;
}
catch (Exception ex) when (retry < maxRetries)
{
var message = string.Format(CultureInfo.CurrentCulture, Strings.Log_RetryingServiceIndex, url)
+ Environment.NewLine
+ ExceptionUtilities.DisplayMessage(ex);
log.LogMinimal(message);
if (ex.InnerException != null &&
ex.InnerException is IOException &&
ex.InnerException.InnerException != null &&
ex.InnerException.InnerException is System.Net.Sockets.SocketException)
{
// An IO Exception with inner SocketException indicates server hangup ("Connection reset by peer").
// Azure DevOps feeds sporadically do this due to mandatory connection cycling.
// Stalling an extra <ExperimentalRetryDelayMilliseconds> gives Azure more of a chance to recover.
log.LogVerbose("Enhanced retry: Encountered SocketException, delaying between tries to allow recovery");
await Task.Delay(TimeSpan.FromMilliseconds(_enhancedHttpRetryHelper.DelayInMillisecondsOrDefault), token);
}
}
catch (Exception ex) when (retry == maxRetries)
{
var message = string.Format(CultureInfo.CurrentCulture, Strings.Log_FailedToReadServiceIndex, url);
throw new FatalProtocolException(message, ex);
}
}
}
return null;
}
private static async Task<ServiceIndexResourceV3> ConsumeServiceIndexStreamAsync(Stream stream, DateTime utcNow, PackageSource source, CancellationToken token)
{
ServiceIndexModel index;
try
{
index = await JsonSerializer.DeserializeAsync(stream, JsonContext.Default.ServiceIndexModel, token);
}
catch (JsonException ex)
{
throw new InvalidDataException(string.Format(
CultureInfo.CurrentCulture,
Strings.Protocol_InvalidJsonObject,
source.Source), ex);
}
if (index?.Version is not string versionString)
{
throw new InvalidDataException(Strings.Protocol_MissingVersion);
}
// Use SemVer instead of NuGetVersion; the service index should always be in strict SemVer format.
if (!SemanticVersion.TryParse(versionString, out SemanticVersion version) || version.Major != 3)
{
throw new InvalidDataException(string.Format(
CultureInfo.CurrentCulture,
Strings.Protocol_UnsupportedVersion,
versionString));
}
return new ServiceIndexResourceV3(index, utcNow, source);
}
protected class ServiceIndexCacheInfo
{
public ServiceIndexResourceV3 Index { get; set; }
public DateTime CachedTime { get; set; }
}
}
}