Skip to content

Commit

Permalink
h2c client
Browse files Browse the repository at this point in the history
  • Loading branch information
jayanth-tatina-groww committed Nov 25, 2024
1 parent 0b75ada commit 89c6aab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 50 deletions.
49 changes: 0 additions & 49 deletions transport/http/client/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{}
28 changes: 27 additions & 1 deletion transport/http/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down

0 comments on commit 89c6aab

Please sign in to comment.