Skip to content

Commit 41ca7df

Browse files
committed
fix(client): Client to handle the random 'context deadline exceeded encounters by simple retrying those requests up to MAX_CONTEXT_DEADLINE_RETRIES times with exponential backoff up to 30 seconds between request.
1 parent f1b7dbe commit 41ca7df

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

v2/api/client.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,29 @@ func (c *Client) sendRequest(request *request) (*http.Response, error) {
179179
}
180180

181181
resp, respErr := c.httpClient.Do(req)
182-
if respErr != nil {
182+
183+
// check if context deadline exceeded
184+
if respErr != nil && strings.Contains(respErr.Error(), "context deadline exceeded") || http.StatusRequestTimeout == resp.StatusCode {
185+
// retry until max retries reached
186+
sleepDuration := time.Duration(1) * time.Second
187+
for i := 0; i < MAX_CONTEXT_DEADLINE_RETRIES; i++ {
188+
// sleep for exponential backoff
189+
if i > 0 {
190+
sleepDuration = sleepDuration * 2
191+
if sleepDuration > time.Duration(MAX_WAIT_SECONDS)*time.Second {
192+
sleepDuration = time.Duration(MAX_WAIT_SECONDS) * time.Second
193+
}
194+
log.Printf("[DEBUG] %s request to %s failed with error %s, retrying in %s seconds...", request.Method, keyfactorPath, respErr.Error(), sleepDuration)
195+
time.Sleep(sleepDuration)
196+
}
197+
198+
log.Printf("[DEBUG] %s request to %s failed with error %s, retrying...", request.Method, keyfactorPath, respErr.Error())
199+
resp, respErr = c.httpClient.Do(req)
200+
if respErr == nil {
201+
break
202+
}
203+
}
204+
} else if respErr != nil {
183205
return nil, respErr
184206
}
185207
var stringMessage string

v2/api/constants.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package api
22

33
const (
4-
MAX_ITERATIONS = 100000
5-
MAX_WAIT_SECONDS = 30
4+
MAX_ITERATIONS = 100000
5+
MAX_WAIT_SECONDS = 30
6+
MAX_CONTEXT_DEADLINE_RETRIES = 5
67
)

0 commit comments

Comments
 (0)