|
| 1 | +package aws_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "net" |
| 7 | + "net/http" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 11 | + "github.com/aws/aws-sdk-go-v2/aws/external" |
| 12 | +) |
| 13 | + |
| 14 | +func ExampleBuildableHTTPClient_WithTransportOptions_connectionPool() { |
| 15 | + cfg, err := external.LoadDefaultAWSConfig() |
| 16 | + if err != nil { |
| 17 | + log.Fatalf("failed to load config, %v", err) |
| 18 | + } |
| 19 | + |
| 20 | + // The SDK's HTTPClient implementation implements a WithTransportOptions |
| 21 | + // method for getting an HTTP client with custom transport options. |
| 22 | + type httpClientTransportOptions interface { |
| 23 | + WithTransportOptions(...func(*http.Transport)) aws.HTTPClient |
| 24 | + } |
| 25 | + |
| 26 | + // Unless cfg.HTTPClient is set to another custom implementation by the |
| 27 | + // application the SDK will use aws.BuildableHTTPClient as the |
| 28 | + // implementation for cfg.HTTPClient. |
| 29 | + client, ok := cfg.HTTPClient.(httpClientTransportOptions) |
| 30 | + if !ok { |
| 31 | + log.Fatalf("expected http client to be SDK's default but wasn't, %T", cfg.HTTPClient) |
| 32 | + } |
| 33 | + |
| 34 | + // Get a client with custom connection pooling options. The client's |
| 35 | + // options are immutable, and return copies of the client when options are |
| 36 | + // applied. |
| 37 | + cfg.HTTPClient = client.WithTransportOptions(func(tr *http.Transport) { |
| 38 | + tr.MaxIdleConnsPerHost = 150 |
| 39 | + // Experiment with 2 * MaxIdleConnsPerHost * number of services, and |
| 40 | + // regions used. Need to balance burst concurrency, with max open |
| 41 | + // connections. May need to adjust how long idle connections are keep |
| 42 | + // around for with IdleConnTimeout. |
| 43 | + tr.MaxIdleConns = tr.MaxIdleConnsPerHost * 2 * 1 |
| 44 | + }) |
| 45 | + |
| 46 | + fmt.Printf("Have client: %T, MaxIdleConnsPerHost: %v\n", |
| 47 | + cfg.HTTPClient, |
| 48 | + cfg.HTTPClient.(*aws.BuildableHTTPClient).GetTransport().MaxIdleConnsPerHost, |
| 49 | + ) |
| 50 | + |
| 51 | + // Create service API clients with cfg value to use the custom Transport options. |
| 52 | + |
| 53 | + // Output: Have client: *aws.BuildableHTTPClient, MaxIdleConnsPerHost: 150 |
| 54 | +} |
| 55 | + |
| 56 | +func ExampleBuildableHTTPClient_WithTransportOptions_responseTimeouts() { |
| 57 | + // Create a new client by calling the constructor. |
| 58 | + // Add custom *http.Transport configuration to the client. The |
| 59 | + // modifications will be on the new client returned. |
| 60 | + client := aws.NewBuildableHTTPClient(). |
| 61 | + WithTransportOptions(func(tr *http.Transport) { |
| 62 | + // Only wait 10 seconds for the full response headers to be read. |
| 63 | + tr.ResponseHeaderTimeout = 10 * time.Second |
| 64 | + }) |
| 65 | + |
| 66 | + // Set the configured HTTP client to the SDK's Config, and use the |
| 67 | + // Config to create API clients with. |
| 68 | + cfg.HTTPClient = client |
| 69 | +} |
| 70 | + |
| 71 | +func ExampleBuildableHTTPClient_WithDialerOptions() { |
| 72 | + // Create a new client by calling the constructor. |
| 73 | + client := aws.NewBuildableHTTPClient(). |
| 74 | + WithDialerOptions(func(d *net.Dialer) { |
| 75 | + // Set the network (e.g. TCP) dial timeout to 10 seconds. |
| 76 | + d.Timeout = 10 * time.Second |
| 77 | + }) |
| 78 | + |
| 79 | + // Set the configured HTTP client to the SDK's Config, and use the |
| 80 | + // Config to create API clients with. |
| 81 | + cfg.HTTPClient = client |
| 82 | +} |
| 83 | + |
| 84 | +var cfg aws.Config |
0 commit comments