Skip to content

Commit 0b14e91

Browse files
authored
Minor refactoring (#3706)
1 parent 0510a17 commit 0b14e91

File tree

3 files changed

+55
-57
lines changed

3 files changed

+55
-57
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "feature",
3+
"category": "AWS CRT HTTP Client",
4+
"contributor": "",
5+
"description": "Renamed `readBufferSize` -> `readBufferSizeInBytes`."
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "removal",
3+
"category": "AWS CRT HTTP Client",
4+
"contributor": "",
5+
"description": "Removed `tlsCipherPreference`."
6+
}

http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/AwsCrtAsyncHttpClient.java

+43-57
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@
5757
* Http Web Services. This client is asynchronous and uses non-blocking IO.
5858
*
5959
* <p>This can be created via {@link #builder()}</p>
60+
* {@snippet :
61+
SdkAsyncHttpClient client = AwsCrtAsyncHttpClient.builder()
62+
.maxConcurrency(100)
63+
.connectionTimeout(Duration.ofSeconds(1))
64+
.connectionMaxIdleTime(Duration.ofSeconds(5))
65+
.build();
66+
* }
67+
*
6068
*
6169
* <b>NOTE:</b> This is a Preview API and is subject to change so it should not be used in production.
6270
*/
@@ -89,24 +97,20 @@ public final class AwsCrtAsyncHttpClient implements SdkAsyncHttpClient {
8997
private boolean isClosed = false;
9098

9199
private AwsCrtAsyncHttpClient(DefaultBuilder builder, AttributeMap config) {
92-
int maxConns = config.get(SdkHttpConfigurationOption.MAX_CONNECTIONS);
93-
94-
Validate.isPositive(maxConns, "maxConns");
95-
Validate.notNull(builder.cipherPreference, "cipherPreference");
96-
Validate.isPositive(builder.readBufferSize, "readBufferSize");
97100

98101
try (ClientBootstrap clientBootstrap = new ClientBootstrap(null, null);
99102
SocketOptions clientSocketOptions = buildSocketOptions(builder, config);
100-
TlsContextOptions clientTlsContextOptions = TlsContextOptions.createDefaultClient() // NOSONAR
101-
.withCipherPreference(builder.cipherPreference)
102-
.withVerifyPeer(!config.get(SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES));
103+
TlsContextOptions clientTlsContextOptions =
104+
TlsContextOptions.createDefaultClient()
105+
.withCipherPreference(TlsCipherPreference.TLS_CIPHER_SYSTEM_DEFAULT)
106+
.withVerifyPeer(!config.get(SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES));
103107
TlsContext clientTlsContext = new TlsContext(clientTlsContextOptions)) {
104108

105109
this.bootstrap = registerOwnedResource(clientBootstrap);
106110
this.socketOptions = registerOwnedResource(clientSocketOptions);
107111
this.tlsContext = registerOwnedResource(clientTlsContext);
108-
this.readBufferSize = builder.readBufferSize;
109-
this.maxConnectionsPerEndpoint = maxConns;
112+
this.readBufferSize = builder.readBufferSize == null ? DEFAULT_STREAM_WINDOW_SIZE : builder.readBufferSize;
113+
this.maxConnectionsPerEndpoint = config.get(SdkHttpConfigurationOption.MAX_CONNECTIONS);
110114
this.monitoringOptions = revolveHttpMonitoringOptions(builder.connectionHealthChecksConfiguration);
111115
this.maxConnectionIdleInMilliseconds = config.get(SdkHttpConfigurationOption.CONNECTION_MAX_IDLE_TIMEOUT).toMillis();
112116
this.proxyOptions = buildProxyOptions(builder.proxyConfiguration);
@@ -317,14 +321,7 @@ public interface Builder extends SdkAsyncHttpClient.Builder<AwsCrtAsyncHttpClien
317321
* @param maxConcurrency maximum concurrency per endpoint
318322
* @return The builder of the method chaining.
319323
*/
320-
Builder maxConcurrency(int maxConcurrency);
321-
322-
/**
323-
* The AWS CRT TlsCipherPreference to use for this Client
324-
* @param tlsCipherPreference The AWS Common Runtime TlsCipherPreference
325-
* @return The builder of the method chaining.
326-
*/
327-
Builder tlsCipherPreference(TlsCipherPreference tlsCipherPreference);
324+
Builder maxConcurrency(Integer maxConcurrency);
328325

329326
/**
330327
* Configures the number of unread bytes that can be buffered in the
@@ -336,7 +333,7 @@ public interface Builder extends SdkAsyncHttpClient.Builder<AwsCrtAsyncHttpClien
336333
*
337334
* TODO: This is also used for the write buffer size. Should we rename it?
338335
*/
339-
Builder readBufferSize(int readBufferSize);
336+
Builder readBufferSizeInBytes(Integer readBufferSize);
340337

341338
/**
342339
* Sets the http proxy configuration to use for this client.
@@ -357,8 +354,10 @@ public interface Builder extends SdkAsyncHttpClient.Builder<AwsCrtAsyncHttpClien
357354
* Configure the health checks for all connections established by this client.
358355
*
359356
* <p>
360-
* eg: you can set a throughput threshold for a connection to be considered healthy.
361-
* If the connection falls below this threshold for a configurable amount of time,
357+
* You can set a throughput threshold for a connection to be considered healthy.
358+
* If a connection falls below this threshold ({@link ConnectionHealthChecksConfiguration#minThroughputInBytesPerSecond()
359+
* }) for the configurable amount
360+
* of time ({@link ConnectionHealthChecksConfiguration#allowableThroughputFailureInterval()}),
362361
* then the connection is considered unhealthy and will be shut down.
363362
*
364363
* @param healthChecksConfiguration The health checks config to use
@@ -367,12 +366,8 @@ public interface Builder extends SdkAsyncHttpClient.Builder<AwsCrtAsyncHttpClien
367366
Builder connectionHealthChecksConfiguration(ConnectionHealthChecksConfiguration healthChecksConfiguration);
368367

369368
/**
370-
* A convenience method to configure the health checks for all connections established by this client.
371-
*
372-
* <p>
373-
* eg: you can set a throughput threshold for a connection to be considered healthy.
374-
* If the connection falls below this threshold for a configurable amount of time,
375-
* then the connection is considered unhealthy and will be shut down.
369+
* A convenience method that creates an instance of the {@link ConnectionHealthChecksConfiguration} builder, avoiding the
370+
* need to create one manually via {@link ConnectionHealthChecksConfiguration#builder()}.
376371
*
377372
* @param healthChecksConfigurationBuilder The health checks config builder to use
378373
* @return The builder of the method chaining.
@@ -382,43 +377,44 @@ Builder connectionHealthChecksConfiguration(Consumer<ConnectionHealthChecksConfi
382377
healthChecksConfigurationBuilder);
383378

384379
/**
385-
* The amount of time to wait when initially establishing a connection before giving up and timing out. The maximum
386-
* possible value, in ms, is the value of {@link Integer#MAX_VALUE}, any longer duration will be reduced to the maximum
387-
* possible value. If not specified, the connection timeout duration will be set to value defined in
388-
* {@link AwsCrtAsyncHttpClient#CRT_SDK_DEFAULT_CONNECTION_TIMEOUT}.
380+
* Configure the maximum amount of time that a connection should be allowed to remain open while idle.
381+
* @param connectionMaxIdleTime the maximum amount of connection idle time
382+
* @return The builder of the method chaining.
389383
*/
390384
Builder connectionMaxIdleTime(Duration connectionMaxIdleTime);
391385

392386
/**
393-
* Configure connection socket timeout
387+
* The amount of time to wait when initially establishing a connection before giving up and timing out.
388+
* @param connectionTimeout timeout
389+
* @return The builder of the method chaining.
394390
*/
395391
Builder connectionTimeout(Duration connectionTimeout);
396392

397393
/**
398-
* Configure whether to enable TCP Keep-alive and relevant configuration for all connections established by this client.
394+
* Configure whether to enable {@code tcpKeepAlive} and relevant configuration for all connections established by this
395+
* client.
399396
*
400397
* <p>
401-
* By default, keepAlive is disabled and this is not required.
402-
* tcpKeepAlive is enabled by providing this configuration and specifying
403-
* periodic keepalive packet intervals and timeouts
404-
* This may be required for certain connections for longer durations than default socket timeouts
398+
* By default, tcpKeepAlive is disabled. You can enable {@code tcpKeepAlive} by providing this configuration
399+
* and specifying periodic TCP keepalive packet intervals and timeouts. This may be required for certain connections for
400+
* longer durations than default socket timeouts.
405401
*
406402
* @param tcpKeepAliveConfiguration The TCP keep-alive configuration to use
407403
* @return The builder of the method chaining.
408404
*/
409405
Builder tcpKeepAliveConfiguration(TcpKeepAliveConfiguration tcpKeepAliveConfiguration);
410406

411407
/**
412-
* Configure whether to enable TCP Keep-alive and relevant configuration for all connections established by this client.
408+
* Configure whether to enable {@code tcpKeepAlive} and relevant configuration for all connections established by this
409+
* client.
413410
*
414411
* <p>
415-
* By default, keepAlive is disabled and this is not required.
416-
* tcpKeepAlive is enabled by providing this configuration and specifying
417-
* periodic keepalive packet intervals and timeouts
418-
* This may be required for certain connections for longer durations than default socket timeouts
412+
* A convenience method that creates an instance of the {@link TcpKeepAliveConfiguration} builder, avoiding the
413+
* need to create one manually via {@link TcpKeepAliveConfiguration#builder()}.
419414
*
420415
* @param tcpKeepAliveConfigurationBuilder The TCP keep-alive configuration builder to use
421416
* @return The builder of the method chaining.
417+
* @see #tcpKeepAliveConfiguration(TcpKeepAliveConfiguration)
422418
*/
423419
Builder tcpKeepAliveConfiguration(Consumer<TcpKeepAliveConfiguration.Builder>
424420
tcpKeepAliveConfigurationBuilder);
@@ -430,8 +426,7 @@ Builder tcpKeepAliveConfiguration(Consumer<TcpKeepAliveConfiguration.Builder>
430426
*/
431427
private static final class DefaultBuilder implements Builder {
432428
private final AttributeMap.Builder standardOptions = AttributeMap.builder();
433-
private TlsCipherPreference cipherPreference = TlsCipherPreference.TLS_CIPHER_SYSTEM_DEFAULT;
434-
private int readBufferSize = DEFAULT_STREAM_WINDOW_SIZE;
429+
private Integer readBufferSize;
435430
private ProxyConfiguration proxyConfiguration;
436431
private ConnectionHealthChecksConfiguration connectionHealthChecksConfiguration;
437432
private TcpKeepAliveConfiguration tcpKeepAliveConfiguration;
@@ -455,24 +450,15 @@ public SdkAsyncHttpClient buildWithDefaults(AttributeMap serviceDefaults) {
455450
}
456451

457452
@Override
458-
public Builder maxConcurrency(int maxConcurrency) {
459-
Validate.isPositive(maxConcurrency, "maxConcurrency");
453+
public Builder maxConcurrency(Integer maxConcurrency) {
454+
Validate.isPositiveOrNull(maxConcurrency, "maxConcurrency");
460455
standardOptions.put(SdkHttpConfigurationOption.MAX_CONNECTIONS, maxConcurrency);
461456
return this;
462457
}
463458

464459
@Override
465-
public Builder tlsCipherPreference(TlsCipherPreference tlsCipherPreference) {
466-
Validate.notNull(tlsCipherPreference, "cipherPreference");
467-
Validate.isTrue(TlsContextOptions.isCipherPreferenceSupported(tlsCipherPreference),
468-
"TlsCipherPreference not supported on current Platform");
469-
this.cipherPreference = tlsCipherPreference;
470-
return this;
471-
}
472-
473-
@Override
474-
public Builder readBufferSize(int readBufferSize) {
475-
Validate.isPositive(readBufferSize, "readBufferSize");
460+
public Builder readBufferSizeInBytes(Integer readBufferSize) {
461+
Validate.isPositiveOrNull(readBufferSize, "readBufferSize");
476462
this.readBufferSize = readBufferSize;
477463
return this;
478464
}

0 commit comments

Comments
 (0)