Skip to content

Commit 16ce171

Browse files
authored
[V4] Add the ConnectTimeout property for .NET 8 target (#3819)
1 parent fbfd1ec commit 16ce171

File tree

12 files changed

+92
-4
lines changed

12 files changed

+92
-4
lines changed

extensions/src/AWSSDK.Extensions.NETCore.Setup/AWSSDK.Extensions.NETCore.Setup.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<metadata>
44
<id>AWSSDK.Extensions.NETCore.Setup</id>
55
<title>AWSSDK - Extensions for NETCore Setup</title>
6-
<version>4.0.0.0</version>
6+
<version>4.0.1.0</version>
77
<authors>Amazon Web Services</authors>
88
<description>Extensions for the AWS SDK for .NET to integrate with .NET Core configuration and dependency injection frameworks.</description>
99
<language>en-US</language>

extensions/src/AWSSDK.Extensions.NETCore.Setup/ClientFactory.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,12 @@ private ClientConfig CreateConfig(AWSOptions options)
339339
{
340340
config.Timeout = defaultConfig.Timeout.Value;
341341
}
342+
#if NET8_0_OR_GREATER
343+
if (defaultConfig.ConnectTimeout.HasValue)
344+
{
345+
config.ConnectTimeout = defaultConfig.ConnectTimeout.Value;
346+
}
347+
#endif
342348
if (defaultConfig.UseAlternateUserAgentHeader.HasValue)
343349
{
344350
config.UseAlternateUserAgentHeader = defaultConfig.UseAlternateUserAgentHeader.Value;

extensions/src/AWSSDK.Extensions.NETCore.Setup/ConfigurationExtensions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,17 @@ public static AWSOptions GetAWSOptions(this IConfiguration config, string config
313313

314314
options.DefaultClientConfig.Timeout = TimeSpan.FromMilliseconds(timeout);
315315
}
316+
#if NET8_0_OR_GREATER
317+
else if (string.Equals(element.Key, nameof(DefaultClientConfig.ConnectTimeout), StringComparison.OrdinalIgnoreCase))
318+
{
319+
if (!int.TryParse(element.Value, out var connectTimeout))
320+
{
321+
throw new ArgumentException($"Invalid integer value for {nameof(DefaultClientConfig.ConnectTimeout)}.");
322+
}
323+
324+
options.DefaultClientConfig.ConnectTimeout = TimeSpan.FromMilliseconds(connectTimeout);
325+
}
326+
#endif
316327
else if (string.Equals(element.Key, nameof(DefaultClientConfig.UseAlternateUserAgentHeader), StringComparison.OrdinalIgnoreCase))
317328
{
318329
if (!bool.TryParse(element.Value, out var useAlternateUserAgentHeader))

extensions/src/AWSSDK.Extensions.NETCore.Setup/DefaultClientConfig.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,15 @@ public class DefaultClientConfig
214214
/// </summary>
215215
public TimeSpan? Timeout { get; set; }
216216

217+
#if NET8_0_OR_GREATER
218+
/// <summary>
219+
/// Gets and sets the connection timeout that will be set on the HttpClient used by the service client to make requests.
220+
/// The connection timeout is used control the wait time for the connection to be established to the service. The default
221+
/// connection timeout for the HttpClient infinite waiting period.
222+
/// </summary>
223+
public TimeSpan? ConnectTimeout { get; set; }
224+
#endif
225+
217226
/// <summary>
218227
/// When set to true, the service client will use the x-amz-user-agent
219228
/// header instead of the User-Agent header to report version and

extensions/test/NETCore.SetupTests/ConfigurationTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ public void GetClientConfigSettingsTest()
127127
var clientConfig = client.Config as AmazonS3Config;
128128
Assert.NotNull(clientConfig);
129129
Assert.True(clientConfig.ForcePathStyle);
130+
131+
#if NET8_0_OR_GREATER
132+
Assert.Equal(TimeSpan.FromMilliseconds(500), options.DefaultClientConfig.ConnectTimeout);
133+
Assert.Equal(TimeSpan.FromMilliseconds(500), clientConfig.ConnectTimeout);
134+
#endif
130135
}
131136

132137
[Fact]

extensions/test/NETCore.SetupTests/TestFiles/GetClientConfigSettingsTest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
"UseHttp": true,
44
"MaxErrorRetry": 6,
55
"Timeout": 1000,
6+
"ConnectTimeout": 500,
67
"AuthenticationRegion": "us-east-1",
78
"Region": "us-west-2",
89
"DefaultsMode": "Standard",
9-
"ForcePathStyle": true,
10+
"ForcePathStyle": true,
1011
"RetryMode": "Standard"
1112
}
1213
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"core": {
3+
"updateMinimum": true,
4+
"type": "Patch",
5+
"changeLogMessages": [
6+
"Add the ConnectTimeout property on the service client config for the .NET 8 target of the SDK."
7+
],
8+
"backwardIncompatibilitiesToIgnore": [
9+
"Amazon.Runtime.ClientConfig/MethodAdded",
10+
"Amazon.Runtime.IClientConfig/MethodAbstractMethodAdded"
11+
]
12+
}
13+
}

sdk/src/Core/Amazon.Runtime/ClientConfig.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,31 @@ public TimeSpan? Timeout
742742
}
743743
}
744744

745-
#if AWS_ASYNC_API
745+
#if NET8_0_OR_GREATER
746+
TimeSpan? _connectTimeout;
747+
748+
/// <summary>
749+
/// Gets and sets the connection timeout that will be set on the HttpClient used by the service client to make requests.
750+
/// The connection timeout is used control the wait time for the connection to be established to the service. The default
751+
/// connection timeout for the HttpClient is infinite waiting period.
752+
/// </summary>
753+
public TimeSpan? ConnectTimeout
754+
{
755+
get
756+
{
757+
if (!this._connectTimeout.HasValue)
758+
return null;
759+
760+
return this._connectTimeout.Value;
761+
}
762+
set
763+
{
764+
ValidateTimeout(value);
765+
this._connectTimeout = value;
766+
}
767+
}
768+
#endif
769+
746770
/// <summary>
747771
/// Generates a <see cref="CancellationToken"/> based on the value
748772
/// for <see cref="DefaultConfiguration.TimeToFirstByteTimeout"/>.
@@ -758,7 +782,6 @@ internal CancellationToken BuildDefaultCancellationToken()
758782
? new CancellationTokenSource(cancelTimeout.Value).Token
759783
: default(CancellationToken);
760784
}
761-
#endif
762785

763786
/// <summary>
764787
/// Configures the endpoint calculation for a service to go to a dual stack (ipv6 enabled) endpoint

sdk/src/Core/Amazon.Runtime/IClientConfig.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,13 @@ public partial interface IClientConfig
260260
/// </remarks>
261261
TimeSpan? Timeout { get; }
262262

263+
#if NET8_0_OR_GREATER
264+
/// <summary>
265+
/// Gets the connection timeout that will be set on the HttpClient used by the service client to make requests.
266+
/// The connection timeout is used control the wait time for the connection to be established to the service.
267+
/// </summary>
268+
TimeSpan? ConnectTimeout { get; }
269+
#endif
263270
/// <summary>
264271
/// Configures the endpoint calculation for a service to go to a dual stack (ipv6 enabled) endpoint
265272
/// for the configured region.

sdk/src/Core/Amazon.Runtime/Pipeline/HttpHandler/_netstandard/HttpRequestMessageFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ private static HttpClient CreateManagedHttpClient(IClientConfig clientConfig)
255255
// is going to be required.
256256
EnableMultipleHttp2Connections = true
257257
};
258+
259+
if (clientConfig.ConnectTimeout.HasValue)
260+
{
261+
httpMessageHandler.ConnectTimeout = clientConfig.ConnectTimeout.Value;
262+
}
258263
#else
259264
var httpMessageHandler = new HttpClientHandler();
260265
#endif

0 commit comments

Comments
 (0)