GoogleAPI: Add retries functionallity to GoogleAPI calls (#69129)

* Add retryer to GoogleAPI calls

* Add comment
This commit is contained in:
Dimitris Sotirakis 2023-05-26 12:59:56 +03:00 committed by GitHub
parent cebc8853f2
commit 515270f5fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -48,14 +48,25 @@ type File struct {
// New creates a new Client by checking for the Google Cloud SDK auth key and/or environment variable.
func New() (*Client, error) {
client, err := newClient()
storageClient, err := newClient()
if err != nil {
return nil, err
}
return &Client{
Client: *client,
}, nil
client := &Client{
Client: *storageClient,
}
client.SetRetryer()
return client, nil
}
// SetRetryer adds a retry strategy for the googleapi client calls that fail.
func (client *Client) SetRetryer() {
client.SetRetry([]storage.RetryOption{
storage.WithPolicy(storage.RetryAlways),
storage.WithErrorFunc(storage.ShouldRetry),
}...)
}
// newClient initializes the google-cloud-storage (GCS) client.
@ -148,8 +159,6 @@ func (client *Client) Copy(ctx context.Context, file File, bucket *storage.Bucke
return fmt.Errorf("failed to copy to Cloud Storage: %w", err)
}
log.Printf("Successfully uploaded tarball to Google Cloud Storage, path: %s/%s\n", remote, file.FullPath)
return nil
}
@ -258,7 +267,6 @@ func (client *Client) Delete(ctx context.Context, bucket *storage.BucketHandle,
if err := object.Delete(ctx); err != nil {
return fmt.Errorf("cannot delete %s, err: %w", path, err)
}
log.Printf("Successfully deleted tarball to Google Cloud Storage, path: %s", path)
return nil
}