Skip to content

Commit

Permalink
Handle 503 HTTP code to schedule member change check
Browse files Browse the repository at this point in the history
Signed-off-by: JmPotato <[email protected]>
  • Loading branch information
JmPotato committed May 29, 2024
1 parent b7d8b94 commit 96b4cd9
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion client/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ func (ci *clientInner) requestWithRetry(
err error
)
execFunc := func() error {
// Handle some special status codes to increase the success rate of the following requests.
defer ci.handleHTTPStatusCode(statusCode)
// It will try to send the request to the PD leader first and then try to send the request to the other PD followers.
clients := ci.sd.GetAllServiceClients()
if len(clients) == 0 {
Expand Down Expand Up @@ -153,13 +155,21 @@ func (ci *clientInner) requestWithRetry(
}
// Copy a new backoffer for each request.
bo := *reqInfo.bo
// Backoffer also needs to check the status code to determine whether to retry.
// Set the retryable checker for the backoffer.
bo.SetRetryableChecker(func(err error) bool {
// Backoffer also needs to check the status code to determine whether to retry.
return err != nil && !noNeedRetry(statusCode)
})
return bo.Exec(ctx, execFunc)
}

func (ci *clientInner) handleHTTPStatusCode(code int) {
// If the status code is 503, it indicates that there may be PD leader/follower changes.
if code == http.StatusServiceUnavailable {
ci.sd.ScheduleCheckMemberChanged()

Check warning on line 169 in client/http/client.go

View check run for this annotation

Codecov / codecov/patch

client/http/client.go#L169

Added line #L169 was not covered by tests
}
}

func noNeedRetry(statusCode int) bool {
return statusCode == http.StatusNotFound ||
statusCode == http.StatusForbidden ||
Expand Down

0 comments on commit 96b4cd9

Please sign in to comment.