Skip to content
This repository was archived by the owner on Apr 24, 2024. It is now read-only.

Commit 1096007

Browse files
authored
Merge pull request #162 from North-Two-Five/Issue161-EventsNotSentToSegmentServersUnexpectedStatusCode
Issue161 events not sent to segment servers unexpected status code
2 parents 470f657 + d5695e7 commit 1096007

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

Analytics/Exception/BadParameter.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ namespace Segment.Exception
77
public class APIException : System.Exception
88
{
99
public string Code { get; set; }
10-
public string message { get; set; }
1110

12-
public APIException(string code, string message) : base(message)
11+
public APIException(string code, string message) : base($"Status Code: {code}, Message: {message}")
1312
{
14-
this.Code = code;
15-
this.message = message;
13+
Code = code;
1614
}
1715
}
1816
}

Analytics/Request/Backo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class Backo
1717

1818

1919
public bool HasReachedMax => _currentAttemptTime >= _max;
20+
public int CurrentAttempt => _attempt;
2021

2122
public Backo(int min = 100, int max = 10000, byte factor = 2, ushort jitter = 10000)
2223
{

Analytics/Request/BlockingRequestHandler.cs

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,30 @@ public async Task MakeRequest(Batch batch)
227227
// Error code 429 indicates rate limited.
228228
// Retry uploading in these cases.
229229
Thread.Sleep(_backo.AttemptTime());
230+
if (statusCode == 429)
231+
{
232+
Logger.Info($"Too many request at the moment CurrentAttempt:{_backo.CurrentAttempt} Retrying to send request", new Dict
233+
{
234+
{ "batch id", batch.MessageId },
235+
{ "statusCode", statusCode },
236+
{ "duration (ms)", watch.ElapsedMilliseconds }
237+
});
238+
}
239+
else
240+
{
241+
Logger.Info($"Internal Segment Server error CurrentAttempt:{_backo.CurrentAttempt} Retrying to send request", new Dict
242+
{
243+
{ "batch id", batch.MessageId },
244+
{ "statusCode", statusCode },
245+
{ "duration (ms)", watch.ElapsedMilliseconds }
246+
});
247+
}
230248
continue;
231249
}
232-
else if (statusCode >= 400)
250+
else
233251
{
252+
//If status code is greater or equal than 400 but not 429 should indicate is client error.
253+
//All other types of HTTP Status code are not errors (Between 100 and 399)
234254
responseStr = String.Format("Status Code {0}. ", statusCode);
235255
responseStr += ex.Message;
236256
break;
@@ -277,21 +297,39 @@ public async Task MakeRequest(Batch batch)
277297
// Error code 429 indicates rate limited.
278298
// Retry uploading in these cases.
279299
await _backo.AttemptAsync();
300+
if (statusCode == 429)
301+
{
302+
Logger.Info($"Too many request at the moment CurrentAttempt:{_backo.CurrentAttempt} Retrying to send request", new Dict
303+
{
304+
{ "batch id", batch.MessageId },
305+
{ "statusCode", statusCode },
306+
{ "duration (ms)", watch.ElapsedMilliseconds }
307+
});
308+
}
309+
else
310+
{
311+
Logger.Info($"Internal Segment Server error CurrentAttempt:{_backo.CurrentAttempt} Retrying to send request", new Dict
312+
{
313+
{ "batch id", batch.MessageId },
314+
{ "statusCode", statusCode },
315+
{ "duration (ms)", watch.ElapsedMilliseconds }
316+
});
317+
}
280318
continue;
281319
}
282-
else if (statusCode >= 400)
320+
else
283321
{
284-
responseStr = String.Format("Status Code {0}. ", response.StatusCode);
285-
responseStr += await response.Content.ReadAsStringAsync().ConfigureAwait(false);
286322
break;
287323
}
288324
}
289325
#endif
290326
}
291327

292-
if (_backo.HasReachedMax || statusCode != (int)HttpStatusCode.OK)
328+
var hasBackoReachedMax = _backo.HasReachedMax;
329+
if (hasBackoReachedMax || statusCode != (int)HttpStatusCode.OK)
293330
{
294-
Fail(batch, new APIException("Unexpected Status Code", responseStr), watch.ElapsedMilliseconds);
331+
var message = $"Has Backoff reached max: {hasBackoReachedMax} with number of Attempts:{_backo.CurrentAttempt},\n Status Code: {statusCode}\n, response message: {responseStr}";
332+
Fail(batch, new APIException(statusCode.ToString(), message), watch.ElapsedMilliseconds);
295333
if (_backo.HasReachedMax)
296334
{
297335
_backo.Reset();

Test/ConnectionTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public void RetryErrorTest()
4747
config.SetHost("https://fake.segment-server.com");
4848
config.SetTimeout(new TimeSpan(0, 0, 1));
4949
Analytics.Initialize(Constants.WRITE_KEY, config);
50-
5150
// Calculate working time for Identiy message with invalid host address
5251
watch.Start();
5352
Actions.Identify(Analytics.Client);
@@ -57,9 +56,9 @@ public void RetryErrorTest()
5756
Assert.AreEqual(0, Analytics.Client.Statistics.Succeeded);
5857
Assert.AreEqual(1, Analytics.Client.Statistics.Failed);
5958

60-
// Handling Identify message will take more than 10s even though the timeout is 1s.
59+
// Handling Identify message will take more than 5s even though the timeout is 1s.
6160
// That's because it retries submit when it's failed.
62-
Assert.AreEqual(true, watch.ElapsedMilliseconds > 10000);
61+
Assert.AreEqual(true, watch.ElapsedMilliseconds > 5000);
6362
}
6463

6564
[Test()]

0 commit comments

Comments
 (0)