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

Fix for User-Agent header duplication on each http request #83

Merged
merged 3 commits into from
Jun 25, 2018

Conversation

daletskyi
Copy link
Contributor

Currently "User-Agent" header is added on each MakeRequest call and it's value gets duplicated (docs). If client application uses Analytics.Client as a singleton then after some time "User-Agent" header reaches request limits on Segment and it will always return 400 Bad Request. This PR moves header initilialization to BlockingRequestHandlerconstructor and also fixes handling of errors.

@f2prateek
Copy link
Contributor

Thanks! This LGTM at a high level - @kevingilliard can you help review this?

@@ -243,7 +249,7 @@ internal BlockingRequestHandler(Client client, TimeSpan timeout)
// 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the MakeRequest method is async using Wait() instead of await can cause deadlock. I can move it to another PR if needed, just thought it is too small for separate one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to be to seperate if you don't mind! It makes it easy to independently revert the header change from this one if needed and also link/attribute properly from the changelog.

@@ -257,7 +263,7 @@ internal BlockingRequestHandler(Client client, TimeSpan timeout)
#endif
}

if (backoff == MAXIMUM_BACKOFF_DURATION && statusCode != (int)HttpStatusCode.OK)
if (backoff >= MAXIMUM_BACKOFF_DURATION || statusCode != (int)HttpStatusCode.OK)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

backoff is changed only after delay where it is multiplied by 2 each time. The initial value is 100, so it will be 200, 400, 800 and so on. MAXIMUM_BACKOFF_DURATION is set to 10000 so if statement will never be executed. The second part will work in the next situation: if you get response 400 before backoff reached max value then you need to check status code here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


if (_client.Config.CompressRequest)
{
requestInfo.Add("gzipped json size", requestData.Length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unrelated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, will remove it.

@@ -36,8 +36,8 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework, Version=3.9.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.9.0\lib\net45\nunit.framework.dll</HintPath>
<Reference Include="nunit.framework, Version=3.8.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious - why did this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't build test project because version 3.8.1 was referenced in packages.config. Since the same version is used across all other test projects I just fixed the path here.

@dailycoding
Copy link
Contributor

Hi, @f2prateek
What can I do for this ticket?

@f2prateek
Copy link
Contributor

@kevingilliard take a look at the changes and let me or @daletskyi know if you spot any issues with this PR or if it looks good to you :)

@f2prateek f2prateek merged commit 497bacf into segmentio:master Jun 25, 2018
@mig42 mig42 mentioned this pull request Jul 5, 2018
@daletskyi daletskyi deleted the request-fixes branch July 8, 2018 18:36
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants