optimize for submission from clustered master nodes

This commit is contained in:
Chris Lu 2014-02-05 10:49:05 -08:00
parent 0e5c4e432d
commit 69a7de1ad8
2 changed files with 11 additions and 3 deletions

View File

@ -62,7 +62,7 @@ func NewMasterServer(r *mux.Router, version string, port int, metaFolder string,
r.HandleFunc("/vol/grow", ms.proxyToLeader(secure(ms.whiteList, ms.volumeGrowHandler)))
r.HandleFunc("/vol/status", ms.proxyToLeader(secure(ms.whiteList, ms.volumeStatusHandler)))
r.HandleFunc("/vol/vacuum", ms.proxyToLeader(secure(ms.whiteList, ms.volumeVacuumHandler)))
r.HandleFunc("/submit", ms.proxyToLeader(secure(ms.whiteList, ms.submitFromMasterServerHandler)))
r.HandleFunc("/submit", secure(ms.whiteList, ms.submitFromMasterServerHandler))
r.HandleFunc("/", ms.redirectHandler)
ms.topo.StartRefreshWritableVolumes(garbageThreshold)
@ -74,9 +74,13 @@ func (ms *MasterServer) SetRaftServer(raftServer *RaftServer) {
ms.raftServer = raftServer
}
func (ms *MasterServer) IsLeader() bool {
return ms.raftServer == nil || ms.raftServer.IsLeader()
}
func (ms *MasterServer) proxyToLeader(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if ms.raftServer == nil || ms.raftServer.IsLeader() {
if ms.IsLeader() {
f(w, r)
} else {
targetUrl, err := url.Parse("http://" + ms.raftServer.Leader())

View File

@ -164,5 +164,9 @@ func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request)
}
func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
submitForClientHandler(w, r, "localhost:"+strconv.Itoa(ms.port))
if ms.IsLeader() {
submitForClientHandler(w, r, "localhost:"+strconv.Itoa(ms.port))
} else {
submitForClientHandler(w, r, ms.raftServer.Leader())
}
}