blob: a1d7beb4d80595445aeaea928208d923c2ecce71 [file] [log] [blame]
David K. Bainbridge215e0242017-09-05 23:18:24 -07001// Package sockets provides helper functions to create and configure Unix or TCP sockets.
2package sockets
3
4import (
5 "errors"
6 "net"
7 "net/http"
8 "time"
9)
10
11// Why 32? See https://github.com/docker/docker/pull/8035.
12const defaultTimeout = 32 * time.Second
13
14// ErrProtocolNotAvailable is returned when a given transport protocol is not provided by the operating system.
15var ErrProtocolNotAvailable = errors.New("protocol not available")
16
17// ConfigureTransport configures the specified Transport according to the
18// specified proto and addr.
19// If the proto is unix (using a unix socket to communicate) or npipe the
20// compression is disabled.
21func ConfigureTransport(tr *http.Transport, proto, addr string) error {
22 switch proto {
23 case "unix":
24 return configureUnixTransport(tr, proto, addr)
25 case "npipe":
26 return configureNpipeTransport(tr, proto, addr)
27 default:
28 tr.Proxy = http.ProxyFromEnvironment
29 dialer, err := DialerFromEnvironment(&net.Dialer{
30 Timeout: defaultTimeout,
31 })
32 if err != nil {
33 return err
34 }
35 tr.Dial = dialer.Dial
36 }
37 return nil
38}