http.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. "encoding/json"
  16. "fmt"
  17. "io/ioutil"
  18. "net/http"
  19. "net/url"
  20. "path"
  21. "strconv"
  22. "strings"
  23. "time"
  24. "github.com/coreos/etcd/Godeps/_workspace/src/code.google.com/p/go.net/context"
  25. )
  26. var (
  27. DefaultV2KeysPrefix = "/v2/keys"
  28. ErrTimeout = context.DeadlineExceeded
  29. )
  30. // transport mimics http.Transport to provide an interface which can be
  31. // substituted for testing (since the RoundTripper interface alone does not
  32. // require the CancelRequest method)
  33. type transport interface {
  34. http.RoundTripper
  35. CancelRequest(req *http.Request)
  36. }
  37. type httpClient struct {
  38. transport transport
  39. endpoint url.URL
  40. timeout time.Duration
  41. }
  42. func NewHTTPClient(tr *http.Transport, ep string, timeout time.Duration) (*httpClient, error) {
  43. u, err := url.Parse(ep)
  44. if err != nil {
  45. return nil, err
  46. }
  47. c := &httpClient{
  48. transport: tr,
  49. endpoint: *u,
  50. timeout: timeout,
  51. }
  52. return c, nil
  53. }
  54. func (c *httpClient) SetPrefix(p string) {
  55. DefaultV2KeysPrefix = p
  56. }
  57. func (c *httpClient) Create(key, val string, ttl time.Duration) (*Response, error) {
  58. create := &createAction{
  59. Key: key,
  60. Value: val,
  61. }
  62. if ttl >= 0 {
  63. uttl := uint64(ttl.Seconds())
  64. create.TTL = &uttl
  65. }
  66. ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
  67. httpresp, body, err := c.do(ctx, create)
  68. cancel()
  69. if err != nil {
  70. return nil, err
  71. }
  72. return unmarshalHTTPResponse(httpresp.StatusCode, body)
  73. }
  74. func (c *httpClient) Get(key string) (*Response, error) {
  75. get := &getAction{
  76. Key: key,
  77. Recursive: false,
  78. }
  79. ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
  80. httpresp, body, err := c.do(ctx, get)
  81. cancel()
  82. if err != nil {
  83. return nil, err
  84. }
  85. return unmarshalHTTPResponse(httpresp.StatusCode, body)
  86. }
  87. type roundTripResponse struct {
  88. resp *http.Response
  89. err error
  90. }
  91. func (c *httpClient) do(ctx context.Context, act httpAction) (*http.Response, []byte, error) {
  92. req := act.httpRequest(c.endpoint)
  93. rtchan := make(chan roundTripResponse, 1)
  94. go func() {
  95. resp, err := c.transport.RoundTrip(req)
  96. rtchan <- roundTripResponse{resp: resp, err: err}
  97. close(rtchan)
  98. }()
  99. var resp *http.Response
  100. var err error
  101. select {
  102. case rtresp := <-rtchan:
  103. resp, err = rtresp.resp, rtresp.err
  104. case <-ctx.Done():
  105. c.transport.CancelRequest(req)
  106. // wait for request to actually exit before continuing
  107. <-rtchan
  108. err = ctx.Err()
  109. }
  110. // always check for resp nil-ness to deal with possible
  111. // race conditions between channels above
  112. defer func() {
  113. if resp != nil {
  114. resp.Body.Close()
  115. }
  116. }()
  117. if err != nil {
  118. return nil, nil, err
  119. }
  120. body, err := ioutil.ReadAll(resp.Body)
  121. return resp, body, err
  122. }
  123. func (c *httpClient) Watch(key string, idx uint64) Watcher {
  124. return &httpWatcher{
  125. httpClient: *c,
  126. nextWait: waitAction{
  127. Key: key,
  128. WaitIndex: idx,
  129. Recursive: false,
  130. },
  131. }
  132. }
  133. func (c *httpClient) RecursiveWatch(key string, idx uint64) Watcher {
  134. return &httpWatcher{
  135. httpClient: *c,
  136. nextWait: waitAction{
  137. Key: key,
  138. WaitIndex: idx,
  139. Recursive: true,
  140. },
  141. }
  142. }
  143. type httpWatcher struct {
  144. httpClient
  145. nextWait waitAction
  146. }
  147. func (hw *httpWatcher) Next() (*Response, error) {
  148. httpresp, body, err := hw.httpClient.do(context.Background(), &hw.nextWait)
  149. if err != nil {
  150. return nil, err
  151. }
  152. resp, err := unmarshalHTTPResponse(httpresp.StatusCode, body)
  153. if err != nil {
  154. return nil, err
  155. }
  156. hw.nextWait.WaitIndex = resp.Node.ModifiedIndex + 1
  157. return resp, nil
  158. }
  159. func v2KeysURL(ep url.URL, key string) *url.URL {
  160. ep.Path = path.Join(ep.Path, DefaultV2KeysPrefix, key)
  161. return &ep
  162. }
  163. type httpAction interface {
  164. httpRequest(url.URL) *http.Request
  165. }
  166. type getAction struct {
  167. Key string
  168. Recursive bool
  169. }
  170. func (g *getAction) httpRequest(ep url.URL) *http.Request {
  171. u := v2KeysURL(ep, g.Key)
  172. params := u.Query()
  173. params.Set("recursive", strconv.FormatBool(g.Recursive))
  174. u.RawQuery = params.Encode()
  175. req, _ := http.NewRequest("GET", u.String(), nil)
  176. return req
  177. }
  178. type waitAction struct {
  179. Key string
  180. WaitIndex uint64
  181. Recursive bool
  182. }
  183. func (w *waitAction) httpRequest(ep url.URL) *http.Request {
  184. u := v2KeysURL(ep, w.Key)
  185. params := u.Query()
  186. params.Set("wait", "true")
  187. params.Set("waitIndex", strconv.FormatUint(w.WaitIndex, 10))
  188. params.Set("recursive", strconv.FormatBool(w.Recursive))
  189. u.RawQuery = params.Encode()
  190. req, _ := http.NewRequest("GET", u.String(), nil)
  191. return req
  192. }
  193. type createAction struct {
  194. Key string
  195. Value string
  196. TTL *uint64
  197. }
  198. func (c *createAction) httpRequest(ep url.URL) *http.Request {
  199. u := v2KeysURL(ep, c.Key)
  200. params := u.Query()
  201. params.Set("prevExist", "false")
  202. u.RawQuery = params.Encode()
  203. form := url.Values{}
  204. form.Add("value", c.Value)
  205. if c.TTL != nil {
  206. form.Add("ttl", strconv.FormatUint(*c.TTL, 10))
  207. }
  208. body := strings.NewReader(form.Encode())
  209. req, _ := http.NewRequest("PUT", u.String(), body)
  210. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  211. return req
  212. }
  213. func unmarshalHTTPResponse(code int, body []byte) (res *Response, err error) {
  214. switch code {
  215. case http.StatusOK, http.StatusCreated:
  216. res, err = unmarshalSuccessfulResponse(body)
  217. default:
  218. err = unmarshalErrorResponse(code)
  219. }
  220. return
  221. }
  222. func unmarshalSuccessfulResponse(body []byte) (*Response, error) {
  223. var res Response
  224. err := json.Unmarshal(body, &res)
  225. if err != nil {
  226. return nil, err
  227. }
  228. return &res, nil
  229. }
  230. func unmarshalErrorResponse(code int) error {
  231. switch code {
  232. case http.StatusNotFound:
  233. return ErrKeyNoExist
  234. case http.StatusPreconditionFailed:
  235. return ErrKeyExists
  236. case http.StatusInternalServerError:
  237. // this isn't necessarily true
  238. return ErrNoLeader
  239. default:
  240. }
  241. return fmt.Errorf("unrecognized HTTP status code %d", code)
  242. }