Skip to content

Fix ApacheHttpClient to use socket keep alive when appropriate #2373

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

Merged
merged 2 commits into from
Apr 8, 2021

Conversation

ajs139
Copy link

@ajs139 ajs139 commented Apr 5, 2021

Description

This enables the underlying Socket.SO_KEEPALIVE when the CONNECTION_KEEPALIVE configuration option is set to true.

Motivation and Context

There's a known issue when using a NAT gateway that it times out after 350 seconds if the connection is not kept alive. If using ApacheHttpClient to create connections to an AWS Lambda from a machine within the VPC it appears that this connection goes through the NAT. This means that any Lambda invocations taking more than 350 seconds will never get a response.

Testing

Deployed the code to environment that was experiencing the timeout issue, and verified that setting setSoKeepAlive(true) on the SocketConfig enabled keep-alive and allowed connections running longer than 350 seconds to respond.

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have read the CONTRIBUTING document
  • Local run of mvn install succeeds
  • My code follows the code style of this project
  • My change requires a change to the Javadoc documentation
  • I have updated the Javadoc documentation accordingly
  • I have read the README document
  • I have added tests to cover my changes
  • All new and existing tests passed
  • A short description of the change has been added to the CHANGELOG
  • My change is to implement 1.11 parity feature and I have updated LaunchChangelog

License

  • I confirm that this pull request can be released under the Apache 2 license

@ajs139 ajs139 force-pushed the fix/keep-alive branch 2 times, most recently from f8f2e17 to d01d32e Compare April 5, 2021 20:22
@zoewangg
Copy link
Contributor

zoewangg commented Apr 5, 2021

Thank you for the PR! We will take a look shortly

Copy link
Contributor

@zoewangg zoewangg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a changelog entry? You can generate one by running the scripts/new-change script and following the instructions.

/**
* Configure the connection to use java.net.SocketOptions.SO_KEEPALIVE.
*/
Builder connectionKeepalive(Boolean keepConnectionAlive);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. Can we change it to tcpKeepAlive?

@@ -675,10 +693,10 @@ public void checkServerTrusted(X509Certificate[] x509Certificates, String s) thr
};
}

private SocketConfig buildSocketConfig(AttributeMap standardOptions) {
private SocketConfig buildSocketConfig(AttributeMap standardOptions,
Boolean soKeepAlive) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get soKeepAlive from standardOptions.get(SdkHttpConfigurationOption.TCP_KEEPALIVE) directly instead of passing it?

/**
* Whether or not to use keepalive on the connection.
*/
public static final SdkHttpConfigurationOption<Boolean> CONNECTION_KEEPALIVE =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename it to TCP_KEEPALIVE so that users don't confuse it with HTTP keep alive?

@@ -116,6 +122,7 @@
private static final Duration DEFAULT_CONNECTION_TIMEOUT = Duration.ofSeconds(2);
private static final Duration DEFAULT_CONNECTION_ACQUIRE_TIMEOUT = Duration.ofSeconds(10);
private static final Duration DEFAULT_CONNECTION_MAX_IDLE_TIMEOUT = Duration.ofSeconds(60);
private static final Boolean DEFAULT_CONNECTION_KEEPALIVE = Boolean.TRUE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably make it default to false so that it has the same behavior as now.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, thought it was, not sure what happened there.

@ajs139 ajs139 force-pushed the fix/keep-alive branch 3 times, most recently from a1ffc01 to 8aa2889 Compare April 7, 2021 01:38
@ajs139
Copy link
Author

ajs139 commented Apr 7, 2021

@zoewangg - thanks for the code review, it's much cleaner now.

@codecov-io
Copy link

Codecov Report

Merging #2373 (fb10a32) into master (e5b53e1) will decrease coverage by 0.02%.
The diff coverage is 12.50%.

❗ Current head fb10a32 differs from pull request most recent head 8aa2889. Consider uploading reports for the commit 8aa2889 to get more accurate results
Impacted file tree graph

@@             Coverage Diff              @@
##             master    #2373      +/-   ##
============================================
- Coverage     77.68%   77.66%   -0.03%     
  Complexity      366      366              
============================================
  Files          1246     1245       -1     
  Lines         39358    39301      -57     
  Branches       3110     3106       -4     
============================================
- Hits          30577    30522      -55     
  Misses         7291     7291              
+ Partials       1490     1488       -2     
Flag Coverage Δ Complexity Δ
unittests 77.66% <12.50%> (-0.03%) 0.00 <0.00> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ Complexity Δ
...amazon/awssdk/http/SdkHttpConfigurationOption.java 0.00% <0.00%> (ø) 0.00 <0.00> (ø)
...re/amazon/awssdk/http/apache/ApacheHttpClient.java 75.58% <20.00%> (-1.45%) 0.00 <0.00> (ø)
...k/core/pagination/async/ResponsesSubscription.java 72.00% <0.00%> (-16.00%) 0.00% <0.00%> (ø%)
...nio/netty/internal/OldConnectionReaperHandler.java 81.81% <0.00%> (-9.10%) 0.00% <0.00%> (ø%)
...mazon/awssdk/utils/internal/MappingSubscriber.java 82.75% <0.00%> (-6.90%) 0.00% <0.00%> (ø%)
...ore/client/config/ClientOverrideConfiguration.java 77.57% <0.00%> (-1.92%) 0.00% <0.00%> (ø%)
.../awscore/client/handler/AwsClientHandlerUtils.java 90.16% <0.00%> (-0.89%) 0.00% <0.00%> (ø%)
...k/core/client/builder/SdkDefaultClientBuilder.java 90.97% <0.00%> (-0.07%) 0.00% <0.00%> (ø%)
...zon/awssdk/core/client/config/SdkClientOption.java 100.00% <0.00%> (ø) 0.00% <0.00%> (ø%)
.../core/internal/handler/BaseAsyncClientHandler.java 94.31% <0.00%> (ø) 0.00% <0.00%> (ø%)
... and 8 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e5b53e1...8aa2889. Read the comment docs.

Copy link
Contributor

@zoewangg zoewangg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating the PR! Just have one minor comment, otherwise lgtm.

return this;
}

public void setTcpKeepalive(Boolean keepConnectionAlive) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the setter also needs to be updated to setTcpKeepAlive

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed that one, thank you.

Copy link
Contributor

@zoewangg zoewangg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you for your contribution!

@sonarqubecloud
Copy link

sonarqubecloud bot commented Apr 7, 2021

@zoewangg zoewangg merged commit 71dfc2c into aws:master Apr 8, 2021
@zoewangg
Copy link
Contributor

zoewangg commented Apr 8, 2021

@all-contributors please add @ajs139 for code

@allcontributors
Copy link
Contributor

@zoewangg

I've put up a pull request to add @ajs139! 🎉

@ajs139 ajs139 deleted the fix/keep-alive branch April 8, 2021 00:21
aws-sdk-java-automation added a commit that referenced this pull request Feb 9, 2023
…bfe69efaa

Pull request: release <- staging/1611ed87-a79f-4f4f-88a8-66ebfe69efaa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants