get_handler.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package v2
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. etcdErr "github.com/coreos/etcd/error"
  9. "github.com/coreos/etcd/log"
  10. "github.com/coreos/etcd/third_party/github.com/goraft/raft"
  11. "github.com/coreos/etcd/third_party/github.com/gorilla/mux"
  12. )
  13. func GetHandler(w http.ResponseWriter, req *http.Request, s Server) error {
  14. vars := mux.Vars(req)
  15. key := "/" + vars["key"]
  16. recursive := (req.FormValue("recursive") == "true")
  17. sort := (req.FormValue("sorted") == "true")
  18. if req.FormValue("quorum") == "true" {
  19. c := s.Store().CommandFactory().CreateGetCommand(key, recursive, sort)
  20. return s.Dispatch(c, w, req)
  21. }
  22. // Help client to redirect the request to the current leader
  23. if req.FormValue("consistent") == "true" && s.State() != raft.Leader {
  24. leader := s.Leader()
  25. hostname, _ := s.ClientURL(leader)
  26. url, err := url.Parse(hostname)
  27. if err != nil {
  28. log.Warn("Redirect cannot parse hostName ", hostname)
  29. return err
  30. }
  31. url.RawQuery = req.URL.RawQuery
  32. url.Path = req.URL.Path
  33. log.Debugf("Redirect consistent get to %s", url.String())
  34. http.Redirect(w, req, url.String(), http.StatusTemporaryRedirect)
  35. return nil
  36. }
  37. waitIndex := req.FormValue("waitIndex")
  38. stream := (req.FormValue("stream") == "true")
  39. if req.FormValue("wait") == "true" {
  40. return handleWatch(key, recursive, stream, waitIndex, w, req, s)
  41. }
  42. return handleGet(key, recursive, sort, w, req, s)
  43. }
  44. func handleWatch(key string, recursive, stream bool, waitIndex string, w http.ResponseWriter, req *http.Request, s Server) error {
  45. // Create a command to watch from a given index (default 0).
  46. var sinceIndex uint64 = 0
  47. var err error
  48. if waitIndex != "" {
  49. sinceIndex, err = strconv.ParseUint(waitIndex, 10, 64)
  50. if err != nil {
  51. return etcdErr.NewError(etcdErr.EcodeIndexNaN, "Watch From Index", s.Store().Index())
  52. }
  53. }
  54. watcher, err := s.Store().Watch(key, recursive, stream, sinceIndex)
  55. if err != nil {
  56. return err
  57. }
  58. cn, _ := w.(http.CloseNotifier)
  59. closeChan := cn.CloseNotify()
  60. writeHeaders(w, s)
  61. if stream {
  62. // watcher hub will not help to remove stream watcher
  63. // so we need to remove here
  64. defer watcher.Remove()
  65. for {
  66. select {
  67. case <-closeChan:
  68. return nil
  69. case event, ok := <-watcher.EventChan:
  70. if !ok {
  71. // If the channel is closed this may be an indication of
  72. // that notifications are much more than we are able to
  73. // send to the client in time. Then we simply end streaming.
  74. return nil
  75. }
  76. if req.Method == "HEAD" {
  77. continue
  78. }
  79. b, _ := json.Marshal(event)
  80. _, err := w.Write(b)
  81. if err != nil {
  82. return nil
  83. }
  84. w.(http.Flusher).Flush()
  85. }
  86. }
  87. }
  88. select {
  89. case <-closeChan:
  90. watcher.Remove()
  91. case event := <-watcher.EventChan:
  92. if req.Method == "HEAD" {
  93. return nil
  94. }
  95. b, _ := json.Marshal(event)
  96. w.Write(b)
  97. }
  98. return nil
  99. }
  100. func handleGet(key string, recursive, sort bool, w http.ResponseWriter, req *http.Request, s Server) error {
  101. event, err := s.Store().Get(key, recursive, sort)
  102. if err != nil {
  103. return err
  104. }
  105. if req.Method == "HEAD" {
  106. return nil
  107. }
  108. writeHeaders(w, s)
  109. b, _ := json.Marshal(event)
  110. w.Write(b)
  111. return nil
  112. }
  113. func writeHeaders(w http.ResponseWriter, s Server) {
  114. w.Header().Set("Content-Type", "application/json")
  115. w.Header().Add("X-Etcd-Index", fmt.Sprint(s.Store().Index()))
  116. w.Header().Add("X-Raft-Index", fmt.Sprint(s.CommitIndex()))
  117. w.Header().Add("X-Raft-Term", fmt.Sprint(s.Term()))
  118. w.WriteHeader(http.StatusOK)
  119. }