...

Source file src/go.etcd.io/etcd/client/pkg/v3/transport/transport.go

Documentation: go.etcd.io/etcd/client/pkg/v3/transport

     1  // Copyright 2016 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package transport
    16  
    17  import (
    18  	"context"
    19  	"net"
    20  	"net/http"
    21  	"strings"
    22  	"time"
    23  )
    24  
    25  type unixTransport struct{ *http.Transport }
    26  
    27  func NewTransport(info TLSInfo, dialtimeoutd time.Duration) (*http.Transport, error) {
    28  	cfg, err := info.ClientConfig()
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	t := &http.Transport{
    34  		Proxy: http.ProxyFromEnvironment,
    35  		DialContext: (&net.Dialer{
    36  			Timeout: dialtimeoutd,
    37  			// value taken from http.DefaultTransport
    38  			KeepAlive: 30 * time.Second,
    39  		}).DialContext,
    40  		// value taken from http.DefaultTransport
    41  		TLSHandshakeTimeout: 10 * time.Second,
    42  		TLSClientConfig:     cfg,
    43  	}
    44  
    45  	dialer := &net.Dialer{
    46  		Timeout:   dialtimeoutd,
    47  		KeepAlive: 30 * time.Second,
    48  	}
    49  
    50  	dialContext := func(ctx context.Context, net, addr string) (net.Conn, error) {
    51  		return dialer.DialContext(ctx, "unix", addr)
    52  	}
    53  	tu := &http.Transport{
    54  		Proxy:               http.ProxyFromEnvironment,
    55  		DialContext:         dialContext,
    56  		TLSHandshakeTimeout: 10 * time.Second,
    57  		TLSClientConfig:     cfg,
    58  		// Cost of reopening connection on sockets is low, and they are mostly used in testing.
    59  		// Long living unix-transport connections were leading to 'leak' test flakes.
    60  		// Alternativly the returned Transport (t) should override CloseIdleConnections to
    61  		// forward it to 'tu' as well.
    62  		IdleConnTimeout: time.Microsecond,
    63  	}
    64  	ut := &unixTransport{tu}
    65  
    66  	t.RegisterProtocol("unix", ut)
    67  	t.RegisterProtocol("unixs", ut)
    68  
    69  	return t, nil
    70  }
    71  
    72  func (urt *unixTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    73  	url := *req.URL
    74  	req.URL = &url
    75  	req.URL.Scheme = strings.Replace(req.URL.Scheme, "unix", "http", 1)
    76  	return urt.Transport.RoundTrip(req)
    77  }
    78  

View as plain text