http.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2016 CoreOS, Inc.
  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 lease
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io/ioutil"
  19. "net/http"
  20. "time"
  21. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  22. "github.com/coreos/etcd/pkg/transport"
  23. )
  24. // NewHandler returns an http Handler for lease renewals
  25. func NewHandler(l Lessor) http.Handler {
  26. return &leaseHandler{l}
  27. }
  28. type leaseHandler struct{ l 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(LeaseID(lreq.ID))
  45. if err != nil {
  46. http.Error(w, err.Error(), http.StatusBadRequest)
  47. return
  48. }
  49. // TODO: fill out ResponseHeader
  50. resp := &pb.LeaseKeepAliveResponse{ID: lreq.ID, TTL: ttl}
  51. v, err := resp.Marshal()
  52. if err != nil {
  53. http.Error(w, err.Error(), http.StatusInternalServerError)
  54. return
  55. }
  56. w.Header().Set("Content-Type", "application/protobuf")
  57. w.Write(v)
  58. }
  59. // RenewHTTP renews a lease at a given primary server.
  60. func RenewHTTP(id LeaseID, url string, tlsInfo transport.TLSInfo, timeout time.Duration) (int64, error) {
  61. // will post lreq protobuf to leader
  62. lreq, err := (&pb.LeaseKeepAliveRequest{ID: int64(id)}).Marshal()
  63. if err != nil {
  64. return -1, err
  65. }
  66. // TODO creating a new transporter for each forward request
  67. // can be expensive; in the future reuse transports and batch requests
  68. rt, err := transport.NewTimeoutTransport(tlsInfo, timeout, 0, 0)
  69. if err != nil {
  70. return -1, err
  71. }
  72. cc := &http.Client{Transport: rt, Timeout: timeout}
  73. resp, err := cc.Post(url, "application/protobuf", bytes.NewReader(lreq))
  74. if err != nil {
  75. // TODO detect if leader failed and retry?
  76. return -1, err
  77. }
  78. b, err := ioutil.ReadAll(resp.Body)
  79. if err != nil {
  80. return -1, err
  81. }
  82. if resp.StatusCode != http.StatusOK {
  83. return -1, fmt.Errorf("lease: %s", string(b))
  84. }
  85. lresp := &pb.LeaseKeepAliveResponse{}
  86. if err := lresp.Unmarshal(b); err != nil {
  87. return -1, fmt.Errorf(`lease: %v. data = "%s"`, err, string(b))
  88. }
  89. if lresp.ID != int64(id) {
  90. return -1, fmt.Errorf("lease: renew id mismatch")
  91. }
  92. return lresp.TTL, err
  93. }