Skip to content

Commit 2c91535

Browse files
authored
Merge pull request kubernetes#129538 from hzxuzhonghu/http2-clean
Cleanup: only initiate http2 server options when http2 is not disabled
2 parents 8b1fe81 + a2a0a75 commit 2c91535

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

staging/src/k8s.io/apiserver/pkg/server/secure_serving.go

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -172,33 +172,31 @@ func (s *SecureServingInfo) Serve(handler http.Handler, shutdownTimeout time.Dur
172172
ReadHeaderTimeout: 32 * time.Second, // just shy of requestTimeoutUpperBound
173173
}
174174

175-
// At least 99% of serialized resources in surveyed clusters were smaller than 256kb.
176-
// This should be big enough to accommodate most API POST requests in a single frame,
177-
// and small enough to allow a per connection buffer of this size multiplied by `MaxConcurrentStreams`.
178-
const resourceBody99Percentile = 256 * 1024
179-
180-
http2Options := &http2.Server{
181-
IdleTimeout: 90 * time.Second, // matches http.DefaultTransport keep-alive timeout
182-
}
183-
184-
// shrink the per-stream buffer and max framesize from the 1MB default while still accommodating most API POST requests in a single frame
185-
http2Options.MaxUploadBufferPerStream = resourceBody99Percentile
186-
http2Options.MaxReadFrameSize = resourceBody99Percentile
187-
188-
// use the overridden concurrent streams setting or make the default of 250 explicit so we can size MaxUploadBufferPerConnection appropriately
189-
if s.HTTP2MaxStreamsPerConnection > 0 {
190-
http2Options.MaxConcurrentStreams = uint32(s.HTTP2MaxStreamsPerConnection)
191-
} else {
192-
// match http2.initialMaxConcurrentStreams used by clients
193-
// this makes it so that a malicious client can only open 400 streams before we forcibly close the connection
194-
// https://github.com/golang/net/commit/b225e7ca6dde1ef5a5ae5ce922861bda011cfabd
195-
http2Options.MaxConcurrentStreams = 100
196-
}
175+
if !s.DisableHTTP2 {
176+
// At least 99% of serialized resources in surveyed clusters were smaller than 256kb.
177+
// This should be big enough to accommodate most API POST requests in a single frame,
178+
// and small enough to allow a per connection buffer of this size multiplied by `MaxConcurrentStreams`.
179+
const resourceBody99Percentile = 256 * 1024
180+
181+
http2Options := &http2.Server{
182+
IdleTimeout: 90 * time.Second, // matches http.DefaultTransport keep-alive timeout
183+
// shrink the per-stream buffer and max framesize from the 1MB default while still accommodating most API POST requests in a single frame
184+
MaxUploadBufferPerStream: resourceBody99Percentile,
185+
MaxReadFrameSize: resourceBody99Percentile,
186+
}
197187

198-
// increase the connection buffer size from the 1MB default to handle the specified number of concurrent streams
199-
http2Options.MaxUploadBufferPerConnection = http2Options.MaxUploadBufferPerStream * int32(http2Options.MaxConcurrentStreams)
188+
// use the overridden concurrent streams setting or make the default of 250 explicit so we can size MaxUploadBufferPerConnection appropriately
189+
if s.HTTP2MaxStreamsPerConnection > 0 {
190+
http2Options.MaxConcurrentStreams = uint32(s.HTTP2MaxStreamsPerConnection)
191+
} else {
192+
// match http2.initialMaxConcurrentStreams used by clients
193+
// this makes it so that a malicious client can only open 400 streams before we forcibly close the connection
194+
// https://github.com/golang/net/commit/b225e7ca6dde1ef5a5ae5ce922861bda011cfabd
195+
http2Options.MaxConcurrentStreams = 100
196+
}
200197

201-
if !s.DisableHTTP2 {
198+
// increase the connection buffer size from the 1MB default to handle the specified number of concurrent streams
199+
http2Options.MaxUploadBufferPerConnection = http2Options.MaxUploadBufferPerStream * int32(http2Options.MaxConcurrentStreams)
202200
// apply settings to the server
203201
if err := http2.ConfigureServer(secureServer, http2Options); err != nil {
204202
return nil, nil, fmt.Errorf("error configuring http2: %v", err)

0 commit comments

Comments
 (0)