Skip to content
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

Bug fix + various code/logging improvements to retry code #643

Merged
merged 1 commit into from
Aug 1, 2018
Merged
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
53 changes: 35 additions & 18 deletions stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,34 +283,51 @@ func (s *BackendConfiguration) Do(req *http.Request, v interface{}) error {
s.Logger.Printf("Requesting %v %v%v\n", req.Method, req.URL.Host, req.URL.Path)
}

start := time.Now()

var res *http.Response
var err error
for retry := 0; ; {
start := time.Now()

res, err = s.HTTPClient.Do(req)
if s.shouldRetry(err, res, retry) {

if s.LogLevel > 2 {
s.Logger.Printf("Request completed in %v (retry: %v)\n",
time.Since(start), retry)
}

// If the response was okay, we're done, and it's safe to break out of
// the retry loop.
if !s.shouldRetry(err, res, retry) {
break
}

resBody, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
if s.LogLevel > 0 {
s.Logger.Printf("Request failed with error: %v. Response: %v\n", err, res)
s.Logger.Printf("Cannot read response: %v\n", err)
}
sleep := sleepTime(retry)
time.Sleep(sleep)
retry++
if s.LogLevel > 1 {
s.Logger.Printf("Retry request %v %v time.\n", req.URL, retry)
}
} else {
break
return err
}
}

if s.LogLevel > 2 {
s.Logger.Printf("Completed in %v\n", time.Since(start))
if s.LogLevel > 0 {
s.Logger.Printf("Request failed with: %s (error: %v)\n", string(resBody), err)
}

sleepDuration := sleepTime(retry)
retry++

if s.LogLevel > 1 {
s.Logger.Printf("Initiating retry %v for request %v %v%v after sleeping %v\n",
retry, req.Method, req.URL.Host, req.URL.Path, sleepDuration)
}

time.Sleep(sleepDuration)
}

if err != nil {
if s.LogLevel > 0 {
s.Logger.Printf("Request to Stripe failed: %v\n", err)
s.Logger.Printf("Request failed: %v\n", err)
}
return err
}
Expand All @@ -319,7 +336,7 @@ func (s *BackendConfiguration) Do(req *http.Request, v interface{}) error {
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
if s.LogLevel > 0 {
s.Logger.Printf("Cannot parse Stripe response: %v\n", err)
s.Logger.Printf("Cannot read response: %v\n", err)
}
return err
}
Expand All @@ -329,7 +346,7 @@ func (s *BackendConfiguration) Do(req *http.Request, v interface{}) error {
}

if s.LogLevel > 2 {
s.Logger.Printf("Stripe Response: %q\n", resBody)
s.Logger.Printf("Response: %s\n", string(resBody))
Copy link
Contributor

Choose a reason for hiding this comment

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

Changed the %q to a %s with a string(...) cast because the %q produces lots of noisy escaping that's not really needed for anything and detrimental to legibility:

Stripe Response: \"{\\\\"id\\\": \\\"ch_*\\\",\

}

if v != nil {
Expand Down