github.com/alloyci/alloy-runner@v1.0.1-0.20180222164613-925503ccafd6/helpers/docker/sockets.go (about)

     1  package docker_helpers
     2  
     3  import (
     4  	"net"
     5  	"net/http"
     6  	"time"
     7  
     8  	"github.com/docker/go-connections/sockets"
     9  )
    10  
    11  // Why 32? See https://github.com/docker/docker/pull/8035.
    12  const defaultTimeout = 32 * time.Second
    13  const defaultKeepAlive = 10 * time.Second
    14  const defaultTLSHandshakeTimeout = 10 * time.Second
    15  const defaultResponseHeaderTimeout = 30 * time.Second
    16  const defaultExpectContinueTimeout = 30 * time.Second
    17  const defaultIdleConnTimeout = time.Minute
    18  
    19  // configureTransport configures the specified Transport according to the
    20  // specified proto and addr.
    21  // If the proto is unix (using a unix socket to communicate) or npipe the
    22  // compression is disabled.
    23  func configureTransport(tr *http.Transport, proto, addr string) error {
    24  	err := sockets.ConfigureTransport(tr, proto, addr)
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	tr.TLSHandshakeTimeout = defaultTLSHandshakeTimeout
    30  	tr.ResponseHeaderTimeout = defaultResponseHeaderTimeout
    31  	tr.ExpectContinueTimeout = defaultExpectContinueTimeout
    32  	tr.IdleConnTimeout = defaultIdleConnTimeout
    33  
    34  	// for network protocols set custom sockets with keep-alive
    35  	if proto == "tcp" || proto == "http" || proto == "https" {
    36  		dialer, err := sockets.DialerFromEnvironment(&net.Dialer{
    37  			Timeout:   defaultTimeout,
    38  			KeepAlive: defaultKeepAlive,
    39  		})
    40  		if err != nil {
    41  			return err
    42  		}
    43  		tr.Dial = dialer.Dial
    44  	}
    45  	return nil
    46  }