Skip to content

Commit

Permalink
Merge pull request #643 from stripe/brandur-retry-improvements
Browse files Browse the repository at this point in the history
Bug fix + various code/logging improvements to retry code
  • Loading branch information
brandur-stripe authored Aug 1, 2018
2 parents ec25373 + 3c4328a commit a2d0799
Showing 1 changed file with 35 additions and 18 deletions.
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))
}

if v != nil {
Expand Down

0 comments on commit a2d0799

Please sign in to comment.