-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
SyncTokenPolicy.cs
63 lines (55 loc) · 1.95 KB
/
SyncTokenPolicy.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Collections.Concurrent;
using System.Collections.Generic;
using Azure.Core;
using Azure.Core.Pipeline;
namespace Azure.Data.AppConfiguration
{
internal class SyncTokenPolicy : HttpPipelineSynchronousPolicy
{
private const string SyncTokenHeader = "Sync-Token";
private readonly ConcurrentDictionary<string, SyncToken> _syncTokens;
public SyncTokenPolicy()
{
_syncTokens = new ConcurrentDictionary<string, SyncToken>();
}
public override void OnSendingRequest(HttpMessage message)
{
message.Request.Headers.Remove(SyncTokenHeader);
foreach (SyncToken token in _syncTokens.Values)
{
message.Request.Headers.Add(SyncTokenHeader, token.ToString());
}
}
public override void OnReceivedResponse(HttpMessage message)
{
if (message.Response.Headers.TryGetValues(SyncTokenHeader, out IEnumerable<string> rawSyncTokens))
{
foreach (string fullRawToken in rawSyncTokens)
{
AddToken(fullRawToken);
}
}
}
public void AddToken(string fullRawToken)
{
// Handle multiple header values.
string[] rawTokens = fullRawToken.Split(',');
foreach (string rawToken in rawTokens)
{
if (SyncTokenUtils.TryParse(rawToken, out SyncToken token))
{
_syncTokens.AddOrUpdate(token.Id, token, (key, existing) =>
{
if (existing.SequenceNumber < token.SequenceNumber)
{
return token;
}
return existing;
});
}
}
}
}
}