-
Notifications
You must be signed in to change notification settings - Fork 864
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to configure SocketOptions on the AwsCrtAsyncHttpClie…
…nt. This is necessary for any clients with long-running connections that exceed default socket timeouts of services along the call path, and need to enable keep-alive settings which the CRT client supports, but the Java client wasn't exposing to callers
- Loading branch information
Showing
5 changed files
with
396 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "feature", | ||
"category": "AWS SDK for Java v2", | ||
"contributor": "nikp", | ||
"description": "Add the ability to configure SocketOptions on the AwsCrtAsyncHttpClient to allow explicit enabling of keep-alive signals for long-running connections" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
215 changes: 215 additions & 0 deletions
215
...-crt-client/src/main/java/software/amazon/awssdk/http/crt/SocketOptionsConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.http.crt; | ||
|
||
import software.amazon.awssdk.annotations.SdkPreviewApi; | ||
import software.amazon.awssdk.annotations.SdkPublicApi; | ||
import software.amazon.awssdk.crt.io.SocketOptions; | ||
import software.amazon.awssdk.utils.Validate; | ||
|
||
/** | ||
* Configuration that defines socket options for all connections established by | ||
* the {@link SocketOptionsConfiguration}. | ||
* | ||
* <b>NOTE:</b> This is a Preview API and is subject to change so it should not be used in production. | ||
*/ | ||
@SdkPublicApi | ||
@SdkPreviewApi | ||
public final class SocketOptionsConfiguration { | ||
|
||
private final SocketOptions.SocketDomain domain; | ||
private final SocketOptions.SocketType type; | ||
private final int connectTimeoutMs; | ||
private final int keepAliveIntervalSecs; | ||
private final int keepAliveTimeoutSecs; | ||
|
||
private SocketOptionsConfiguration(DefaultSocketOptionsConfigurationBuilder builder) { | ||
this.domain = Validate.paramNotNull(builder.domain, | ||
"domain"); | ||
this.type = Validate.paramNotNull(builder.type, | ||
"type"); | ||
this.connectTimeoutMs = Validate.isPositive(builder.connectTimeoutMs, | ||
"connectTimeoutMs"); | ||
this.keepAliveIntervalSecs = Validate.isNotNegative(builder.keepAliveIntervalSecs, | ||
"keepAliveIntervalSecs"); | ||
this.keepAliveTimeoutSecs = Validate.isNotNegative(builder.keepAliveTimeoutSecs, | ||
"keepAliveTimeoutSecs"); | ||
} | ||
|
||
/** | ||
* @return socket domain | ||
*/ | ||
public SocketOptions.SocketDomain domain() { | ||
return domain; | ||
} | ||
|
||
/** | ||
* @return socket type | ||
*/ | ||
public SocketOptions.SocketType type() { | ||
return type; | ||
} | ||
|
||
/** | ||
* @return number of milliseconds before a connection will be considered timed out | ||
*/ | ||
public int connectTimeoutMs() { | ||
return connectTimeoutMs; | ||
} | ||
|
||
/** | ||
* @return number of seconds between TCP keepalive packets being sent to the peer | ||
*/ | ||
public int keepAliveIntervalSecs() { | ||
return keepAliveIntervalSecs; | ||
} | ||
|
||
/** | ||
* @return number of seconds to wait for a keepalive response before considering the connection timed out | ||
*/ | ||
public int keepAliveTimeoutSecs() { | ||
return keepAliveTimeoutSecs; | ||
} | ||
|
||
public static Builder builder() { | ||
return new DefaultSocketOptionsConfigurationBuilder(); | ||
} | ||
|
||
/** | ||
* A builder for {@link SocketOptionsConfiguration}. | ||
* | ||
* <p>All implementations of this interface are mutable and not thread safe.</p> | ||
*/ | ||
public interface Builder { | ||
|
||
/** | ||
* Sets the socket domain | ||
* @param domain socket domain | ||
* @return Builder | ||
*/ | ||
Builder domain(SocketOptions.SocketDomain domain); | ||
|
||
/** | ||
* Sets the socket type | ||
* @param type socket type | ||
* @return Builder | ||
*/ | ||
Builder type(SocketOptions.SocketType type); | ||
|
||
/** | ||
* Sets the number of milliseconds before a connection will be considered timed out | ||
* @param connectTimeoutMs number of milliseconds before a connection will be considered timed out | ||
* @return Builder | ||
*/ | ||
Builder connectTimeoutMs(int connectTimeoutMs); | ||
|
||
/** | ||
* Sets the number of seconds between TCP keepalive packets being sent to the peer | ||
* @param keepAliveIntervalSecs number of seconds between TCP keepalive packets being sent to the peer | ||
* @return Builder | ||
*/ | ||
Builder keepAliveIntervalSecs(int keepAliveIntervalSecs); | ||
|
||
/** | ||
* Sets the number of seconds to wait for a keepalive response before considering the connection timed out | ||
* @param keepAliveTimeoutSecs number of seconds to wait for a keepalive response before considering the connection timed out | ||
* @return Builder | ||
*/ | ||
Builder keepAliveTimeoutSecs(int keepAliveTimeoutSecs); | ||
|
||
SocketOptionsConfiguration build(); | ||
} | ||
|
||
/** | ||
* An SDK-internal implementation of {@link Builder}. | ||
*/ | ||
private static final class DefaultSocketOptionsConfigurationBuilder implements Builder { | ||
|
||
private SocketOptions.SocketDomain domain = SocketOptions.SocketDomain.IPv6; | ||
private SocketOptions.SocketType type = SocketOptions.SocketType.STREAM; | ||
private int connectTimeoutMs = 3000; | ||
private int keepAliveIntervalSecs = 0; | ||
private int keepAliveTimeoutSecs = 0; | ||
|
||
private DefaultSocketOptionsConfigurationBuilder() { | ||
} | ||
|
||
/** | ||
* Sets the socket domain | ||
* Default: {@link SocketOptions.SocketDomain#IPv6} | ||
* @param domain socket domain | ||
* @return Builder | ||
*/ | ||
@Override | ||
public Builder domain(SocketOptions.SocketDomain domain) { | ||
this.domain = domain; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the socket type | ||
* Default: {@link SocketOptions.SocketType#STREAM} | ||
* @param type socket type | ||
* @return Builder | ||
*/ | ||
@Override | ||
public Builder type(SocketOptions.SocketType type) { | ||
this.type = type; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the number of milliseconds before a connection will be considered timed out | ||
* Default: 3000ms | ||
* @param connectTimeoutMs number of milliseconds before a connection will be considered timed out | ||
* @return Builder | ||
*/ | ||
@Override | ||
public Builder connectTimeoutMs(int connectTimeoutMs) { | ||
this.connectTimeoutMs = connectTimeoutMs; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the number of seconds between TCP keepalive packets being sent to the peer | ||
* Default: 0 disables keepalive | ||
* @param keepAliveIntervalSecs number of seconds between TCP keepalive packets being sent to the peer | ||
* @return Builder | ||
*/ | ||
@Override | ||
public Builder keepAliveIntervalSecs(int keepAliveIntervalSecs) { | ||
this.keepAliveIntervalSecs = keepAliveIntervalSecs; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the number of seconds to wait for a keepalive response before considering the connection timed out | ||
* Default: 0 disables keepalive | ||
* @param keepAliveTimeoutSecs number of seconds to wait for a keepalive response before considering the connection timed out | ||
* @return Builder | ||
*/ | ||
@Override | ||
public Builder keepAliveTimeoutSecs(int keepAliveTimeoutSecs) { | ||
this.keepAliveTimeoutSecs = keepAliveTimeoutSecs; | ||
return this; | ||
} | ||
|
||
@Override | ||
public SocketOptionsConfiguration build() { | ||
return new SocketOptionsConfiguration(this); | ||
} | ||
} | ||
} |
Oops, something went wrong.