Skip to content

[minor_change] Add hardcoded retry mechanism for json parse errors of response in client #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 34 additions & 20 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,30 +353,44 @@ func (c *Client) Do(req *http.Request) (*container.Container, *http.Response, er
if !c.skipLoggingPayload {
log.Printf("[TRACE] HTTP Request Body: %v", req.Body)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, nil, err
}
log.Printf("[DEBUG] HTTP Request: %s %s", req.Method, req.URL.String())
log.Printf("[DEBUG] HTTP Response: %d %s %v", resp.StatusCode, resp.Status, resp)

bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyStr := string(bodyBytes)
resp.Body.Close()
log.Printf("[DEBUG] HTTP response unique string %s %s %s", req.Method, req.URL.String(), bodyStr)
if req.Method != "DELETE" && resp.StatusCode != 204 {
obj, err := container.ParseJSON(bodyBytes)

// Attempts are used to retry the request in case of a JSON parse failure
for attempts := 1; ; attempts++ {
log.Printf("[DEBUG] HTTP Request: %s %s", req.Method, req.URL.String())
resp, err := c.httpClient.Do(req)
if err != nil {
log.Printf("Error occured while json parsing %+v", err)
log.Printf("[ERROR] Error occured on HTTP Request: %s", err)
return nil, nil, err
}
log.Printf("[DEBUG] HTTP Response: %d %s %v", resp.StatusCode, resp.Status, resp)

bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyStr := string(bodyBytes)
resp.Body.Close()
log.Printf("[DEBUG] HTTP response unique string %s %s %s", req.Method, req.URL.String(), bodyStr)
if req.Method != "DELETE" && resp.StatusCode != 204 {
obj, err := container.ParseJSON(bodyBytes)
if err != nil {
// With large schemas errors are sometimes noticed for JSON parsing
// Recreate is not consistent accross attempt, where some schemas that failed are parsed correctly next run
// According to CX investigation is related to retransmissions and lost packets
// Workaround for https://github.com/CiscoDevNet/terraform-provider-mso/issues/338
if ok := attempts <= 3; ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we add Exponential backoff-> The delay increases exponentially after each attempt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is pending discussion from team

log.Printf("[ERROR] Error occured while parsing JSON, retrying failed HTTP Request with StatusCode %v, Retries Executed: %v", resp.StatusCode, attempts)
continue
}
log.Printf("[ERROR] HTTP Request Method and URL: %s %s", req.Method, req.URL.String())
log.Printf("[ERROR] HTTP Response: %d %s %v", resp.StatusCode, resp.Status, resp)
log.Printf("[ERROR] Error occured while json parsing %+v", err)
return nil, resp, err
}
log.Printf("[DEBUG] Exit from do method")
return obj, resp, err
} else if resp.StatusCode == 204 {
return nil, nil, nil
} else {
return nil, resp, err
}
log.Printf("[DEBUG] Exit from do method")
return obj, resp, err
} else if resp.StatusCode == 204 {
return nil, nil, nil
} else {
return nil, resp, err
}
}

Expand Down