debian-mirror-gitlab/workhorse/internal/upstream/roundtripper/roundtripper.go

68 lines
2.1 KiB
Go
Raw Normal View History

2021-02-22 17:27:13 +05:30
package roundtripper
import (
"context"
2021-11-11 11:23:49 +05:30
"crypto/tls"
2021-02-22 17:27:13 +05:30
"fmt"
"net"
"net/http"
"net/url"
"time"
"gitlab.com/gitlab-org/labkit/correlation"
"gitlab.com/gitlab-org/labkit/tracing"
2021-10-27 15:23:28 +05:30
"gitlab.com/gitlab-org/gitlab/workhorse/internal/badgateway"
2021-02-22 17:27:13 +05:30
)
func mustParseAddress(address, scheme string) string {
for _, suffix := range []string{"", ":" + scheme} {
address += suffix
if host, port, err := net.SplitHostPort(address); err == nil && host != "" && port != "" {
return host + ":" + port
}
}
panic(fmt.Errorf("could not parse host:port from address %q and scheme %q", address, scheme))
}
// NewBackendRoundTripper returns a new RoundTripper instance using the provided values
func NewBackendRoundTripper(backend *url.URL, socket string, proxyHeadersTimeout time.Duration, developmentMode bool) http.RoundTripper {
2021-11-11 11:23:49 +05:30
return newBackendRoundTripper(backend, socket, proxyHeadersTimeout, developmentMode, nil)
}
func newBackendRoundTripper(backend *url.URL, socket string, proxyHeadersTimeout time.Duration, developmentMode bool, tlsConf *tls.Config) http.RoundTripper {
2021-12-11 22:18:48 +05:30
transport := http.DefaultTransport.(*http.Transport).Clone()
2021-02-22 17:27:13 +05:30
transport.ResponseHeaderTimeout = proxyHeadersTimeout
2021-11-11 11:23:49 +05:30
transport.TLSClientConfig = tlsConf
2021-02-22 17:27:13 +05:30
2021-12-11 22:18:48 +05:30
// Puma does not support http/2, there's no point in reconnecting
transport.ForceAttemptHTTP2 = false
dial := transport.DialContext
2021-02-22 17:27:13 +05:30
if backend != nil && socket == "" {
address := mustParseAddress(backend.Host, backend.Scheme)
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
2021-12-11 22:18:48 +05:30
return dial(ctx, "tcp", address)
2021-02-22 17:27:13 +05:30
}
} else if socket != "" {
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
2021-12-11 22:18:48 +05:30
return dial(ctx, "unix", socket)
2021-02-22 17:27:13 +05:30
}
} else {
panic("backend is nil and socket is empty")
}
return tracing.NewRoundTripper(
correlation.NewInstrumentedRoundTripper(
badgateway.NewRoundTripper(developmentMode, transport),
),
)
}
// NewTestBackendRoundTripper sets up a RoundTripper for testing purposes
func NewTestBackendRoundTripper(backend *url.URL) http.RoundTripper {
return NewBackendRoundTripper(backend, "", 0, true)
}