http.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. )
  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(id lease.LeaseID, url string, rt http.RoundTripper, timeout time.Duration) (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, Timeout: timeout}
  72. resp, err := cc.Post(url, "application/protobuf", bytes.NewReader(lreq))
  73. if err != nil {
  74. // TODO detect if leader failed and retry?
  75. return -1, err
  76. }
  77. b, err := ioutil.ReadAll(resp.Body)
  78. if err != nil {
  79. return -1, err
  80. }
  81. if resp.StatusCode == http.StatusNotFound {
  82. return -1, lease.ErrLeaseNotFound
  83. }
  84. if resp.StatusCode != http.StatusOK {
  85. return -1, fmt.Errorf("lease: unknown error(%s)", string(b))
  86. }
  87. lresp := &pb.LeaseKeepAliveResponse{}
  88. if err := lresp.Unmarshal(b); err != nil {
  89. return -1, fmt.Errorf(`lease: %v. data = "%s"`, err, string(b))
  90. }
  91. if lresp.ID != int64(id) {
  92. return -1, fmt.Errorf("lease: renew id mismatch")
  93. }
  94. return lresp.TTL, nil
  95. }