diff --git a/transport/http/client/executor.go b/transport/http/client/executor.go index e099bff..3ddcb8b 100644 --- a/transport/http/client/executor.go +++ b/transport/http/client/executor.go @@ -7,12 +7,7 @@ package client import ( "context" - "crypto/tls" - "net" "net/http" - "time" - - "golang.org/x/net/http2" ) // HTTPRequestExecutor defines the interface of the request executor for the HTTP transport protocol @@ -31,48 +26,4 @@ type HTTPClientFactory func(ctx context.Context) *http.Client // NewHTTPClient just returns the http default client func NewHTTPClient(_ context.Context) *http.Client { return defaultHTTPClient } -//This should be only called once on init and use the client for all requests -func NewHTTP2Client(_ context.Context) *http.Client{ - //default transport settings from http.DefaultTransport - transport := &http.Transport{ - DialContext: defaultTransportDialContext(&net.Dialer{ - Timeout: 30 * time.Second, - KeepAlive: 30 * time.Second, - }), - ForceAttemptHTTP2: false, - MaxIdleConns: 100, - IdleConnTimeout: 90 * time.Second, - TLSHandshakeTimeout: 10 * time.Second, - ExpectContinueTimeout: 1 * time.Second, - } - //converts h2c host to http - transportH2C := &h2cTransportWrapper{ - Transport: &http2.Transport{ - DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) { - return net.Dial(network, addr) - }, - AllowHTTP: true, - }, - } - //only use http2 transport when host is h2c - transport.RegisterProtocol("h2c", transportH2C) - return &http.Client{ - Transport: transport, - } -} - -func defaultTransportDialContext(dialer *net.Dialer) func(context.Context, string, string) (net.Conn, error) { - return dialer.DialContext -} - - -type h2cTransportWrapper struct { - *http2.Transport -} - -func (t *h2cTransportWrapper) RoundTrip(req *http.Request) (*http.Response, error) { - req.URL.Scheme = "http" - return t.Transport.RoundTrip(req) -} - var defaultHTTPClient = &http.Client{} diff --git a/transport/http/server/server.go b/transport/http/server/server.go index 9d62812..0bfa552 100644 --- a/transport/http/server/server.go +++ b/transport/http/server/server.go @@ -80,7 +80,7 @@ func InitHTTPDefaultTransportWithLogger(cfg config.ServiceConfig, logger logging } func newTransport(cfg config.ServiceConfig, logger logging.Logger) *http.Transport { - return &http.Transport{ + transport := &http.Transport{ Proxy: http.ProxyFromEnvironment, DialContext: (&net.Dialer{ Timeout: cfg.DialerTimeout, @@ -98,6 +98,32 @@ func newTransport(cfg config.ServiceConfig, logger logging.Logger) *http.Transpo TLSHandshakeTimeout: 10 * time.Second, TLSClientConfig: ParseClientTLSConfigWithLogger(cfg.ClientTLS, logger), } + if cfg.UseH2C { + transport.RegisterProtocol("h2c", newh2cTransport(cfg)) + } + return transport +} + +func newh2cTransport(cfg config.ServiceConfig) http.RoundTripper { + return &h2cTransportWrapper{&http2.Transport{ + DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) { + return net.Dial(network, addr) + }, + DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) { + return net.Dial(network, addr) + }, + AllowHTTP: true, + IdleConnTimeout: cfg.IdleConnTimeout, + }} +} + +type h2cTransportWrapper struct { + *http2.Transport +} + +func (t *h2cTransportWrapper) RoundTrip(req *http.Request) (*http.Response, error) { + req.URL.Scheme = "http" + return t.Transport.RoundTrip(req) } // RunServer runs a http.Server with the given handler and configuration.