http.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2016 The etcd Authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package leasehttp
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io/ioutil"
  19. "net/http"
  20. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. "github.com/coreos/etcd/lease"
  22. "golang.org/x/net/context"
  23. )
  24. // NewHandler returns an http Handler for lease renewals
  25. func NewHandler(l lease.Lessor) http.Handler {
  26. return &leaseHandler{l}
  27. }
  28. type leaseHandler struct{ l lease.Lessor }
  29. func (h *leaseHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  30. if r.Method != "POST" {
  31. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  32. return
  33. }
  34. b, err := ioutil.ReadAll(r.Body)
  35. if err != nil {
  36. http.Error(w, "error reading body", http.StatusBadRequest)
  37. return
  38. }
  39. lreq := pb.LeaseKeepAliveRequest{}
  40. if err := lreq.Unmarshal(b); err != nil {
  41. http.Error(w, "error unmarshalling request", http.StatusBadRequest)
  42. return
  43. }
  44. ttl, err := h.l.Renew(lease.LeaseID(lreq.ID))
  45. if err != nil {
  46. if err == lease.ErrLeaseNotFound {
  47. http.Error(w, err.Error(), http.StatusNotFound)
  48. return
  49. }
  50. http.Error(w, err.Error(), http.StatusBadRequest)
  51. return
  52. }
  53. // TODO: fill out ResponseHeader
  54. resp := &pb.LeaseKeepAliveResponse{ID: lreq.ID, TTL: ttl}
  55. v, err := resp.Marshal()
  56. if err != nil {
  57. http.Error(w, err.Error(), http.StatusInternalServerError)
  58. return
  59. }
  60. w.Header().Set("Content-Type", "application/protobuf")
  61. w.Write(v)
  62. }
  63. // RenewHTTP renews a lease at a given primary server.
  64. // TODO: Batch request in future?
  65. func RenewHTTP(ctx context.Context, id lease.LeaseID, url string, rt http.RoundTripper) (int64, error) {
  66. // will post lreq protobuf to leader
  67. lreq, err := (&pb.LeaseKeepAliveRequest{ID: int64(id)}).Marshal()
  68. if err != nil {
  69. return -1, err
  70. }
  71. cc := &http.Client{Transport: rt}
  72. req, err := http.NewRequest("POST", url, bytes.NewReader(lreq))
  73. if err != nil {
  74. return -1, err
  75. }
  76. req.Header.Set("Content-Type", "application/protobuf")
  77. req.Cancel = ctx.Done()
  78. resp, err := cc.Do(req)
  79. if err != nil {
  80. // TODO detect if leader failed and retry?
  81. return -1, err
  82. }
  83. b, err := ioutil.ReadAll(resp.Body)
  84. resp.Body.Close()
  85. if err != nil {
  86. return -1, err
  87. }
  88. if resp.StatusCode == http.StatusNotFound {
  89. return -1, lease.ErrLeaseNotFound
  90. }
  91. if resp.StatusCode != http.StatusOK {
  92. return -1, fmt.Errorf("lease: unknown error(%s)", string(b))
  93. }
  94. lresp := &pb.LeaseKeepAliveResponse{}
  95. if err := lresp.Unmarshal(b); err != nil {
  96. return -1, fmt.Errorf(`lease: %v. data = "%s"`, err, string(b))
  97. }
  98. if lresp.ID != int64(id) {
  99. return -1, fmt.Errorf("lease: renew id mismatch")
  100. }
  101. return lresp.TTL, nil
  102. }