Skip to content

Commit

Permalink
refactor: extract retryable error check into a function
Browse files Browse the repository at this point in the history
  • Loading branch information
JadhavPoonam committed Feb 1, 2023
1 parent bf77e11 commit 42aece2
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions agent/consul/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,23 @@ func (c *limitedConn) Read(b []byte) (n int, err error) {
return c.lr.Read(b)
}

func hasRetryableError(err error) bool {
retryableMessages := []error{
// If we are chunking and it doesn't seem to have completed, try again.
ErrChunkingResubmit,

// These rate limit errors are returned before the handler is called, so are
// safe to retry.
rate.ErrRetryElsewhere,
}
for _, m := range retryableMessages {
if err != nil && strings.Contains(err.Error(), m.Error()) {
return true
}
}
return false
}

// canRetry returns true if the request and error indicate that a retry is safe.
func canRetry(info structs.RPCInfo, err error, start time.Time, config *Config) bool {
if info != nil {
Expand All @@ -579,18 +596,21 @@ func canRetry(info structs.RPCInfo, err error, start time.Time, config *Config)
return true
}

retryableMessages := []error{
// If we are chunking and it doesn't seem to have completed, try again.
ErrChunkingResubmit,

// These rate limit errors are returned before the handler is called, so are
// safe to retry.
rate.ErrRetryElsewhere,
}
for _, m := range retryableMessages {
if err != nil && strings.Contains(err.Error(), m.Error()) {
return true
}
// retryableMessages := []error{
// // If we are chunking and it doesn't seem to have completed, try again.
// ErrChunkingResubmit,

// // These rate limit errors are returned before the handler is called, so are
// // safe to retry.
// rate.ErrRetryElsewhere,
// }
// for _, m := range retryableMessages {
// if err != nil && strings.Contains(err.Error(), m.Error()) {
// return true
// }
// }
if hasRetryableError(err) {
return true
}

// Reads are safe to retry for stream errors, such as if a server was
Expand Down Expand Up @@ -746,7 +766,7 @@ CHECK_LEADER:
}
}
// defer to caller to retry
if rpcErr != rate.ErrRetryElsewhere {
if !errors.Is(rpcErr, rate.ErrRetryElsewhere) {
return true, rpcErr
}

Expand Down

0 comments on commit 42aece2

Please sign in to comment.