raft_handler.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright 2014 CoreOS Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package etcd
  14. import (
  15. "encoding/json"
  16. "log"
  17. "net/http"
  18. "strconv"
  19. "sync"
  20. "github.com/coreos/etcd/raft"
  21. )
  22. const (
  23. raftPrefix = "/raft"
  24. )
  25. type raftHandler struct {
  26. mu sync.RWMutex
  27. serving bool
  28. peerGetter peerGetter
  29. recv chan *raft.Message
  30. *http.ServeMux
  31. }
  32. func newRaftHandler(p peerGetter) *raftHandler {
  33. h := &raftHandler{
  34. recv: make(chan *raft.Message, 512),
  35. peerGetter: p,
  36. }
  37. h.ServeMux = http.NewServeMux()
  38. h.ServeMux.HandleFunc(raftPrefix+"/cfg/", h.serveCfg)
  39. h.ServeMux.HandleFunc(raftPrefix, h.serveRaft)
  40. return h
  41. }
  42. func (h *raftHandler) start() {
  43. h.mu.Lock()
  44. h.serving = true
  45. h.mu.Unlock()
  46. }
  47. func (h *raftHandler) stop() {
  48. h.mu.Lock()
  49. h.serving = false
  50. h.mu.Unlock()
  51. }
  52. func (h *raftHandler) serveRaft(w http.ResponseWriter, r *http.Request) {
  53. h.mu.RLock()
  54. serving := h.serving
  55. h.mu.RUnlock()
  56. if !serving {
  57. http.Error(w, "404 page not found", http.StatusNotFound)
  58. return
  59. }
  60. msg := new(raft.Message)
  61. if err := json.NewDecoder(r.Body).Decode(msg); err != nil {
  62. log.Println(err)
  63. return
  64. }
  65. select {
  66. case h.recv <- msg:
  67. default:
  68. log.Println("drop")
  69. // drop the incoming package at network layer if the upper layer
  70. // cannot consume them in time.
  71. // TODO(xiangli): not return 200.
  72. }
  73. return
  74. }
  75. func (h *raftHandler) serveCfg(w http.ResponseWriter, r *http.Request) {
  76. h.mu.RLock()
  77. serving := h.serving
  78. h.mu.RUnlock()
  79. if !serving {
  80. http.Error(w, "404 page not found", http.StatusNotFound)
  81. return
  82. }
  83. id, err := strconv.ParseInt(r.URL.Path[len("/raft/cfg/"):], 10, 64)
  84. if err != nil {
  85. http.Error(w, err.Error(), http.StatusBadRequest)
  86. return
  87. }
  88. p, err := h.peerGetter.peer(id)
  89. if err == nil {
  90. w.Write([]byte(p.url))
  91. return
  92. }
  93. http.Error(w, err.Error(), http.StatusNotFound)
  94. }