tcp_socket.go 917 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package sockets
  2. import (
  3. "crypto/tls"
  4. "net"
  5. "net/http"
  6. "time"
  7. "github.com/docker/docker/pkg/listenbuffer"
  8. )
  9. func NewTcpSocket(addr string, tlsConfig *tls.Config, activate <-chan struct{}) (net.Listener, error) {
  10. l, err := listenbuffer.NewListenBuffer("tcp", addr, activate)
  11. if err != nil {
  12. return nil, err
  13. }
  14. if tlsConfig != nil {
  15. tlsConfig.NextProtos = []string{"http/1.1"}
  16. l = tls.NewListener(l, tlsConfig)
  17. }
  18. return l, nil
  19. }
  20. func ConfigureTCPTransport(tr *http.Transport, proto, addr string) {
  21. // Why 32? See https://github.com/docker/docker/pull/8035.
  22. timeout := 32 * time.Second
  23. if proto == "unix" {
  24. // No need for compression in local communications.
  25. tr.DisableCompression = true
  26. tr.Dial = func(_, _ string) (net.Conn, error) {
  27. return net.DialTimeout(proto, addr, timeout)
  28. }
  29. } else {
  30. tr.Proxy = http.ProxyFromEnvironment
  31. tr.Dial = (&net.Dialer{Timeout: timeout}).Dial
  32. }
  33. }