http.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 client
  14. import (
  15. "io/ioutil"
  16. "net/http"
  17. "net/url"
  18. "time"
  19. "github.com/coreos/etcd/Godeps/_workspace/src/code.google.com/p/go.net/context"
  20. )
  21. var (
  22. ErrTimeout = context.DeadlineExceeded
  23. DefaultRequestTimeout = 5 * time.Second
  24. )
  25. // transport mimics http.Transport to provide an interface which can be
  26. // substituted for testing (since the RoundTripper interface alone does not
  27. // require the CancelRequest method)
  28. type transport interface {
  29. http.RoundTripper
  30. CancelRequest(req *http.Request)
  31. }
  32. type httpAction interface {
  33. httpRequest(url.URL) *http.Request
  34. }
  35. type roundTripResponse struct {
  36. resp *http.Response
  37. err error
  38. }
  39. type httpClient struct {
  40. transport transport
  41. endpoint url.URL
  42. timeout time.Duration
  43. }
  44. func newHTTPClient(tr *http.Transport, ep string, to time.Duration) (*httpClient, error) {
  45. u, err := url.Parse(ep)
  46. if err != nil {
  47. return nil, err
  48. }
  49. c := &httpClient{
  50. transport: tr,
  51. endpoint: *u,
  52. timeout: to,
  53. }
  54. return c, nil
  55. }
  56. func (c *httpClient) doWithTimeout(act httpAction) (*http.Response, []byte, error) {
  57. ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
  58. defer cancel()
  59. return c.do(ctx, act)
  60. }
  61. func (c *httpClient) do(ctx context.Context, act httpAction) (*http.Response, []byte, error) {
  62. req := act.httpRequest(c.endpoint)
  63. rtchan := make(chan roundTripResponse, 1)
  64. go func() {
  65. resp, err := c.transport.RoundTrip(req)
  66. rtchan <- roundTripResponse{resp: resp, err: err}
  67. close(rtchan)
  68. }()
  69. var resp *http.Response
  70. var err error
  71. select {
  72. case rtresp := <-rtchan:
  73. resp, err = rtresp.resp, rtresp.err
  74. case <-ctx.Done():
  75. c.transport.CancelRequest(req)
  76. // wait for request to actually exit before continuing
  77. <-rtchan
  78. err = ctx.Err()
  79. }
  80. // always check for resp nil-ness to deal with possible
  81. // race conditions between channels above
  82. defer func() {
  83. if resp != nil {
  84. resp.Body.Close()
  85. }
  86. }()
  87. if err != nil {
  88. return nil, nil, err
  89. }
  90. body, err := ioutil.ReadAll(resp.Body)
  91. return resp, body, err
  92. }