github.com/secure-build/gitlab-runner@v12.5.0+incompatible/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  const defaultTimeout = 300 * time.Second
    12  const defaultKeepAlive = 10 * time.Second
    13  const defaultTLSHandshakeTimeout = 60 * time.Second
    14  const defaultResponseHeaderTimeout = 120 * time.Second
    15  const defaultExpectContinueTimeout = 120 * time.Second
    16  const defaultIdleConnTimeout = 10 * time.Second
    17  
    18  // configureTransport configures the specified Transport according to the
    19  // specified proto and addr.
    20  // If the proto is unix (using a unix socket to communicate) or npipe the
    21  // compression is disabled.
    22  func configureTransport(tr *http.Transport, proto, addr string) error {
    23  	err := sockets.ConfigureTransport(tr, proto, addr)
    24  	if err != nil {
    25  		return err
    26  	}
    27  
    28  	tr.TLSHandshakeTimeout = defaultTLSHandshakeTimeout
    29  	tr.ResponseHeaderTimeout = defaultResponseHeaderTimeout
    30  	tr.ExpectContinueTimeout = defaultExpectContinueTimeout
    31  	tr.IdleConnTimeout = defaultIdleConnTimeout
    32  
    33  	// for network protocols set custom sockets with keep-alive
    34  	if proto == "tcp" || proto == "http" || proto == "https" {
    35  		dialer, err := sockets.DialerFromEnvironment(&net.Dialer{
    36  			Timeout:   defaultTimeout,
    37  			KeepAlive: defaultKeepAlive,
    38  		})
    39  		if err != nil {
    40  			return err
    41  		}
    42  		tr.Dial = dialer.Dial
    43  	}
    44  	return nil
    45  }