Skip to content

Commit

Permalink
Avoid redirecting when the leader remains unchanged
Browse files Browse the repository at this point in the history
Signed-off-by: JmPotato <ghzpotato@gmail.com>
  • Loading branch information
JmPotato committed May 30, 2024
1 parent 52389b0 commit d027b92
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/utils/apiutil/serverapi/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,16 @@ func (h *redirector) ServeHTTP(w http.ResponseWriter, r *http.Request, next http
w.Header().Add(apiutil.XForwardedToMicroServiceHeader, "true")
} else if name := r.Header.Get(apiutil.PDRedirectorHeader); len(name) == 0 {
leader := h.waitForLeader(r)
// The leader has not been elected yet.
if leader == nil {
http.Error(w, "no leader", http.StatusServiceUnavailable)
return
}
// If the leader is the current server now, we can handle the request directly.
if h.s.GetMember().IsLeader() || leader.GetName() == h.s.Name() {
next(w, r)
return
}
clientUrls = leader.GetClientUrls()
r.Header.Set(apiutil.PDRedirectorHeader, h.s.Name())
} else {
Expand Down

0 comments on commit d027b92

Please sign in to comment.