http.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 leasehttp
  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/lease"
  23. "github.com/coreos/etcd/pkg/transport"
  24. )
  25. // NewHandler returns an http Handler for lease renewals
  26. func NewHandler(l lease.Lessor) http.Handler {
  27. return &leaseHandler{l}
  28. }
  29. type leaseHandler struct{ l lease.Lessor }
  30. func (h *leaseHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  31. if r.Method != "POST" {
  32. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  33. return
  34. }
  35. b, err := ioutil.ReadAll(r.Body)
  36. if err != nil {
  37. http.Error(w, "error reading body", http.StatusBadRequest)
  38. return
  39. }
  40. lreq := pb.LeaseKeepAliveRequest{}
  41. if err := lreq.Unmarshal(b); err != nil {
  42. http.Error(w, "error unmarshalling request", http.StatusBadRequest)
  43. return
  44. }
  45. ttl, err := h.l.Renew(lease.LeaseID(lreq.ID))
  46. if err != nil {
  47. http.Error(w, err.Error(), http.StatusBadRequest)
  48. return
  49. }
  50. // TODO: fill out ResponseHeader
  51. resp := &pb.LeaseKeepAliveResponse{ID: lreq.ID, TTL: ttl}
  52. v, err := resp.Marshal()
  53. if err != nil {
  54. http.Error(w, err.Error(), http.StatusInternalServerError)
  55. return
  56. }
  57. w.Header().Set("Content-Type", "application/protobuf")
  58. w.Write(v)
  59. }
  60. // RenewHTTP renews a lease at a given primary server.
  61. func RenewHTTP(id lease.LeaseID, url string, tlsInfo transport.TLSInfo, timeout time.Duration) (int64, error) {
  62. // will post lreq protobuf to leader
  63. lreq, err := (&pb.LeaseKeepAliveRequest{ID: int64(id)}).Marshal()
  64. if err != nil {
  65. return -1, err
  66. }
  67. // TODO creating a new transporter for each forward request
  68. // can be expensive; in the future reuse transports and batch requests
  69. rt, err := transport.NewTimeoutTransport(tlsInfo, timeout, 0, 0)
  70. if err != nil {
  71. return -1, err
  72. }
  73. cc := &http.Client{Transport: rt, Timeout: timeout}
  74. resp, err := cc.Post(url, "application/protobuf", bytes.NewReader(lreq))
  75. if err != nil {
  76. // TODO detect if leader failed and retry?
  77. return -1, err
  78. }
  79. b, err := ioutil.ReadAll(resp.Body)
  80. if err != nil {
  81. return -1, err
  82. }
  83. if resp.StatusCode != http.StatusOK {
  84. return -1, fmt.Errorf("lease: %s", string(b))
  85. }
  86. lresp := &pb.LeaseKeepAliveResponse{}
  87. if err := lresp.Unmarshal(b); err != nil {
  88. return -1, fmt.Errorf(`lease: %v. data = "%s"`, err, string(b))
  89. }
  90. if lresp.ID != int64(id) {
  91. return -1, fmt.Errorf("lease: renew id mismatch")
  92. }
  93. return lresp.TTL, err
  94. }