diff --git a/weed/command/scaffold/master.toml b/weed/command/scaffold/master.toml index cfca8826fc..edc4ea5694 100644 --- a/weed/command/scaffold/master.toml +++ b/weed/command/scaffold/master.toml @@ -45,7 +45,8 @@ bucket = "your_bucket_name" # an existing bucket endpoint = "" storage_class = "STANDARD_IA" # optional HTTP client tuning for S3-compatible services: -# disable_http2 = false # true keeps the client on HTTP/1.1, one connection per concurrent request +# disable_http2 = false # true keeps the client on HTTP/1.1, one connection per concurrent request, +# # with a 30s response header timeout and 5 retries # max_connections = 0 # cap on connections to the endpoint, 0 = no cap # idle_connection_timeout_seconds = 0 # close idle connections sooner, 0 = Go default (90s) diff --git a/weed/storage/backend/s3_backend/s3_sessions.go b/weed/storage/backend/s3_backend/s3_sessions.go index bf9f3af5f0..9e6310314e 100644 --- a/weed/storage/backend/s3_backend/s3_sessions.go +++ b/weed/storage/backend/s3_backend/s3_sessions.go @@ -43,13 +43,27 @@ type httpOptions struct { idleConnTimeout time.Duration } +// Over HTTP/1.1 every concurrent request needs a connection of its own, so +// every client keeps more idle connections than Go's default of 2 per host; +// HTTP/2 holds one connection and is not affected. With disable_http2 the +// client also gets a shorter bound on the wait for response headers, and more +// retries with back-off for 5xx, 503 SlowDown and throttling. +const ( + http1MaxIdleConns = 100 + http1ResponseHeaderTimeout = 30 * time.Second + http1MaxRetries = 5 +) + func (o httpOptions) newTransport() *http.Transport { t := http.DefaultTransport.(*http.Transport).Clone() t.DialContext = (&net.Dialer{Timeout: s3ConnectTimeout, KeepAlive: 30 * time.Second}).DialContext t.ResponseHeaderTimeout = s3ReadTimeout + t.MaxIdleConns = http1MaxIdleConns + t.MaxIdleConnsPerHost = http1MaxIdleConns if o.disableHTTP2 { t.ForceAttemptHTTP2 = false t.TLSNextProto = map[string]func(string, *tls.Conn) http.RoundTripper{} + t.ResponseHeaderTimeout = http1ResponseHeaderTimeout } if o.maxConnsPerHost > 0 { t.MaxConnsPerHost = o.maxConnsPerHost @@ -80,6 +94,9 @@ func createSession(awsAccessKeyId, awsSecretAccessKey, region, endpoint string, S3DisableContentMD5Validation: aws.Bool(true), HTTPClient: &http.Client{Transport: httpOpts.newTransport()}, } + if httpOpts.disableHTTP2 { + config.MaxRetries = aws.Int(http1MaxRetries) + } if awsAccessKeyId != "" && awsSecretAccessKey != "" { config.Credentials = credentials.NewStaticCredentials(awsAccessKeyId, awsSecretAccessKey, "") } diff --git a/weed/storage/backend/s3_backend/s3_sessions_test.go b/weed/storage/backend/s3_backend/s3_sessions_test.go index eb5699259c..454b3d4423 100644 --- a/weed/storage/backend/s3_backend/s3_sessions_test.go +++ b/weed/storage/backend/s3_backend/s3_sessions_test.go @@ -218,3 +218,43 @@ func TestS3BackendIdleTimeoutOverflowFallsBackToDefault(t *testing.T) { } } } + +// With disable_http2 each request needs its own connection, so the client +// gets a 30 s bound on response headers and 5 retries. The idle pool of 100 +// applies to every client: a plain http:// endpoint is HTTP/1.1 without the flag. +func TestS3BackendHTTP1ClientDefaults(t *testing.T) { + s, err := newS3BackendStorage(testProperties{"endpoint": "http://127.0.0.1:11", "disable_http2": "true"}, "", "http1-defaults") + if err != nil { + t.Fatal(err) + } + tr := backendTransport(t, s) + if tr == nil { + t.Fatal("disable_http2 set, yet the client has no transport of its own") + } + if tr.MaxIdleConns != http1MaxIdleConns || tr.MaxIdleConnsPerHost != http1MaxIdleConns { + t.Errorf("idle pool %d/%d per host, want %d/%d", tr.MaxIdleConns, tr.MaxIdleConnsPerHost, http1MaxIdleConns, http1MaxIdleConns) + } + if tr.ResponseHeaderTimeout != http1ResponseHeaderTimeout { + t.Errorf("response header timeout %v, want %v", tr.ResponseHeaderTimeout, http1ResponseHeaderTimeout) + } + if got := s.conn.(*s3.S3).Client.Config.MaxRetries; got == nil || *got != http1MaxRetries { + t.Errorf("MaxRetries %v, want %d", got, http1MaxRetries) + } + + // without disable_http2 the client keeps the SDK's retries and the + // s3ReadTimeout header bound, but still gets the idle pool + plain, err := newS3BackendStorage(testProperties{"endpoint": "http://127.0.0.1:12", "max_connections": "8"}, "", "http1-plain") + if err != nil { + t.Fatal(err) + } + if got := plain.conn.(*s3.S3).Client.Config.MaxRetries; got != nil && *got == http1MaxRetries { + t.Error("MaxRetries raised without disable_http2") + } + tr = backendTransport(t, plain) + if tr == nil || tr.ResponseHeaderTimeout != s3ReadTimeout { + t.Error("disable_http2 header bound applied to a client without disable_http2") + } + if tr != nil && (tr.MaxIdleConns != http1MaxIdleConns || tr.MaxIdleConnsPerHost != http1MaxIdleConns) { + t.Errorf("idle pool %d/%d per host without disable_http2, want %d/%d", tr.MaxIdleConns, tr.MaxIdleConnsPerHost, http1MaxIdleConns, http1MaxIdleConns) + } +}