Skip to content

Commit

Permalink
api/middleware: avoid redirecting when the leader remains unchanged (#…
Browse files Browse the repository at this point in the history
…8228)

ref #7300

Avoid redirecting when the leader remains unchanged.

Signed-off-by: JmPotato <ghzpotato@gmail.com>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
JmPotato and ti-chi-bot[bot] authored May 30, 2024
1 parent 71490f7 commit 632cda4
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 632cda4

Please sign in to comment.