timeout_transport.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package transport
  14. import (
  15. "net"
  16. "net/http"
  17. "time"
  18. )
  19. // NewTimeoutTransport returns a transport created using the given TLS info.
  20. // If read/write on the created connection blocks longer than its time limit,
  21. // it will return timeout error.
  22. func NewTimeoutTransport(info TLSInfo, rdtimeoutd, wtimeoutd time.Duration) (*http.Transport, error) {
  23. tr, err := NewTransport(info)
  24. if err != nil {
  25. return nil, err
  26. }
  27. tr.Dial = (&rwTimeoutDialer{
  28. Dialer: net.Dialer{
  29. Timeout: 30 * time.Second,
  30. KeepAlive: 30 * time.Second,
  31. },
  32. rdtimeoutd: rdtimeoutd,
  33. wtimeoutd: wtimeoutd,
  34. }).Dial
  35. return tr, nil
  36. }