Skip to content

Commit

Permalink
Retry more kinds of google errors, handle duplicate sheets
Browse files Browse the repository at this point in the history
  • Loading branch information
Bowbaq committed Feb 10, 2020
1 parent 71cdd3f commit 5b1692b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
22 changes: 16 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net"
"time"

retry "github.com/avast/retry-go"
Expand Down Expand Up @@ -270,22 +271,31 @@ func NewServiceAccountClient(credsReader io.Reader) (*Client, error) {
func googleRetry(f func() error) error {
return retry.Do(
f,
retry.Delay(30*time.Second),
retry.Delay(15*time.Second),
retry.Attempts(5),
retry.RetryIf(func(err error) bool {
// Retry network errors, sometimes Google's API craps out
if _, ok := err.(*net.OpError); ok {
return true
}
if err == io.EOF {
return true
}

// Retry more specific Google API errors
if gerr, ok := err.(*googleapi.Error); ok {
switch {
// Too many requests
case gerr.Code == 429:
return true

case (gerr.Code >= 500 && gerr.Code <= 599):
return true

// Too many requests as a 403
case gerr.Code == 403 && gerr.Message == "Rate Limit Exceeded":
return true

default:
return false
// Server error. This may lead to duplicates, calling code must check for that
case (gerr.Code >= 500 && gerr.Code <= 599):
return true
}
}

Expand Down
26 changes: 23 additions & 3 deletions spreadsheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package sheets

import (
"bufio"
"errors"

"fmt"
"io"
"strings"

retry "github.com/avast/retry-go"
"github.com/pkg/errors"
"google.golang.org/api/googleapi"
sheets "google.golang.org/api/sheets/v4"
)

Expand Down Expand Up @@ -76,8 +79,8 @@ func (s *Spreadsheet) DuplicateSheet(title, newTitle string) (*Sheet, error) {
SourceSheetId: origin.Properties.SheetId,
},
})
if err != nil {
return nil, err
if err != nil && !isDuplicateSheetError(err) {
return nil, errors.Wrap(err, "couldn't duplicate sheet")
}

duplicate := s.GetSheet(newTitle)
Expand All @@ -88,6 +91,23 @@ func (s *Spreadsheet) DuplicateSheet(title, newTitle string) (*Sheet, error) {
return duplicate, nil
}

func isDuplicateSheetError(err error) bool {
rerr, ok := err.(retry.Error)
if !ok {
return false
}

for _, e := range rerr.WrappedErrors() {
if gerr, ok := e.(*googleapi.Error); ok {
if gerr.Code == 400 && strings.Contains(gerr.Message, "duplicateSheet") {
return true
}
}
}

return false
}

func (s *Sheet) Title() string {
return s.Properties.Title
}
Expand Down

0 comments on commit 5b1692b

Please sign in to comment.