Skip to content

Commit

Permalink
Allow more connection reuse for DynamoDB and S3 calls (grafana#2268)
Browse files Browse the repository at this point in the history
We will connect many times in parallel to the same DynamoDB server,
and with default settings Go will close and re-open connections; see
golang/go#13801

Raise MaxIdleConnsPerHost to avoid this.

Signed-off-by: Bryan Boreham <bryan@weave.works>
  • Loading branch information
bboreham authored Mar 16, 2020
1 parent bca670f commit 1c13de1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
20 changes: 20 additions & 0 deletions aws/dynamodb_storage_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"flag"
"fmt"
"net"
"net/http"
"net/url"
"strings"
"time"
Expand Down Expand Up @@ -865,5 +867,23 @@ func awsSessionFromURL(awsURL *url.URL) (client.ConfigProvider, error) {
return nil, err
}
config = config.WithMaxRetries(0) // We do our own retries, so we can monitor them
config = config.WithHTTPClient(&http.Client{Transport: defaultTransport})
return session.NewSession(config)
}

// Copy-pasted http.DefaultTransport
var defaultTransport http.RoundTripper = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
// We will connect many times in parallel to the same DynamoDB server,
// see https://github.com/golang/go/issues/13801
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
2 changes: 2 additions & 0 deletions aws/s3_storage_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"hash/fnv"
"io"
"net/http"
"strings"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -72,6 +73,7 @@ func NewS3ObjectClient(cfg S3Config) (*S3ObjectClient, error) {
s3Config = s3Config.WithS3ForcePathStyle(cfg.S3ForcePathStyle) // support for Path Style S3 url if has the flag

s3Config = s3Config.WithMaxRetries(0) // We do our own retries, so we can monitor them
s3Config = s3Config.WithHTTPClient(&http.Client{Transport: defaultTransport})
sess, err := session.NewSession(s3Config)
if err != nil {
return nil, err
Expand Down

0 comments on commit 1c13de1

Please sign in to comment.