set_handler.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package v2
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "net/url"
  7. "github.com/gorilla/mux"
  8. )
  9. // setHandler attempts to set the current leader.
  10. func (h *handler) setHandler(w http.ResponseWriter, req *http.Request) {
  11. vars := mux.Vars(req)
  12. name := req.FormValue("name")
  13. if name == "" {
  14. http.Error(w, "leader name required", http.StatusInternalServerError)
  15. return
  16. }
  17. // Proxy the request to the the lock service.
  18. u, err := url.Parse(fmt.Sprintf("%s/mod/v2/lock/%s", h.addr, vars["key"]))
  19. if err != nil {
  20. http.Error(w, err.Error(), http.StatusInternalServerError)
  21. return
  22. }
  23. q := u.Query()
  24. q.Set("value", name)
  25. q.Set("ttl", req.FormValue("ttl"))
  26. q.Set("timeout", req.FormValue("timeout"))
  27. u.RawQuery = q.Encode()
  28. r, err := http.NewRequest("POST", u.String(), nil)
  29. if err != nil {
  30. http.Error(w, err.Error(), http.StatusInternalServerError)
  31. return
  32. }
  33. // Close request if this connection disconnects.
  34. closeNotifier, _ := w.(http.CloseNotifier)
  35. stopChan := make(chan bool)
  36. defer close(stopChan)
  37. go func() {
  38. select {
  39. case <-closeNotifier.CloseNotify():
  40. h.transport.CancelRequest(r)
  41. case <-stopChan:
  42. }
  43. }()
  44. // Read from the leader lock.
  45. resp, err := h.client.Do(r)
  46. if err != nil {
  47. http.Error(w, "set leader http error: " + err.Error(), http.StatusInternalServerError)
  48. return
  49. }
  50. defer resp.Body.Close()
  51. w.WriteHeader(resp.StatusCode)
  52. if resp.StatusCode != http.StatusOK {
  53. w.Write([]byte("set leader error: "))
  54. }
  55. io.Copy(w, resp.Body)
  56. }