From 8aa39dd49347dd1fdd9e552ae5e3642edaa37819 Mon Sep 17 00:00:00 2001 From: Sergey Daletskiy Date: Fri, 22 Jun 2018 20:09:21 +0200 Subject: [PATCH 1/3] Fix for User-Agent header duplication on each http request --- Analytics/Request/BlockingRequestHandler.cs | 36 ++++++++++++--------- Test.Net45/Test.Net45.csproj | 4 +-- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/Analytics/Request/BlockingRequestHandler.cs b/Analytics/Request/BlockingRequestHandler.cs index 479ce40d..a9ef9a3c 100644 --- a/Analytics/Request/BlockingRequestHandler.cs +++ b/Analytics/Request/BlockingRequestHandler.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Diagnostics; using System.Net; #if NET35 @@ -107,6 +107,15 @@ internal BlockingRequestHandler(Client client, TimeSpan timeout) #if NET35 #else _httpClient = new HttpClient(handler) { Timeout = Timeout }; +#endif + // Send user agent in the form of {library_name}/{library_version} as per RFC 7231. + var context = new Context(); + var library = context["library"] as Dict; + string szUserAgent = string.Format("{0}/{1}", library["name"], library["version"]); +#if NET35 + _httpClient.Headers.Add("User-Agent", szUserAgent); +#else + _httpClient.DefaultRequestHeaders.Add("User-Agent", szUserAgent); #endif } @@ -132,16 +141,6 @@ public async Task MakeRequest(Batch batch) _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", BasicAuthHeader(batch.WriteKey, string.Empty)); #endif - // Send user agent in the form of {library_name}/{library_version} as per RFC 7231. - var context = new Context(); - var library = context["library"] as Dict; - string szUserAgent = string.Format("{0}/{1}", library["name"], library["version"]); -#if NET35 - _httpClient.Headers.Add("User-Agent", szUserAgent); -#else - _httpClient.DefaultRequestHeaders.Add("User-Agent", szUserAgent); -#endif - // Prepare request data; var requestData = Encoding.UTF8.GetBytes(json); @@ -165,12 +164,19 @@ public async Task MakeRequest(Batch batch) } } - Logger.Info("Sending analytics request to Segment.io ..", new Dict + var requestInfo = new Dict { { "batch id", batch.MessageId }, { "json size", json.Length }, { "batch size", batch.batch.Count } - }); + }; + + if (_client.Config.CompressRequest) + { + requestInfo.Add("gzipped json size", requestData.Length); + } + + Logger.Info("Sending analytics request to Segment.io ..", requestInfo); // Retries with exponential backoff const int MAXIMUM_BACKOFF_DURATION = 10000; // Set maximum waiting limit to 10s @@ -243,7 +249,7 @@ public async Task MakeRequest(Batch batch) // If status code is greater than 500 and less than 600, it indicates server error // Error code 429 indicates rate limited. // Retry uploading in these cases. - Task.Delay(backoff).Wait(); + await Task.Delay(backoff).ConfigureAwait(false); backoff *= 2; continue; } @@ -257,7 +263,7 @@ public async Task MakeRequest(Batch batch) #endif } - if (backoff == MAXIMUM_BACKOFF_DURATION && statusCode != (int)HttpStatusCode.OK) + if (backoff >= MAXIMUM_BACKOFF_DURATION || statusCode != (int)HttpStatusCode.OK) { Fail(batch, new APIException("Unexpected Status Code", responseStr), watch.ElapsedMilliseconds); } diff --git a/Test.Net45/Test.Net45.csproj b/Test.Net45/Test.Net45.csproj index 80f74ab7..ef7c6c7b 100644 --- a/Test.Net45/Test.Net45.csproj +++ b/Test.Net45/Test.Net45.csproj @@ -36,8 +36,8 @@ false - - ..\packages\NUnit.3.9.0\lib\net45\nunit.framework.dll + + ..\packages\NUnit.3.8.1\lib\net45\nunit.framework.dll From 7aa5458808718aae2504866f9e7d76619332cc0d Mon Sep 17 00:00:00 2001 From: Sergey Daletskiy Date: Fri, 22 Jun 2018 21:37:06 +0200 Subject: [PATCH 2/3] Cleanup --- Analytics/Request/BlockingRequestHandler.cs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/Analytics/Request/BlockingRequestHandler.cs b/Analytics/Request/BlockingRequestHandler.cs index a9ef9a3c..856ab358 100644 --- a/Analytics/Request/BlockingRequestHandler.cs +++ b/Analytics/Request/BlockingRequestHandler.cs @@ -164,19 +164,12 @@ public async Task MakeRequest(Batch batch) } } - var requestInfo = new Dict + Logger.Info("Sending analytics request to Segment.io ..", new Dict { { "batch id", batch.MessageId }, { "json size", json.Length }, { "batch size", batch.batch.Count } - }; - - if (_client.Config.CompressRequest) - { - requestInfo.Add("gzipped json size", requestData.Length); - } - - Logger.Info("Sending analytics request to Segment.io ..", requestInfo); + }); // Retries with exponential backoff const int MAXIMUM_BACKOFF_DURATION = 10000; // Set maximum waiting limit to 10s From ddf906dbaf5effb42efc70ac603e07670e612e5c Mon Sep 17 00:00:00 2001 From: Sergey Daletskiy Date: Sat, 23 Jun 2018 11:15:03 +0200 Subject: [PATCH 3/3] Removing changes that will go into other PRs --- Analytics/Request/BlockingRequestHandler.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Analytics/Request/BlockingRequestHandler.cs b/Analytics/Request/BlockingRequestHandler.cs index 856ab358..aab00a7f 100644 --- a/Analytics/Request/BlockingRequestHandler.cs +++ b/Analytics/Request/BlockingRequestHandler.cs @@ -242,7 +242,7 @@ public async Task MakeRequest(Batch batch) // If status code is greater than 500 and less than 600, it indicates server error // Error code 429 indicates rate limited. // Retry uploading in these cases. - await Task.Delay(backoff).ConfigureAwait(false); + Task.Delay(backoff).Wait(); backoff *= 2; continue; } @@ -256,7 +256,7 @@ public async Task MakeRequest(Batch batch) #endif } - if (backoff >= MAXIMUM_BACKOFF_DURATION || statusCode != (int)HttpStatusCode.OK) + if (backoff == MAXIMUM_BACKOFF_DURATION && statusCode != (int)HttpStatusCode.OK) { Fail(batch, new APIException("Unexpected Status Code", responseStr), watch.ElapsedMilliseconds); }