Skip to content

Commit 5d7c42a

Browse files
authored
chore: configure timeouts for HTTP/2 (#8388)
And add a unit test for the default transport.
1 parent a9d540a commit 5d7c42a

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

pkg/azclient/utils/transport.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ import (
2222
"net/http"
2323
"sync"
2424
"time"
25+
26+
"golang.org/x/net/http2"
2527
)
2628

29+
// DefaultTransport is the default transport used by the Azure SDK for Go.
2730
var DefaultTransport *http.Transport
2831
var once sync.Once
2932

@@ -44,5 +47,11 @@ func init() {
4447
MinVersion: tls.VersionTLS12,
4548
},
4649
}
50+
51+
// Configure HTTP/2
52+
if http2Transport, err := http2.ConfigureTransports(DefaultTransport); err == nil {
53+
http2Transport.ReadIdleTimeout = 30 * time.Second
54+
http2Transport.PingTimeout = 15 * time.Second
55+
}
4756
})
4857
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package utils
18+
19+
import (
20+
"crypto/tls"
21+
"testing"
22+
"time"
23+
)
24+
25+
func TestDefaultTransport(t *testing.T) {
26+
if DefaultTransport == nil {
27+
t.Fatal("DefaultTransport is nil")
28+
}
29+
30+
if DefaultTransport.DialContext == nil {
31+
t.Error("DialContext is nil")
32+
}
33+
34+
if !DefaultTransport.ForceAttemptHTTP2 {
35+
t.Error("ForceAttemptHTTP2 is false")
36+
}
37+
38+
if DefaultTransport.MaxIdleConns != 100 {
39+
t.Errorf("Expected MaxIdleConns to be 100, got %d", DefaultTransport.MaxIdleConns)
40+
}
41+
42+
if DefaultTransport.MaxConnsPerHost != 100 {
43+
t.Errorf("Expected MaxConnsPerHost to be 100, got %d", DefaultTransport.MaxConnsPerHost)
44+
}
45+
46+
if DefaultTransport.IdleConnTimeout != 90*time.Second {
47+
t.Errorf("Expected IdleConnTimeout to be 90s, got %v", DefaultTransport.IdleConnTimeout)
48+
}
49+
50+
if DefaultTransport.TLSHandshakeTimeout != 10*time.Second {
51+
t.Errorf("Expected TLSHandshakeTimeout to be 10s, got %v", DefaultTransport.TLSHandshakeTimeout)
52+
}
53+
54+
if DefaultTransport.ExpectContinueTimeout != 1*time.Second {
55+
t.Errorf("Expected ExpectContinueTimeout to be 1s, got %v", DefaultTransport.ExpectContinueTimeout)
56+
}
57+
58+
if DefaultTransport.ResponseHeaderTimeout != 60*time.Second {
59+
t.Errorf("Expected ResponseHeaderTimeout to be 60s, got %v", DefaultTransport.ResponseHeaderTimeout)
60+
}
61+
62+
if DefaultTransport.TLSClientConfig == nil {
63+
t.Fatal("TLSClientConfig is nil")
64+
}
65+
66+
if DefaultTransport.TLSClientConfig.MinVersion != tls.VersionTLS12 {
67+
t.Errorf("Expected MinVersion to be TLS1.2, got %v", DefaultTransport.TLSClientConfig.MinVersion)
68+
}
69+
70+
if DefaultTransport.TLSClientConfig.Renegotiation != tls.RenegotiateNever {
71+
t.Errorf("Expected Renegotiation to be RenegotiateNever, got %v", DefaultTransport.TLSClientConfig.Renegotiation)
72+
}
73+
74+
// NOTE: http2 transport settings are not exposed hence testing is skipped.
75+
}

0 commit comments

Comments
 (0)