-
Notifications
You must be signed in to change notification settings - Fork 652
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
aws: Add example for custom HTTP client idle connection options #350
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,84 @@ | ||
package aws_test | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/aws/external" | ||
) | ||
|
||
func ExampleBuildableHTTPClient_WithTransportOptions_connectionPool() { | ||
cfg, err := external.LoadDefaultAWSConfig() | ||
if err != nil { | ||
log.Fatalf("failed to load config, %v", err) | ||
} | ||
|
||
// The SDK's HTTPClient implementation implements a WithTransportOptions | ||
// method for getting an HTTP client with custom transport options. | ||
type httpClientTransportOptions interface { | ||
WithTransportOptions(...func(*http.Transport)) aws.HTTPClient | ||
} | ||
|
||
// Unless cfg.HTTPClient is set to another custom implementation by the | ||
// application the SDK will use aws.BuildableHTTPClient as the | ||
// implementation for cfg.HTTPClient. | ||
client, ok := cfg.HTTPClient.(httpClientTransportOptions) | ||
if !ok { | ||
log.Fatalf("expected http client to be SDK's default but wasn't, %T", cfg.HTTPClient) | ||
} | ||
|
||
// Get a client with custom connection pooling options. The client's | ||
// options are immutable, and return copies of the client when options are | ||
// applied. | ||
cfg.HTTPClient = client.WithTransportOptions(func(tr *http.Transport) { | ||
tr.MaxIdleConnsPerHost = 150 | ||
// Experiment with 2 * MaxIdleConnsPerHost * number of services, and | ||
// regions used. Need to balance burst concurrency, with max open | ||
// connections. May need to adjust how long idle connections are keep | ||
// around for with IdleConnTimeout. | ||
tr.MaxIdleConns = tr.MaxIdleConnsPerHost * 2 * 1 | ||
}) | ||
|
||
fmt.Printf("Have client: %T, MaxIdleConnsPerHost: %v\n", | ||
cfg.HTTPClient, | ||
cfg.HTTPClient.(*aws.BuildableHTTPClient).GetTransport().MaxIdleConnsPerHost, | ||
) | ||
|
||
// Create service API clients with cfg value to use the custom Transport options. | ||
|
||
// Output: Have client: *aws.BuildableHTTPClient, MaxIdleConnsPerHost: 150 | ||
} | ||
|
||
func ExampleBuildableHTTPClient_WithTransportOptions_responseTimeouts() { | ||
// Create a new client by calling the constructor. | ||
// Add custom *http.Transport configuration to the client. The | ||
// modifications will be on the new client returned. | ||
client := aws.NewBuildableHTTPClient(). | ||
WithTransportOptions(func(tr *http.Transport) { | ||
// Only wait 10 seconds for the full response headers to be read. | ||
tr.ResponseHeaderTimeout = 10 * time.Second | ||
}) | ||
|
||
// Set the configured HTTP client to the SDK's Config, and use the | ||
// Config to create API clients with. | ||
cfg.HTTPClient = client | ||
} | ||
|
||
func ExampleBuildableHTTPClient_WithDialerOptions() { | ||
// Create a new client by calling the constructor. | ||
client := aws.NewBuildableHTTPClient(). | ||
WithDialerOptions(func(d *net.Dialer) { | ||
// Set the network (e.g. TCP) dial timeout to 10 seconds. | ||
d.Timeout = 10 * time.Second | ||
}) | ||
|
||
// Set the configured HTTP client to the SDK's Config, and use the | ||
// Config to create API clients with. | ||
cfg.HTTPClient = client | ||
} | ||
|
||
var cfg aws.Config |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be fatal too? Since it would be a network error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in this case since the assertion failing wouldn't prevent further assertions being performed the
Errorf
is good.Fatalf
is great further assertions may not be testable.