/
retry.go
32 lines (27 loc) · 952 Bytes
/
retry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package internal
import (
"time"
"github.com/lestrrat-go/backoff"
"google.golang.org/api/googleapi"
)
var defaultRetryPolicy = backoff.NewExponential(
backoff.WithInterval(3*time.Second),
backoff.WithMaxRetries(5),
)
// IsRetryableError returns true if the error is retryable,
// such as status code is 5xx or network error occurs.
// Otherwise returns false.
// See https://developers.google.com/photos/library/guides/best-practices#retrying-failed-requests
func IsRetryableError(err error) bool {
if apiErr, ok := err.(*googleapi.Error); ok {
return IsRetryableStatusCode(apiErr.Code)
}
return true
}
// IsRetryableStatusCode returns true if the status code is retryable,
// such as status code is 5xx or network error occurs.
// Otherwise returns false.
// See https://developers.google.com/photos/library/guides/best-practices#retrying-failed-requests
func IsRetryableStatusCode(code int) bool {
return code >= 500 && code <= 599
}