Skip to content

Commit cdc6dff

Browse files
authored
[confighttp] - Expose an option to set ForceAttemptHttp2 setting (#13426)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description We make use golang's `DefaultTransport`, which forces the use of [HTTP/2 by default](https://cs.opensource.google/go/go/+/refs/tags/go1.24.5:src/net/http/transport.go;l=51). However, some of our settings are only applicable while using HTTP/1 transport (`MaxConnsPerHost`, `MaxIdleConnsPerHost` and `MaxIdleConns`). Users wanting to force such settings should have an option to use HTTP/1 by disabling `ForceAttemptHTTP2` to `false`. This PR adds that option. By default, `ForceAttemptHTTP2` is set to `true` for backward compatibility.
1 parent fea16eb commit cdc6dff

6 files changed

Lines changed: 66 additions & 8 deletions

File tree

.chloggen/http2.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: confighttp
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add option to configure ForceAttemptHTTP2 to support HTTP/1 specific transport settings.
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [13426]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: []

config/confighttp/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ README](../configtls/README.md).
5656
- [`idle_conn_timeout`](https://golang.org/pkg/net/http/#Transport)
5757
- [`auth`](../configauth/README.md)
5858
- [`disable_keep_alives`](https://golang.org/pkg/net/http/#Transport)
59+
- [`force_attempt_http2`](https://golang.org/pkg/net/http/#Transport)
5960
- [`http2_read_idle_timeout`](https://pkg.go.dev/golang.org/x/net/http2#Transport)
6061
- [`http2_ping_timeout`](https://pkg.go.dev/golang.org/x/net/http2#Transport)
6162
- [`cookies`](https://pkg.go.dev/net/http#CookieJar)

config/confighttp/confighttp.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ type ClientConfig struct {
119119
// Cookies configures the cookie management of the HTTP client.
120120
Cookies CookiesConfig `mapstructure:"cookies,omitempty"`
121121

122+
// Enabling ForceAttemptHTTP2 forces the HTTP transport to use the HTTP/2 protocol.
123+
// By default, this is set to true.
124+
// NOTE: HTTP/2 does not support settings such as MaxConnsPerHost, MaxIdleConnsPerHost and MaxIdleConns.
125+
ForceAttemptHTTP2 bool `mapstructure:"force_attempt_http2,omitempty"`
126+
122127
// Middlewares are used to add custom functionality to the HTTP client.
123128
// Middleware handlers are called in the order they appear in this list,
124129
// with the first middleware becoming the outermost handler.
@@ -141,9 +146,10 @@ func NewDefaultClientConfig() ClientConfig {
141146
defaultTransport := http.DefaultTransport.(*http.Transport)
142147

143148
return ClientConfig{
144-
Headers: map[string]configopaque.String{},
145-
MaxIdleConns: defaultTransport.MaxIdleConns,
146-
IdleConnTimeout: defaultTransport.IdleConnTimeout,
149+
Headers: map[string]configopaque.String{},
150+
MaxIdleConns: defaultTransport.MaxIdleConns,
151+
IdleConnTimeout: defaultTransport.IdleConnTimeout,
152+
ForceAttemptHTTP2: true,
147153
}
148154
}
149155

@@ -184,6 +190,7 @@ func (hcs *ClientConfig) ToClient(ctx context.Context, host component.Host, sett
184190
transport.MaxIdleConnsPerHost = hcs.MaxIdleConnsPerHost
185191
transport.MaxConnsPerHost = hcs.MaxConnsPerHost
186192
transport.IdleConnTimeout = hcs.IdleConnTimeout
193+
transport.ForceAttemptHTTP2 = hcs.ForceAttemptHTTP2
187194

188195
// Setting the Proxy URL
189196
if hcs.ProxyURL != "" {

config/confighttp/confighttp_test.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,28 @@ func TestAllHTTPClientSettings(t *testing.T) {
101101
},
102102
shouldError: false,
103103
},
104+
{
105+
name: "all_valid_settings_http2_enabled",
106+
settings: ClientConfig{
107+
Endpoint: "localhost:1234",
108+
TLS: configtls.ClientConfig{
109+
Insecure: false,
110+
},
111+
ReadBufferSize: 1024,
112+
WriteBufferSize: 512,
113+
MaxIdleConns: maxIdleConns,
114+
MaxIdleConnsPerHost: maxIdleConnsPerHost,
115+
MaxConnsPerHost: maxConnsPerHost,
116+
ForceAttemptHTTP2: true,
117+
IdleConnTimeout: idleConnTimeout,
118+
Compression: "",
119+
DisableKeepAlives: true,
120+
Cookies: CookiesConfig{Enabled: true},
121+
HTTP2ReadIdleTimeout: idleConnTimeout,
122+
HTTP2PingTimeout: http2PingTimeout,
123+
},
124+
shouldError: false,
125+
},
104126
{
105127
name: "all_valid_settings_with_none_compression",
106128
settings: ClientConfig{
@@ -699,8 +721,9 @@ func TestHttpReception(t *testing.T) {
699721
}
700722

701723
hcs := &ClientConfig{
702-
Endpoint: prefix + ln.Addr().String(),
703-
TLS: *tt.tlsClientCreds,
724+
Endpoint: prefix + ln.Addr().String(),
725+
TLS: *tt.tlsClientCreds,
726+
ForceAttemptHTTP2: true,
704727
}
705728

706729
client, errClient := hcs.ToClient(context.Background(), componenttest.NewNopHost(), nilProvidersSettings)

exporter/otlphttpexporter/config_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func TestUnmarshalConfig(t *testing.T) {
8282
MaxIdleConnsPerHost: defaultMaxIdleConnsPerHost,
8383
MaxConnsPerHost: defaultMaxConnsPerHost,
8484
IdleConnTimeout: defaultIdleConnTimeout,
85+
ForceAttemptHTTP2: true,
8586
},
8687
}, cfg)
8788
}

internal/e2e/confighttp_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ func TestConfmapMarshalConfigHTTP(t *testing.T) {
1818
conf := confmap.New()
1919
require.NoError(t, conf.Marshal(confighttp.NewDefaultClientConfig()))
2020
assert.Equal(t, map[string]any{
21-
"headers": map[string]any{},
22-
"idle_conn_timeout": 90 * time.Second,
23-
"max_idle_conns": 100,
21+
"headers": map[string]any{},
22+
"idle_conn_timeout": 90 * time.Second,
23+
"max_idle_conns": 100,
24+
"force_attempt_http2": true,
2425
}, conf.ToStringMap())
2526

2627
conf = confmap.New()

0 commit comments

Comments
 (0)