David K. Bainbridge | 215e024 | 2017-09-05 23:18:24 -0700 | [diff] [blame] | 1 | // Package sockets provides helper functions to create and configure Unix or TCP sockets. |
| 2 | package sockets |
| 3 | |
| 4 | import ( |
| 5 | "errors" |
| 6 | "net" |
| 7 | "net/http" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | // Why 32? See https://github.com/docker/docker/pull/8035. |
| 12 | const defaultTimeout = 32 * time.Second |
| 13 | |
| 14 | // ErrProtocolNotAvailable is returned when a given transport protocol is not provided by the operating system. |
| 15 | var 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. |
| 21 | func 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 | } |