Skip to content

Commit

Permalink
Retry errors on timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
mtibben committed Nov 2, 2015
1 parent 26aa14a commit e60a965
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions faulttolerant.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package smartling

import (
"log"
"net"
"strings"
"time"
)
Expand All @@ -22,8 +23,21 @@ func isNetworkErrClosing(err error) bool {
return false
}

func isTimeoutError(err error) bool {
if err != nil {
if netErr, ok := err.(net.Error); ok {
return netErr.Timeout()
}
}
return false
}

func isRetryableError(err error) bool {
return isResourceLockedError(err) || isNetworkErrClosing(err) || isTimeoutError(err)
}

// FaultTolerantClient decorates a Client and retries
// requests when Smartling returns with a Resource Locked error
// requests when Smartling returns with an error
type FaultTolerantClient struct {
*Client
RetriesOnError int
Expand All @@ -35,7 +49,7 @@ func (c *FaultTolerantClient) execWithRetry(f func() error) {

err := f()

for retries > 0 && (isResourceLockedError(err) || isNetworkErrClosing(err)) {
for retries > 0 && isRetryableError(err) {
log.Printf("%s, Retrying...\n", err.Error())

time.Sleep(backoff)
Expand Down

0 comments on commit e60a965

Please sign in to comment.