| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Collections.Concurrent; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using Azure.Core; |
| | 7 | | using Azure.Core.Pipeline; |
| | 8 | |
|
| | 9 | | namespace Azure.Data.AppConfiguration |
| | 10 | | { |
| | 11 | | internal class SyncTokenPolicy : HttpPipelineSynchronousPolicy |
| | 12 | | { |
| | 13 | | private const string SyncTokenHeader = "Sync-Token"; |
| | 14 | |
|
| | 15 | | private readonly ConcurrentDictionary<string, SyncToken> _syncTokens; |
| | 16 | |
|
| 344 | 17 | | public SyncTokenPolicy() |
| | 18 | | { |
| 344 | 19 | | _syncTokens = new ConcurrentDictionary<string, SyncToken>(); |
| 344 | 20 | | } |
| | 21 | |
|
| | 22 | | public override void OnSendingRequest(HttpMessage message) |
| | 23 | | { |
| 840 | 24 | | message.Request.Headers.Remove(SyncTokenHeader); |
| 2648 | 25 | | foreach (SyncToken token in _syncTokens.Values) |
| | 26 | | { |
| 484 | 27 | | message.Request.Headers.Add(SyncTokenHeader, token.ToString()); |
| | 28 | | } |
| 840 | 29 | | } |
| | 30 | |
|
| | 31 | | public override void OnReceivedResponse(HttpMessage message) |
| | 32 | | { |
| 840 | 33 | | if (message.Response.Headers.TryGetValues(SyncTokenHeader, out IEnumerable<string> rawSyncTokens)) |
| | 34 | | { |
| 2456 | 35 | | foreach (string fullRawToken in rawSyncTokens) |
| | 36 | | { |
| | 37 | | // Handle multiple header values. |
| 616 | 38 | | string[] rawTokens = fullRawToken.Split(','); |
| 2472 | 39 | | foreach (string rawToken in rawTokens) |
| | 40 | | { |
| 620 | 41 | | if (SyncTokenUtils.TryParse(rawToken, out SyncToken token)) |
| | 42 | | { |
| 620 | 43 | | _syncTokens.AddOrUpdate(token.Id, token, (key, existing) => |
| 620 | 44 | | { |
| 1036 | 45 | | if (existing.SequenceNumber < token.SequenceNumber) |
| 620 | 46 | | { |
| 904 | 47 | | return token; |
| 620 | 48 | | } |
| 620 | 49 | |
|
| 752 | 50 | | return existing; |
| 620 | 51 | | }); |
| | 52 | | } |
| | 53 | | } |
| | 54 | | } |
| | 55 | | } |
| 840 | 56 | | } |
| | 57 | | } |
| | 58 | | } |