http.go 2.5 KB

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