Browse Source

refactor dispatch

Xiang Li 12 years ago
parent
commit
e41ef9b733
3 changed files with 45 additions and 43 deletions
  1. 8 32
      etcd_handlers.go
  2. 6 3
      raft_handlers.go
  3. 31 8
      util.go

+ 8 - 32
etcd_handlers.go

@@ -105,7 +105,7 @@ func CreateHttpHandler(w http.ResponseWriter, req *http.Request) error {
 		ExpireTime: expireTime,
 	}
 
-	return dispatch(command, w, req, true)
+	return dispatchEtcdCommand(command, w, req)
 
 }
 
@@ -140,7 +140,7 @@ func UpdateHttpHandler(w http.ResponseWriter, req *http.Request) error {
 			ExpireTime: expireTime,
 		}
 
-		return dispatch(command, w, req, true)
+		return dispatchEtcdCommand(command, w, req)
 
 	} else { // update with test
 		var prevIndex uint64
@@ -161,7 +161,7 @@ func UpdateHttpHandler(w http.ResponseWriter, req *http.Request) error {
 			PrevIndex: prevIndex,
 		}
 
-		return dispatch(command, w, req, true)
+		return dispatchEtcdCommand(command, w, req)
 	}
 
 }
@@ -180,37 +180,12 @@ func DeleteHttpHandler(w http.ResponseWriter, req *http.Request) error {
 		command.Recursive = true
 	}
 
-	return dispatch(command, w, req, true)
+	return dispatchEtcdCommand(command, w, req)
 }
 
 // Dispatch the command to leader
-func dispatch(c Command, w http.ResponseWriter, req *http.Request, etcd bool) error {
-	if r.State() == raft.Leader {
-		if body, err := r.Do(c); err != nil {
-			return err
-		} else {
-			if body == nil {
-				return etcdErr.NewError(300, "Empty result from raft")
-			} else {
-				body, _ := body.([]byte)
-				w.WriteHeader(http.StatusOK)
-				w.Write(body)
-				return nil
-			}
-		}
-
-	} else {
-		leader := r.Leader()
-		// current no leader
-		if leader == "" {
-			return etcdErr.NewError(300, "")
-		}
-
-		redirect(leader, etcd, w, req)
-
-		return nil
-	}
-	return etcdErr.NewError(300, "")
+func dispatchEtcdCommand(c Command, w http.ResponseWriter, req *http.Request) error {
+	return dispatch(c, w, req, nameToEtcdURL)
 }
 
 //--------------------------------------
@@ -295,7 +270,8 @@ func GetHttpHandler(w http.ResponseWriter, req *http.Request) error {
 		if req.FormValue("consistent") == "true" {
 			if r.State() != raft.Leader {
 				leader := r.Leader()
-				redirect(leader, true, w, req)
+				url, _ := nameToEtcdURL(leader)
+				redirect(url, w, req)
 				return nil
 			}
 		}

+ 6 - 3
raft_handlers.go

@@ -104,7 +104,7 @@ func JoinHttpHandler(w http.ResponseWriter, req *http.Request) error {
 
 	if err := decodeJsonRequest(req, command); err == nil {
 		debugf("Receive Join Request from %s", command.Name)
-		return dispatch(command, w, req, false)
+		return dispatchRaftCommand(command, w, req)
 	} else {
 		w.WriteHeader(http.StatusInternalServerError)
 		return nil
@@ -125,8 +125,7 @@ func RemoveHttpHandler(w http.ResponseWriter, req *http.Request) {
 
 	debugf("[recv] Remove Request [%s]", command.Name)
 
-	dispatch(command, w, req, false)
-
+	dispatchRaftCommand(command, w, req)
 }
 
 // Response to the name request
@@ -142,3 +141,7 @@ func RaftVersionHttpHandler(w http.ResponseWriter, req *http.Request) {
 	w.WriteHeader(http.StatusOK)
 	w.Write([]byte(r.version))
 }
+
+func dispatchRaftCommand(c Command, w http.ResponseWriter, req *http.Request) error {
+	return dispatch(c, w, req, nameToRaftURL)
+}

+ 31 - 8
util.go

@@ -65,17 +65,40 @@ func startWebInterface() {
 // HTTP Utilities
 //--------------------------------------
 
-func redirect(node string, etcd bool, w http.ResponseWriter, req *http.Request) {
-	var url string
-	path := req.URL.Path
+func dispatch(c Command, w http.ResponseWriter, req *http.Request, toURL func(name string) (string, bool)) error {
+	if r.State() == raft.Leader {
+		if body, err := r.Do(c); err != nil {
+			return err
+		} else {
+			if body == nil {
+				return etcdErr.NewError(300, "Empty result from raft")
+			} else {
+				body, _ := body.([]byte)
+				w.WriteHeader(http.StatusOK)
+				w.Write(body)
+				return nil
+			}
+		}
 
-	if etcd {
-		etcdAddr, _ := nameToEtcdURL(node)
-		url = etcdAddr + path
 	} else {
-		raftAddr, _ := nameToRaftURL(node)
-		url = raftAddr + path
+		leader := r.Leader()
+		// current no leader
+		if leader == "" {
+			return etcdErr.NewError(300, "")
+		}
+		url, _ := toURL(leader)
+
+		redirect(url, w, req)
+
+		return nil
 	}
+	return etcdErr.NewError(300, "")
+}
+
+func redirect(hostname string, w http.ResponseWriter, req *http.Request) {
+	path := req.URL.Path
+
+	url := hostname + path
 
 	debugf("Redirect to %s", url)