use 307 to redirect client

This commit is contained in:
Xiang Li 2013-06-28 11:26:17 -07:00
parent e91ea7be38
commit fcb78ed5cb

View File

@ -4,7 +4,7 @@ import (
"encoding/json" "encoding/json"
"github.com/xiangli-cmu/go-raft" "github.com/xiangli-cmu/go-raft"
"net/http" "net/http"
//"fmt" "fmt"
"io/ioutil" "io/ioutil"
//"bytes" //"bytes"
"strconv" "strconv"
@ -78,7 +78,7 @@ func JoinHttpHandler(w http.ResponseWriter, req *http.Request) {
if err := decodeJsonRequest(req, command); err == nil { if err := decodeJsonRequest(req, command); err == nil {
debug("Receive Join Request from %s", command.Name) debug("Receive Join Request from %s", command.Name)
excute(command, &w) excute(command, &w, req)
} else { } else {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
@ -116,7 +116,7 @@ func SetHttpHandler(w http.ResponseWriter, req *http.Request) {
command.ExpireTime = time.Unix(0, 0) command.ExpireTime = time.Unix(0, 0)
} }
excute(command, &w) excute(command, &w, req)
} }
@ -128,10 +128,10 @@ func DeleteHttpHandler(w http.ResponseWriter, req *http.Request) {
command := &DeleteCommand{} command := &DeleteCommand{}
command.Key = key command.Key = key
excute(command, &w) excute(command, &w, req)
} }
func excute(c Command, w *http.ResponseWriter) { func excute(c Command, w *http.ResponseWriter, req *http.Request) {
if server.State() == "leader" { if server.State() == "leader" {
if body, err := server.Do(c); err != nil { if body, err := server.Do(c); err != nil {
warn("Commit failed %v", err) warn("Commit failed %v", err)
@ -155,8 +155,12 @@ func excute(c Command, w *http.ResponseWriter) {
} else { } else {
// tell the client where is the leader // tell the client where is the leader
debug("Redirect to the leader %s", server.Leader()) debug("Redirect to the leader %s", server.Leader())
(*w).WriteHeader(http.StatusServiceUnavailable)
(*w).Write([]byte(server.Leader())) path := req.URL.Path
url := "http://" + server.Leader() + path
debug("redirect to ", url)
http.Redirect(*w, req, url, http.StatusTemporaryRedirect)
return return
} }