Skip to content

Commit 3fb7211

Browse files
Merge pull request #3423 from paulomorgado/performance/string-builder-1
Refactor User-Agent construction in Marshaller.cs
2 parents 0b38792 + 8dc4639 commit 3fb7211

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"core": {
3+
"changeLogMessages": [
4+
"Improve user agent string construction in Marshaller.cs"
5+
],
6+
"type": "patch",
7+
"updateMinimum": true
8+
}
9+
}

sdk/src/Core/Amazon.Runtime/Pipeline/Handlers/Marshaller.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System;
1717
using System.Collections.Generic;
18+
using System.Diagnostics;
1819
using System.Globalization;
1920
using System.Text;
2021
using Amazon.Runtime.Telemetry;
@@ -106,29 +107,28 @@ private static void SetRecursionDetectionHeader(IDictionary<string, string> head
106107

107108
private static void SetUserAgentHeader(IRequestContext requestContext)
108109
{
109-
var sb = new StringBuilder(requestContext.ClientConfig.UserAgent);
110+
var sb = new StringBuilder(256);
111+
112+
sb.Append(InternalSDKUtils.ReplaceInvalidUserAgentCharacters(requestContext.ClientConfig.UserAgent));
110113

111114
var clientAppId = requestContext.ClientConfig.ClientAppId;
112115
if (!string.IsNullOrEmpty(clientAppId))
113-
sb.AppendFormat(" app/{0}", clientAppId);
116+
sb.Append(" app/").Append(InternalSDKUtils.ReplaceInvalidUserAgentCharacters(clientAppId));
114117

115-
var retryMode = requestContext.ClientConfig.RetryMode.ToString().ToLower();
116-
sb.AppendFormat(" cfg/retry-mode#{0}", retryMode);
118+
sb.Append(" cfg/retry-mode#}").Append(ToUserAgentHeaderString(requestContext.ClientConfig.RetryMode));
117119

118-
sb.AppendFormat(" md/{0}", requestContext.IsAsync ? "ClientAsync" : "ClientSync");
120+
sb.Append(" md/").Append(requestContext.IsAsync ? "ClientAsync" : "ClientSync");
119121

120-
sb.AppendFormat(" cfg/init-coll#{0}", AWSConfigs.InitializeCollections ? "1" : "0");
122+
sb.Append(" cfg/init-coll#").Append(AWSConfigs.InitializeCollections ? '1' : '0');
121123

122124
var userAgentAddition = requestContext.OriginalRequest.UserAgentAddition;
123125
if (!string.IsNullOrEmpty(userAgentAddition))
124126
{
125-
sb.AppendFormat(" {0}", userAgentAddition);
127+
sb.Append(' ').Append(InternalSDKUtils.ReplaceInvalidUserAgentCharacters(userAgentAddition));
126128
}
127129

128130
var userAgent = sb.ToString();
129131

130-
userAgent = InternalSDKUtils.ReplaceInvalidUserAgentCharacters(userAgent);
131-
132132
if (requestContext.ClientConfig.UseAlternateUserAgentHeader)
133133
{
134134
requestContext.Request.Headers[HeaderKeys.XAmzUserAgentHeader] = userAgent;
@@ -138,5 +138,18 @@ private static void SetUserAgentHeader(IRequestContext requestContext)
138138
requestContext.Request.Headers[HeaderKeys.UserAgentHeader] = userAgent;
139139
}
140140
}
141+
142+
private static string ToUserAgentHeaderString(RequestRetryMode requestRetryMode)
143+
{
144+
switch (requestRetryMode)
145+
{
146+
case RequestRetryMode.Standard:
147+
return "standard";
148+
case RequestRetryMode.Adaptive:
149+
return "adaptive";
150+
default:
151+
return requestRetryMode.ToString().ToLowerInvariant();
152+
}
153+
}
141154
}
142155
}

sdk/src/Core/GlobalSuppressions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@
191191
[module: SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Scope = "member", Target = "Amazon.Runtime.Internal.Auth.AWS4Signer.#CanonicalizeHeaders(System.Collections.Generic.ICollection`1<System.Collections.Generic.KeyValuePair`2<System.String,System.String>>)")]
192192
[module: SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Scope = "member", Target = "Amazon.Runtime.Internal.Auth.S3Signer.#BuildCanonicalizedHeaders(System.Collections.Generic.IDictionary`2<System.String,System.String>)")]
193193
[module: SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Scope = "member", Target = "Amazon.Runtime.Internal.Auth.AWS4Signer.#DetermineSigningRegion(Amazon.Runtime.IClientConfig,System.String,Amazon.RegionEndpoint,Amazon.Runtime.Internal.IRequest)")]
194-
195194
[module: SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Scope = "member", Target = "Amazon.Runtime.Internal.Auth.AWS4Signer.#CanonicalizeHeaders(System.Collections.Generic.IDictionary`2<System.String,System.String>)")]
196195
[module: SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Scope = "member", Target = "Amazon.Runtime.Internal.Auth.AWS4Signer.#CanonicalizeHeaderNames(System.Collections.Generic.IDictionary`2<System.String,System.String>)")]
196+
[module: SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Scope = "member", Target = "Amazon.Runtime.Internal.Marshaller.#ToUserAgentHeaderString(Amazon.Runtime.RequestRetryMode)")]
197197

198198
// Types names matching namespaces
199199
[module: SuppressMessage("Microsoft.Naming", "CA1724:TypeNamesShouldNotMatchNamespaces", Scope = "type", Target = "Amazon.Auth.AccessControlPolicy.Policy")]

sdk/test/Services/SecurityToken/UnitTests/Custom/StaticCheckTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,15 @@ public void EnsureCredentialProfileDetectorSetup()
8383
// the properties are used at least once.
8484
Assert.IsTrue(profileOptionsProperties.SetEquals(referencedProfileOptionsProperties));
8585
}
86+
87+
[TestMethod]
88+
public void LookForRequestRetryModeChanges()
89+
{
90+
var expectedHash = "2FC699DB6A4284C5D53F3B7FA4E64165576FD1F6AE458BC737AE503F06DFBE4C";
91+
AssertExtensions.AssertEnumUnchanged(
92+
typeof(RequestRetryMode),
93+
expectedHash,
94+
"The Amazon.Runtime.Internal.Marshaller.ToUserAgentHeaderString method implementation may need to be updated.");
95+
}
8696
}
8797
}

0 commit comments

Comments
 (0)