sockets.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Package sockets provides helper functions to create and configure Unix or TCP sockets.
  2. package sockets
  3. import (
  4. "net"
  5. "net/http"
  6. "time"
  7. )
  8. // Why 32? See https://github.com/docker/docker/pull/8035.
  9. const defaultTimeout = 32 * time.Second
  10. // ConfigureTransport configures the specified Transport according to the
  11. // specified proto and addr.
  12. // If the proto is unix (using a unix socket to communicate) or npipe the
  13. // compression is disabled.
  14. func ConfigureTransport(tr *http.Transport, proto, addr string) error {
  15. switch proto {
  16. case "unix":
  17. // No need for compression in local communications.
  18. tr.DisableCompression = true
  19. tr.Dial = func(_, _ string) (net.Conn, error) {
  20. return net.DialTimeout(proto, addr, defaultTimeout)
  21. }
  22. case "npipe":
  23. // No need for compression in local communications.
  24. tr.DisableCompression = true
  25. tr.Dial = func(_, _ string) (net.Conn, error) {
  26. return DialPipe(addr, defaultTimeout)
  27. }
  28. default:
  29. tr.Proxy = http.ProxyFromEnvironment
  30. dialer, err := DialerFromEnvironment(&net.Dialer{
  31. Timeout: defaultTimeout,
  32. })
  33. if err != nil {
  34. return err
  35. }
  36. tr.Dial = dialer.Dial
  37. }
  38. return nil
  39. }