From: Florian Forster Date: Fri, 2 Feb 2018 07:48:07 +0000 (+0100) Subject: Packages app and gfit: Unify HTTP retries with retry.Transport. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=1ce3c0a76452d49dc65e4321b6746c5ae3e8982f;p=kraftakt.git Packages app and gfit: Unify HTTP retries with retry.Transport. All retrying logic in the gfit package is no longer needed and has been reverted. --- diff --git a/app/user.go b/app/user.go index 563f449..63cd086 100644 --- a/app/user.go +++ b/app/user.go @@ -10,6 +10,7 @@ import ( "sync" "github.com/google/uuid" + "github.com/octo/retry" legacy_context "golang.org/x/net/context" "golang.org/x/oauth2" "google.golang.org/appengine/datastore" @@ -107,12 +108,17 @@ func (u *User) OAuthClient(ctx context.Context, svc string, cfg *oauth2.Config) } src := cfg.TokenSource(ctx, &tok) - return oauth2.NewClient(ctx, &persistingTokenSource{ + c := oauth2.NewClient(ctx, &persistingTokenSource{ ctx: ctx, t: &tok, src: src, key: key, - }), nil + }) + c.Transport = retry.Transport{ + RoundTripper: c.Transport, + } + + return c, nil } func (u *User) String() string { diff --git a/gfit/gfit.go b/gfit/gfit.go index 36e5d5f..9a1c6f4 100644 --- a/gfit/gfit.go +++ b/gfit/gfit.go @@ -9,7 +9,6 @@ import ( "github.com/octo/kraftakt/app" "github.com/octo/kraftakt/fitbit" - "github.com/octo/retry" "golang.org/x/oauth2" oauth2google "golang.org/x/oauth2/google" fitness "google.golang.org/api/fitness/v1" @@ -119,28 +118,8 @@ func DataStreamID(dataSource *fitness.DataSource) string { return strings.Join(fields, ":") } -func wrapGoogleError(err error) error { - if err == nil { - return nil - } - - if gerr, ok := err.(*googleapi.Error); ok && gerr.Code >= 400 && gerr.Code < 500 { - return retry.Abort(err) - } - - return err -} - func (c *Client) DataSourceCreate(ctx context.Context, dataSource *fitness.DataSource) (string, error) { - var dataStreamID string - err := retry.Do(ctx, func(ctx context.Context) error { - res, err := c.Service.Users.DataSources.Create(userID, dataSource).Context(ctx).Do() - if err != nil { - return wrapGoogleError(err) - } - dataStreamID = res.DataStreamId - return nil - }) + res, err := c.Service.Users.DataSources.Create(userID, dataSource).Context(ctx).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == http.StatusConflict { if dataSource.DataStreamId != "" { @@ -148,29 +127,20 @@ func (c *Client) DataSourceCreate(ctx context.Context, dataSource *fitness.DataS } return DataStreamID(dataSource), nil } - log.Errorf(ctx, "c.Service.Users.DataSources.Create(%q) = %v", DataStreamID(dataSource), err) - return "", err + return "", fmt.Errorf("DataSources.Create(%q) = %v", DataStreamID(dataSource), err) } - return dataStreamID, nil + + return res.DataStreamId, nil } -func (c *Client) DatasetGet(ctx context.Context, dataStreamID string, startTime, endTime time.Time) (*fitness.Dataset, error) { +func (c *Client) DatasetGet(ctx context.Context, dataSourceID string, startTime, endTime time.Time) (*fitness.Dataset, error) { datasetID := fmt.Sprintf("%d-%d", startTime.UnixNano(), endTime.UnixNano()) - var dataset *fitness.Dataset - err := retry.Do(ctx, func(ctx context.Context) error { - res, err := c.Service.Users.DataSources.Datasets.Get(userID, dataStreamID, datasetID).Context(ctx).Do() - if err != nil { - return wrapGoogleError(err) - } - dataset = res - return nil - }) + res, err := c.Service.Users.DataSources.Datasets.Get(userID, dataSourceID, datasetID).Context(ctx).Do() if err != nil { - log.Errorf(ctx, "c.Service.Users.DataSources.Datasets.Get(%q, %q) = %v", dataStreamID, datasetID, err) - return nil, err + return nil, fmt.Errorf("DataSources.Datasets.Get(%q, %q) = %v", dataSourceID, datasetID, err) } - return dataset, nil + return res, nil } func (c *Client) DatasetPatch(ctx context.Context, dataSourceID string, points []*fitness.DataPoint) error { @@ -192,12 +162,9 @@ func (c *Client) DatasetPatch(ctx context.Context, dataSourceID string, points [ Point: points, } - err := retry.Do(ctx, func(ctx context.Context) error { - _, err := c.Service.Users.DataSources.Datasets.Patch(userID, dataSourceID, datasetID, dataset).Context(ctx).Do() - return wrapGoogleError(err) - }) + _, err := c.Service.Users.DataSources.Datasets.Patch(userID, dataSourceID, datasetID, dataset).Context(ctx).Do() if err != nil { - log.Errorf(ctx, "c.Service.Users.DataSources.Datasets.Patch() = %v", err) + log.Errorf(ctx, "DataSources.Datasets.Patch(%q, %q) = %v", dataSourceID, datasetID, err) return err } return nil