get_handler.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. w.(http.Flusher).Flush()
  62. if stream {
  63. // watcher hub will not help to remove stream watcher
  64. // so we need to remove here
  65. defer watcher.Remove()
  66. for {
  67. select {
  68. case <-closeChan:
  69. return nil
  70. case event, ok := <-watcher.EventChan:
  71. if !ok {
  72. // If the channel is closed this may be an indication of
  73. // that notifications are much more than we are able to
  74. // send to the client in time. Then we simply end streaming.
  75. return nil
  76. }
  77. if req.Method == "HEAD" {
  78. continue
  79. }
  80. b, _ := json.Marshal(event)
  81. _, err := w.Write(b)
  82. if err != nil {
  83. return nil
  84. }
  85. w.(http.Flusher).Flush()
  86. }
  87. }
  88. }
  89. select {
  90. case <-closeChan:
  91. watcher.Remove()
  92. case event := <-watcher.EventChan:
  93. if req.Method == "HEAD" {
  94. return nil
  95. }
  96. b, _ := json.Marshal(event)
  97. w.Write(b)
  98. }
  99. return nil
  100. }
  101. func handleGet(key string, recursive, sort bool, w http.ResponseWriter, req *http.Request, s Server) error {
  102. event, err := s.Store().Get(key, recursive, sort)
  103. if err != nil {
  104. return err
  105. }
  106. if req.Method == "HEAD" {
  107. return nil
  108. }
  109. writeHeaders(w, s)
  110. b, _ := json.Marshal(event)
  111. w.Write(b)
  112. return nil
  113. }
  114. func writeHeaders(w http.ResponseWriter, s Server) {
  115. w.Header().Set("Content-Type", "application/json")
  116. w.Header().Add("X-Etcd-Index", fmt.Sprint(s.Store().Index()))
  117. w.Header().Add("X-Raft-Index", fmt.Sprint(s.CommitIndex()))
  118. w.Header().Add("X-Raft-Term", fmt.Sprint(s.Term()))
  119. w.WriteHeader(http.StatusOK)
  120. }