Skip to content

Commit 30227e2

Browse files
authored
aws: Add example for custom HTTP client idle connection options (#350)
Adds example to the SDK for configuring custom HTTP client idle connection keep alive options.
1 parent 5208fda commit 30227e2

File tree

2 files changed

+84
-32
lines changed

2 files changed

+84
-32
lines changed

aws/http_client_example_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

aws/http_client_test.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package aws_test
22

33
import (
4-
"net"
54
"net/http"
65
"net/http/httptest"
76
"testing"
8-
"time"
97

108
"github.com/aws/aws-sdk-go-v2/aws"
119
)
@@ -29,33 +27,3 @@ func TestBuildableHTTPClient_NoFollowRedirect(t *testing.T) {
2927
t.Errorf("expect %v code, got %v", e, a)
3028
}
3129
}
32-
33-
func ExampleBuildableHTTPClient_WithTransportOptions() {
34-
// Create a new client by calling the constructor.
35-
// Add custom *http.Transport configuration to the client. The
36-
// modifications will be on the new client returned.
37-
client := aws.NewBuildableHTTPClient().
38-
WithTransportOptions(func(tr *http.Transport) {
39-
// Only wait 10 seconds for the full response headers to be read.
40-
tr.ResponseHeaderTimeout = 10 * time.Second
41-
})
42-
43-
// Set the configured HTTP client to the SDK's Config, and use the
44-
// Config to create API clients with.
45-
cfg.HTTPClient = client
46-
}
47-
48-
func ExampleBuildableHTTPClient_WithDialerOptions() {
49-
// Create a new client by calling the constructor.
50-
client := aws.NewBuildableHTTPClient().
51-
WithDialerOptions(func(d *net.Dialer) {
52-
// Set the network (e.g. TCP) dial timeout to 10 seconds.
53-
d.Timeout = 10 * time.Second
54-
})
55-
56-
// Set the configured HTTP client to the SDK's Config, and use the
57-
// Config to create API clients with.
58-
cfg.HTTPClient = client
59-
}
60-
61-
var cfg aws.Config

0 commit comments

Comments
 (0)