-
Notifications
You must be signed in to change notification settings - Fork 862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor User-Agent construction in Marshaller.cs #3423
Refactor User-Agent construction in Marshaller.cs #3423
Conversation
|
||
private static string ToUserAgentHeaderString(RequestRetryMode requestRetryMode) | ||
{ | ||
if (requestRetryMode == RequestRetryMode.Adaptive) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change seems a bit limited since we have Legacy
mode and we may add other retry modes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, @muhammad-othman, but I don't understand your point.
RequestRetryMode
has only 2 values: Adaptive
and Standard
. There's no Legacy
value.
This code can be generated using something like NetEscapades.EnumGenerators. This might me a bit more specific, but nothing that cannot be easily done. I just didn't want to taggle that in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a Debug.Assert
to assert the value is the expected one, when in DEBUG.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We had it before but looks like we removed it at V4, still we may add other RequestRetryMode
in the future,.
Legacy, |
Debug.Assert
looks like something we may miss, maybe we can add an else statement that throws an exception instead to be sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need something that catches that at build-time, not at run-time.
Having Debug.Assert
catches that at debug-time.
Comprehensive unit tests will also catch this kind of errors.
I don't mind pushing this to some utility class adding a comprehensive of unit tests, if you point me in the right direction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was just thinking that Debug.Assert
might be lost and someone adding a new retry mode will run atleast one request to test their changes, but I agree unit tests are much better.
You can use this utility method to make sure that the enum didn't change with a note to update ToUserAgentHeaderString
if it changed.
https://github.com/aws/aws-sdk-net/blob/v4-development/sdk/test/Common/Utils/AssertExtensions.cs#L137
And here is an example of its usage
public void LookForProfileTypeChanges() |
And you can add the tests here
https://github.com/aws/aws-sdk-net/blob/v4-development/sdk/test/UnitTests/Custom/Runtime
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And you can add the tests here https://github.com/aws/aws-sdk-net/blob/v4-development/sdk/test/UnitTests/Custom/Runtime
What solution would that be? There are 405 .sln
files in the repo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically any service solution will include the core UnitTestUtilities porject, here is an example from S3
Please include the newly add test file the following projects as we don't include all the files added to the Runtime folder by default
sdk\test\UnitTests\Custom\AWSSDK.UnitTestUtilities.NetFramework.csproj
sdk\test\UnitTests\Custom\AWSSDK.UnitTestUtilities.NetStandard.csproj
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After digging a little bit into the code, I realized that there is no validation when IRequestContext.ClientConfig.RetryMode
, so I changed ToUserAgentHeaderString
to have a fast path for know values and a fallback of requestRetryMode.ToString().ToLowerInvariant()
:
private static string ToUserAgentHeaderString(RequestRetryMode requestRetryMode)
{
switch (requestRetryMode)
{
case RequestRetryMode.Standard:
return "standard";
case RequestRetryMode.Adaptive:
return "adaptive";
default:
return requestRetryMode.ToString().ToLowerInvariant();
}
}
Added this to sdk/test/Services/SecurityToken/UnitTests/Custom/StaticCheckTests.cs:
[TestMethod]
public void LookForRequestRetryModeChanges()
{
var expectedHash = "2FC699DB6A4284C5D53F3B7FA4E64165576FD1F6AE458BC737AE503F06DFBE4C";
AssertExtensions.AssertEnumUnchanged(
typeof(RequestRetryMode),
expectedHash,
"The Amazon.Runtime.Internal.Marshaller.ToUserAgentHeaderString method implementation may need to be updated.");
}
Thank you for making the requested changes. |
I noticed that /// <summary>
/// RetryMode determines which request retry mode is used for requests that do
/// not complete successfully.
/// </summary>
public enum RequestRetryMode
{
/// <summary>
/// Legacy request retry strategy.
/// </summary>
Legacy,
/// <summary>
/// Standardized request retry strategy that is consistent across all SDKs.
/// </summary>
Standard,
/// <summary>
/// An experimental request retry strategy that builds on the Standard strategy
/// and introduces congestion control through client side rate limiting.
/// </summary>
Adaptive
} /// <summary>
/// RetryMode determines which request retry mode is used for requests that do
/// not complete successfully.
/// </summary>
public enum RequestRetryMode
{
/// <summary>
/// Standardized request retry strategy that is consistent across all SDKs.
/// </summary>
Standard,
/// <summary>
/// An experimental request retry strategy that builds on the Standard strategy
/// and introduces congestion control through client side rate limiting.
/// </summary>
Adaptive
} This might be an issue for anyone saving these settings as integers because it will be changing from:
to:
So, |
For the way this enum is used I don't see anybody relying on the integer value. It it would be set via the property on the config or via an environment variable which uses the string value. But it wouldn't be bad thing to assign |
@paulomorgado Our dry run failed because there conflicts between this branch and the v4-development, can you check these conflicts? |
6e384a2
to
9d267a2
Compare
Hi @muhammad-othman,
I don't know what those conflicts are, but I rebased on the latest commit of |
Hi @normj,
I'm using here an in-memory collection as a simple example, but it can be any configuration source: var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
["Amazon:KeyManagementService:RetryMode"] = "2",
})
.Build();
var keyManagmentServiceConfig = new AmazonKeyManagementServiceConfig();
configuration.GetSection("Amazon:KeyManagementService").Bind(keyManagmentServiceConfig);
Console.WriteLine($"The retry mode is: {keyManagmentServiceConfig.RetryMode}");
// The retry mode is: Adaptive The value of an |
9d267a2
to
58989b5
Compare
Hi @paulomorgado, |
Updated `SetUserAgentHeader` to improve User-Agent string construction: - Initialize `StringBuilder` with a capacity of 256. - Append `UserAgent` after replacing invalid characters. - Append `ClientAppId` after replacing invalid characters. - Use `ToUserAgentHeaderString` for retry mode conversion. - Append `IsAsync` and initialization config directly. - Append `UserAgentAddition` after replacing invalid characters. - Removed redundant call to `ReplaceInvalidUserAgentCharacters`. - Added `ToUserAgentHeaderString` method for `RequestRetryMode`.
Added a `using System.Diagnostics;` directive. Introduced a `retryMode` variable and added a `Debug.Assert` to ensure its correctness. Updated user agent string logic to use `retryMode` and `requestContext.IsAsync`. Changed initialization collections flag to `AWSConfigs.InitializeCollections`.
Refactored the ToUserAgentHeaderString method in Marshaller.cs to use a switch statement instead of if-else statements, including a default case for converting requestRetryMode to lowercase. Removed Debug.Assert for retryMode and directly appended it to the StringBuilder object. Added LookForRequestRetryModeChanges test in StaticCheckTests.cs to verify the integrity of the RequestRetryMode enum.
…serAgentHeaderString
36c21e0
to
8dc4639
Compare
That discussion was a general heads up on the issue. I don't have anything to add to this PR. |
Updated
SetUserAgentHeader
to improve User-Agent string construction:StringBuilder
with a capacity of 256.UserAgent
after replacing invalid characters.ClientAppId
after replacing invalid characters.ToUserAgentHeaderString
for retry mode conversion.IsAsync
and initialization config directly.UserAgentAddition
after replacing invalid characters.ReplaceInvalidUserAgentCharacters
.ToUserAgentHeaderString
method forRequestRetryMode
.Description
See #3412
Motivation and Context
See #3412
Testing
There should be no changes to the result.
Screenshots (if appropriate)
Types of changes
Checklist
License