Skip to content

Commit

Permalink
Add disable_http2 field for HTTP client components (#169)
Browse files Browse the repository at this point in the history
Fixes #117

Signed-off-by: Mihai Todor <[email protected]>
  • Loading branch information
mihaitodor authored Jan 29, 2025
1 parent 9211500 commit 63e911b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file.

- A `crash` processor for FATAL logging. (@rockwotj)
- A `uuid_v7` bloblang function. (@rockwotj)
- Field `disable_http2` added to the `http_client` input and output and to the `http` processor. (@mihaitodor)

## 4.43.0 - 2025-01-13

Expand Down
7 changes: 7 additions & 0 deletions internal/httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package httpclient
import (
"bytes"
"context"
"crypto/tls"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -120,6 +121,12 @@ func NewClientFromOldConfig(conf OldConfig, mgr *service.Resources, opts ...Requ
return nil, fmt.Errorf("failed to config logger for request dump: %v", err)
}

if conf.DisableHTTP2 {
if c, ok := h.client.Transport.(*http.Transport); ok {
c.TLSNextProto = map[string]func(string, *tls.Conn) http.RoundTripper{}
}
}

h.client = conf.clientCtor(h.clientCtx, h.client)

for _, c := range conf.BackoffOn {
Expand Down
56 changes: 56 additions & 0 deletions internal/httpclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,3 +828,59 @@ extract_headers:
require.True(t, ok)
require.Equal(t, "https://example.com", location)
}

func TestHTTPClientDisableHTTP2(t *testing.T) {
tests := []struct {
name string
disableHTTP2 bool
expProtocol string
}{
{
name: "http2 enabled",
expProtocol: "HTTP/2.0",
},
{
name: "http2 disabled",
disableHTTP2: true,
expProtocol: "HTTP/1.1",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, test.expProtocol, r.Proto)
b, err := io.ReadAll(r.Body)
require.NoError(t, err)
_, _ = w.Write(b)
}))
defer ts.Close()

ts.EnableHTTP2 = true

ts.StartTLS()

conf := clientConfig(t, `
url: %s
disable_http2: %t
tls:
enabled: true
skip_cert_verify: true
`, ts.URL, test.disableHTTP2)

h, err := NewClientFromOldConfig(conf, service.MockResources())
require.NoError(t, err)

dummyMsg := "hello world"
resBatch, err := h.Send(context.Background(), service.MessageBatch{
service.NewMessage([]byte(dummyMsg)),
})
require.NoError(t, err)
require.Len(t, resBatch, 1)

mBytes, err := resBatch[0].AsBytes()
require.NoError(t, err)
assert.Equal(t, dummyMsg, string(mBytes))
})
}
}
10 changes: 10 additions & 0 deletions internal/httpclient/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
hcFieldDumpRequestLogLevel = "dump_request_log_level"
hcFieldTLS = "tls"
hcFieldProxyURL = "proxy_url"
hcFieldDisableHTTP2 = "disable_http2"
)

// ConfigField returns a public API config field spec for an HTTP component,
Expand Down Expand Up @@ -108,6 +109,11 @@ func ConfigField(defaultVerb string, forOutput bool, extraChildren ...*service.C
Description("An optional HTTP proxy URL.").
Advanced().
Optional(),
service.NewBoolField(hcFieldDisableHTTP2).
Description("Whether or not to disable disable HTTP/2").
Advanced().
Default(false).
Version("4.44.0"),
)

innerFields = append(innerFields, extraChildren...)
Expand Down Expand Up @@ -167,6 +173,9 @@ func ConfigFromParsed(pConf *service.ParsedConfig) (conf OldConfig, err error) {
if conf.authSigner, err = pConf.HTTPRequestAuthSignerFromParsed(); err != nil {
return
}
if conf.DisableHTTP2, err = pConf.FieldBool(hcFieldDisableHTTP2); err != nil {
return
}
if conf.clientCtor, err = oauth2ClientCtorFromParsed(pConf); err != nil {
return
}
Expand All @@ -193,6 +202,7 @@ type OldConfig struct {
TLSEnabled bool
TLSConf *tls.Config
ProxyURL string
DisableHTTP2 bool
authSigner func(f fs.FS, req *http.Request) error
clientCtor func(context.Context, *http.Client) *http.Client
}

0 comments on commit 63e911b

Please sign in to comment.