github.com/tompao/docker@v1.9.1/pkg/sockets/tcp_socket.go (about)

     1  // Package sockets provides helper functions to create and configure Unix or TCP
     2  // sockets.
     3  package sockets
     4  
     5  import (
     6  	"crypto/tls"
     7  	"net"
     8  	"net/http"
     9  	"time"
    10  
    11  	"github.com/docker/docker/pkg/listenbuffer"
    12  )
    13  
    14  // NewTCPSocket creates a TCP socket listener with the specified address and
    15  // and the specified tls configuration. If TLSConfig is set, will encapsulate the
    16  // TCP listener inside a TLS one.
    17  // The channel passed is used to activate the listenbuffer when the caller is ready
    18  // to accept connections.
    19  func NewTCPSocket(addr string, tlsConfig *tls.Config, activate <-chan struct{}) (net.Listener, error) {
    20  	l, err := listenbuffer.NewListenBuffer("tcp", addr, activate)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  	if tlsConfig != nil {
    25  		tlsConfig.NextProtos = []string{"http/1.1"}
    26  		l = tls.NewListener(l, tlsConfig)
    27  	}
    28  	return l, nil
    29  }
    30  
    31  // ConfigureTCPTransport configures the specified Transport according to the
    32  // specified proto and addr.
    33  // If the proto is unix (using a unix socket to communicate) the compression
    34  // is disabled.
    35  func ConfigureTCPTransport(tr *http.Transport, proto, addr string) {
    36  	// Why 32? See https://github.com/docker/docker/pull/8035.
    37  	timeout := 32 * time.Second
    38  	if proto == "unix" {
    39  		// No need for compression in local communications.
    40  		tr.DisableCompression = true
    41  		tr.Dial = func(_, _ string) (net.Conn, error) {
    42  			return net.DialTimeout(proto, addr, timeout)
    43  		}
    44  	} else {
    45  		tr.Proxy = http.ProxyFromEnvironment
    46  		tr.Dial = (&net.Dialer{Timeout: timeout}).Dial
    47  	}
    48  }