-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathBackoffLogicManager.cs
More file actions
205 lines (171 loc) · 7.74 KB
/
Copy pathBackoffLogicManager.cs
File metadata and controls
205 lines (171 loc) · 7.74 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
namespace Microsoft.ApplicationInsights.Channel.Implementation
{
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation;
internal class BackoffLogicManager
{
internal const int SlotDelayInSeconds = 10;
private const int MaxDelayInSeconds = 3600;
private const int DefaultBackoffEnabledReportingIntervalInMin = 30;
private static readonly Random Random = new Random();
private static readonly DataContractJsonSerializer Serializer = new DataContractJsonSerializer(typeof(BackendResponse));
private readonly object lockConsecutiveErrors = new object();
private readonly TimeSpan minIntervalToUpdateConsecutiveErrors;
private bool exponentialBackoffReported = false;
private int consecutiveErrors;
private DateTimeOffset nextMinTimeToUpdateConsecutiveErrors = DateTimeOffset.MinValue;
public BackoffLogicManager()
{
this.DefaultBackoffEnabledReportingInterval = TimeSpan.FromMinutes(DefaultBackoffEnabledReportingIntervalInMin);
this.minIntervalToUpdateConsecutiveErrors = TimeSpan.FromSeconds(SlotDelayInSeconds);
this.CurrentDelay = TimeSpan.FromSeconds(SlotDelayInSeconds);
}
public BackoffLogicManager(TimeSpan defaultBackoffEnabledReportingInterval)
{
this.DefaultBackoffEnabledReportingInterval = defaultBackoffEnabledReportingInterval;
this.minIntervalToUpdateConsecutiveErrors = TimeSpan.FromSeconds(SlotDelayInSeconds);
this.CurrentDelay = TimeSpan.FromSeconds(SlotDelayInSeconds);
}
internal BackoffLogicManager(TimeSpan defaultBackoffEnabledReportingInterval, TimeSpan minIntervalToUpdateConsecutiveErrors)
: this(defaultBackoffEnabledReportingInterval)
{
// This constructor is used from unit tests
this.minIntervalToUpdateConsecutiveErrors = minIntervalToUpdateConsecutiveErrors;
}
/// <summary>
/// Gets the number of consecutive errors SDK transmitter got so far while sending telemetry to backend.
/// </summary>
public int ConsecutiveErrors
{
get { return this.consecutiveErrors; }
}
/// <summary>
/// Gets the last status code SDK received from the backend.
/// </summary>
public int LastStatusCode { get; private set; }
public TimeSpan DefaultBackoffEnabledReportingInterval { get; set; }
internal TimeSpan CurrentDelay { get; private set; }
public static BackendResponse GetBackendResponse(string responseContent)
{
BackendResponse backendResponse = null;
try
{
if (!string.IsNullOrEmpty(responseContent))
{
using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(responseContent)))
{
backendResponse = Serializer.ReadObject(ms) as BackendResponse;
}
}
}
catch (ArgumentException exp)
{
TelemetryChannelEventSource.Log.BreezeResponseWasNotParsedWarning(exp.Message, responseContent);
backendResponse = null;
}
catch (InvalidOperationException exp)
{
TelemetryChannelEventSource.Log.BreezeResponseWasNotParsedWarning(exp.Message, responseContent);
backendResponse = null;
}
catch (SerializationException exp)
{
TelemetryChannelEventSource.Log.BreezeResponseWasNotParsedWarning(exp.Message, responseContent);
backendResponse = null;
}
return backendResponse;
}
/// <summary>
/// Sets ConsecutiveErrors to 0.
/// </summary>
public void ResetConsecutiveErrors()
{
lock (this.lockConsecutiveErrors)
{
this.consecutiveErrors = 0;
}
}
public void ReportBackoffEnabled(int statusCode)
{
this.LastStatusCode = statusCode;
if (!this.exponentialBackoffReported && this.CurrentDelay > this.DefaultBackoffEnabledReportingInterval)
{
TelemetryChannelEventSource.Log.BackoffEnabled(this.CurrentDelay.TotalMinutes, statusCode);
this.exponentialBackoffReported = true;
}
lock (this.lockConsecutiveErrors)
{
// Do not increase number of errors more often than minimum interval (SlotDelayInSeconds)
// since we have 3 senders and all of them most likely would fail if we have intermittent error
if (DateTimeOffset.UtcNow > this.nextMinTimeToUpdateConsecutiveErrors)
{
this.consecutiveErrors++;
this.nextMinTimeToUpdateConsecutiveErrors = DateTimeOffset.UtcNow + this.minIntervalToUpdateConsecutiveErrors;
}
}
}
public void ReportBackoffDisabled()
{
this.LastStatusCode = 200;
if (this.exponentialBackoffReported)
{
TelemetryChannelEventSource.Log.BackoffDisabled();
this.exponentialBackoffReported = false;
}
}
public TimeSpan GetBackOffTimeInterval(string headerValue)
{
TimeSpan backOffTime = this.GetBackOffTime(headerValue);
this.CurrentDelay = backOffTime;
return backOffTime;
}
// Calculates the time to wait before retrying in case of an error based on
// http://en.wikipedia.org/wiki/Exponential_backoff
protected virtual TimeSpan GetBackOffTime(string headerValue)
{
if (!TryParseRetryAfter(headerValue, out TimeSpan retryAfterTimeSpan))
{
double delayInSeconds;
if (this.ConsecutiveErrors <= 1)
{
delayInSeconds = SlotDelayInSeconds;
}
else
{
double backOffSlot = (Math.Pow(2, this.ConsecutiveErrors) - 1) / 2;
var backOffDelay = Random.Next(1, (int)Math.Min(backOffSlot * SlotDelayInSeconds, int.MaxValue));
delayInSeconds = Math.Max(Math.Min(backOffDelay, MaxDelayInSeconds), SlotDelayInSeconds);
}
TelemetryChannelEventSource.Log.BackoffTimeSetInSeconds(delayInSeconds);
retryAfterTimeSpan = TimeSpan.FromSeconds(delayInSeconds);
}
TelemetryChannelEventSource.Log.BackoffInterval(retryAfterTimeSpan.TotalSeconds);
return retryAfterTimeSpan;
}
private static bool TryParseRetryAfter(string retryAfter, out TimeSpan retryAfterTimeSpan)
{
retryAfterTimeSpan = TimeSpan.FromSeconds(0);
if (string.IsNullOrEmpty(retryAfter))
{
return false;
}
TelemetryChannelEventSource.Log.RetryAfterHeaderIsPresent(retryAfter);
var now = DateTimeOffset.UtcNow;
if (DateTimeOffset.TryParse(retryAfter, out DateTimeOffset retryAfterDate))
{
if (retryAfterDate > now)
{
retryAfterTimeSpan = retryAfterDate - now;
return true;
}
return false;
}
TelemetryChannelEventSource.Log.TransmissionPolicyRetryAfterParseFailedWarning(retryAfter);
return false;
}
}
}