knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/network/transports.go (about)

     1  /*
     2  Copyright 2019 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package network
    18  
    19  import (
    20  	"context"
    21  	"crypto/tls"
    22  	"errors"
    23  	"fmt"
    24  	"net"
    25  	"net/http"
    26  	"time"
    27  
    28  	"k8s.io/apimachinery/pkg/util/wait"
    29  )
    30  
    31  // RoundTripperFunc implementation roundtrips a request.
    32  type RoundTripperFunc func(*http.Request) (*http.Response, error)
    33  
    34  // RoundTrip implements http.RoundTripper.
    35  func (rt RoundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
    36  	return rt(r)
    37  }
    38  
    39  func newAutoTransport(v1, v2 http.RoundTripper) http.RoundTripper {
    40  	return RoundTripperFunc(func(r *http.Request) (*http.Response, error) {
    41  		t := v1
    42  		if r.ProtoMajor == 2 {
    43  			t = v2
    44  		}
    45  		return t.RoundTrip(r)
    46  	})
    47  }
    48  
    49  const sleep = 30 * time.Millisecond
    50  
    51  var backOffTemplate = wait.Backoff{
    52  	Duration: 50 * time.Millisecond,
    53  	Factor:   1.4,
    54  	Jitter:   0.1, // At most 10% jitter.
    55  	Steps:    15,
    56  }
    57  
    58  // ErrTimeoutDialing when the timeout is reached after set amount of time.
    59  var ErrTimeoutDialing = errors.New("timed out dialing")
    60  
    61  // DialWithBackOff executes `net.Dialer.DialContext()` with exponentially increasing
    62  // dial timeouts. In addition it sleeps with random jitter between tries.
    63  var DialWithBackOff = NewBackoffDialer(backOffTemplate)
    64  
    65  // NewBackoffDialer returns a dialer that executes `net.Dialer.DialContext()` with
    66  // exponentially increasing dial timeouts. In addition it sleeps with random jitter
    67  // between tries.
    68  func NewBackoffDialer(backoffConfig wait.Backoff) func(context.Context, string, string) (net.Conn, error) {
    69  	return func(ctx context.Context, network, address string) (net.Conn, error) {
    70  		return dialBackOffHelper(ctx, network, address, backoffConfig, nil)
    71  	}
    72  }
    73  
    74  // DialTLSWithBackOff is same with DialWithBackOff but takes tls config.
    75  var DialTLSWithBackOff = NewTLSBackoffDialer(backOffTemplate)
    76  
    77  // NewTLSBackoffDialer is same with NewBackoffDialer but takes tls config.
    78  func NewTLSBackoffDialer(backoffConfig wait.Backoff) func(context.Context, string, string, *tls.Config) (net.Conn, error) {
    79  	return func(ctx context.Context, network, address string, tlsConf *tls.Config) (net.Conn, error) {
    80  		return dialBackOffHelper(ctx, network, address, backoffConfig, tlsConf)
    81  	}
    82  }
    83  
    84  func dialBackOffHelper(ctx context.Context, network, address string, bo wait.Backoff, tlsConf *tls.Config) (net.Conn, error) {
    85  	dialer := &net.Dialer{
    86  		Timeout:   bo.Duration, // Initial duration.
    87  		KeepAlive: 5 * time.Second,
    88  		DualStack: true,
    89  	}
    90  	start := time.Now()
    91  	for {
    92  		var (
    93  			c   net.Conn
    94  			err error
    95  		)
    96  		if tlsConf == nil {
    97  			c, err = dialer.DialContext(ctx, network, address)
    98  		} else {
    99  			d := tls.Dialer{NetDialer: dialer, Config: tlsConf}
   100  			c, err = d.DialContext(ctx, network, address)
   101  		}
   102  		if err != nil {
   103  			var errNet net.Error
   104  			if errors.As(err, &errNet) && errNet.Timeout() {
   105  				if bo.Steps < 1 {
   106  					break
   107  				}
   108  				dialer.Timeout = bo.Step()
   109  				time.Sleep(wait.Jitter(sleep, 1.0)) // Sleep with jitter.
   110  				continue
   111  			}
   112  			return nil, err
   113  		}
   114  		return c, nil
   115  	}
   116  	elapsed := time.Since(start)
   117  	return nil, fmt.Errorf("%w %s after %.2fs", ErrTimeoutDialing, address, elapsed.Seconds())
   118  }
   119  
   120  func newHTTPTransport(disableKeepAlives, disableCompression bool, maxIdle, maxIdlePerHost int) http.RoundTripper {
   121  	transport := http.DefaultTransport.(*http.Transport).Clone()
   122  	transport.DialContext = DialWithBackOff
   123  	transport.DisableKeepAlives = disableKeepAlives
   124  	transport.MaxIdleConns = maxIdle
   125  	transport.MaxIdleConnsPerHost = maxIdlePerHost
   126  	transport.ForceAttemptHTTP2 = false
   127  	transport.DisableCompression = disableCompression
   128  	return transport
   129  }
   130  
   131  type DialTLSContextFunc func(ctx context.Context, network, addr string) (net.Conn, error)
   132  
   133  func newHTTPSTransport(disableKeepAlives, disableCompression bool, maxIdle, maxIdlePerHost int, tlsContext DialTLSContextFunc) http.RoundTripper {
   134  	transport := http.DefaultTransport.(*http.Transport).Clone()
   135  	transport.DisableKeepAlives = disableKeepAlives
   136  	transport.MaxIdleConns = maxIdle
   137  	transport.MaxIdleConnsPerHost = maxIdlePerHost
   138  	transport.ForceAttemptHTTP2 = false
   139  	transport.DisableCompression = disableCompression
   140  	transport.DialTLSContext = tlsContext
   141  
   142  	return transport
   143  }
   144  
   145  // NewProberTransport creates a RoundTripper that is useful for probing,
   146  // since it will not cache connections.
   147  func NewProberTransport() http.RoundTripper {
   148  	return newAutoTransport(
   149  		newHTTPTransport(true /*disable keep-alives*/, false /*disable auto-compression*/, 0, 0 /*no caching*/),
   150  		NewH2CTransport())
   151  }
   152  
   153  // NewProxyAutoTLSTransport is same with NewProxyAutoTransport but it has DialTLSContextFunc to create HTTPS request.
   154  func NewProxyAutoTLSTransport(maxIdle, maxIdlePerHost int, tlsContext DialTLSContextFunc) http.RoundTripper {
   155  	return newAutoTransport(
   156  		newHTTPSTransport(false /*disable keep-alives*/, true /*disable auto-compression*/, maxIdle, maxIdlePerHost, tlsContext),
   157  		newH2Transport(true /*disable auto-compression*/, tlsContext))
   158  }
   159  
   160  // NewAutoTransport creates a RoundTripper that can use appropriate transport
   161  // based on the request's HTTP version.
   162  func NewAutoTransport(maxIdle, maxIdlePerHost int) http.RoundTripper {
   163  	return newAutoTransport(
   164  		newHTTPTransport(false /*disable keep-alives*/, false /*disable auto-compression*/, maxIdle, maxIdlePerHost),
   165  		newH2CTransport(false /*disable auto-compression*/))
   166  }
   167  
   168  // NewProxyAutoTransport creates a RoundTripper suitable for use by a reverse
   169  // proxy.  The returned transport uses HTTP or H2C based on the request's HTTP
   170  // version. The transport has DisableCompression set to true.
   171  func NewProxyAutoTransport(maxIdle, maxIdlePerHost int) http.RoundTripper {
   172  	return newAutoTransport(
   173  		newHTTPTransport(false /*disable keep-alives*/, true /*disable auto-compression*/, maxIdle, maxIdlePerHost),
   174  		newH2CTransport(true /*disable auto-compression*/))
   175  }
   176  
   177  // AutoTransport uses h2c for HTTP2 requests and falls back to `http.DefaultTransport` for all others
   178  var AutoTransport = NewAutoTransport(1000, 100)