|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Linq; |
| 6 | +using System.Net.Http; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.Extensions.Logging; |
| 9 | +using Newtonsoft.Json; |
| 10 | + |
| 11 | +namespace NuGet.Services.Metadata.Catalog |
| 12 | +{ |
| 13 | + public class HttpReadCursorWithUpdates : HttpReadCursor |
| 14 | + { |
| 15 | + private readonly ILogger _logger; |
| 16 | + |
| 17 | + public HttpReadCursorWithUpdates(Uri address, ILogger logger, Func<HttpMessageHandler> handlerFunc = null) |
| 18 | + : base(address, handlerFunc) |
| 19 | + { |
| 20 | + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| 21 | + } |
| 22 | + |
| 23 | + public override async Task<string> GetValueInJsonAsync(HttpResponseMessage response) |
| 24 | + { |
| 25 | + var storageDateTimeOffset = response.Headers.Date; |
| 26 | + DateTime? storageDateTimeInUtc = null; |
| 27 | + if (storageDateTimeOffset.HasValue) |
| 28 | + { |
| 29 | + storageDateTimeInUtc = storageDateTimeOffset.Value.UtcDateTime; |
| 30 | + } |
| 31 | + |
| 32 | + var update = await GetUpdate(response, storageDateTimeInUtc); |
| 33 | + |
| 34 | + return JsonConvert.SerializeObject(new { value = update.Value }); |
| 35 | + } |
| 36 | + |
| 37 | + private async Task<CursorValueUpdate> GetUpdate(HttpResponseMessage response, DateTime? storageDateTimeInUtc) |
| 38 | + { |
| 39 | + if (!storageDateTimeInUtc.HasValue) |
| 40 | + { |
| 41 | + throw new ArgumentNullException(nameof(storageDateTimeInUtc)); |
| 42 | + } |
| 43 | + |
| 44 | + var content = await response.Content.ReadAsStringAsync(); |
| 45 | + |
| 46 | + var cursorValueWithUpdates = JsonConvert.DeserializeObject<CursorValueWithUpdates>(content, CursorValueWithUpdates.SerializerSettings); |
| 47 | + var minIntervalBeforeToReadUpdate = cursorValueWithUpdates.MinIntervalBeforeToReadUpdate; |
| 48 | + var updates = cursorValueWithUpdates.Updates.OrderByDescending(u => u.TimeStamp).ToList(); |
| 49 | + |
| 50 | + foreach (var update in updates) |
| 51 | + { |
| 52 | + if (update.TimeStamp <= storageDateTimeInUtc.Value - minIntervalBeforeToReadUpdate) |
| 53 | + { |
| 54 | + _logger.LogInformation("Read the cursor update with timeStamp: {TimeStamp} and value: {UpdateValue}, at {Address}. " + |
| 55 | + "(Storage DateTime: {StorageDateTime}, MinIntervalBeforeToReadUpdate: {MinIntervalBeforeToReadUpdate})", |
| 56 | + update.TimeStamp.ToString(CursorValueWithUpdates.SerializerSettings.DateFormatString), |
| 57 | + update.Value, |
| 58 | + _address.AbsoluteUri, |
| 59 | + storageDateTimeInUtc.Value.ToString(CursorValueWithUpdates.SerializerSettings.DateFormatString), |
| 60 | + minIntervalBeforeToReadUpdate); |
| 61 | + |
| 62 | + return update; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (updates.Count > 0) |
| 67 | + { |
| 68 | + _logger.LogWarning("Unable to find the cursor update and the oldest cursor update has timeStamp: {TimeStamp}, at {Address}. " + |
| 69 | + "(Storage DateTime: {StorageDateTime}, MinIntervalBeforeToReadUpdate: {MinIntervalBeforeToReadUpdate})", |
| 70 | + updates.Last().TimeStamp.ToString(CursorValueWithUpdates.SerializerSettings.DateFormatString), |
| 71 | + _address.AbsoluteUri, |
| 72 | + storageDateTimeInUtc.Value.ToString(CursorValueWithUpdates.SerializerSettings.DateFormatString), |
| 73 | + minIntervalBeforeToReadUpdate); |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + _logger.LogWarning("Unable to find the cursor update and the count of updates is {CursorUpdatesCount}, at {Address}.", |
| 78 | + updates.Count, |
| 79 | + _address.AbsoluteUri); |
| 80 | + } |
| 81 | + |
| 82 | + throw new InvalidOperationException("Unable to find the cursor update."); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments