Exit early if context is marked as done (#2883)

Keeps the code from blocking and then failing immediately without sending a retry.

---

#### Does this PR need a docs update or release note?

- [ ]  Yes, it's included
- [ ] 🕐 Yes, but in a later PR
- [x]  No

#### Type of change

- [ ] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Supportability/Tests
- [ ] 💻 CI/Deployment
- [x] 🧹 Tech Debt/Cleanup

#### Issue(s)

* closes #2882

#### Test Plan

- [ ] 💪 Manual
- [ ]  Unit test
- [ ] 💚 E2E
This commit is contained in:
ashmrtn 2023-04-10 11:38:14 -07:00 committed by GitHub
parent b6ae9c6d07
commit 27909592b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -42,9 +42,20 @@ func (middleware RetryHandler) retryRequest(
cumulativeDelay += delay
req.Header.Set(retryAttemptHeader, strconv.Itoa(executionCount))
timer := time.NewTimer(delay)
time.Sleep(delay)
select {
case <-ctx.Done():
// Don't retry if the context is marked as done, it will just error out
// when we attempt to send the retry anyway.
return resp, ctx.Err()
// Will exit switch-block so the remainder of the code doesn't need to be
// indented.
case <-timer.C:
}
req.Header.Set(retryAttemptHeader, strconv.Itoa(executionCount))
response, err := pipeline.Next(req, middlewareIndex)
if err != nil && !IsErrTimeout(err) {