From: Florian Forster Date: Mon, 22 Jan 2018 07:37:22 +0000 (+0100) Subject: Write Google Fit entries concurrently. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=865506366bdebb66328062c1d62579853bba060c;p=kraftakt.git Write Google Fit entries concurrently. --- diff --git a/gfitsync.go b/gfitsync.go index 28c0685..4ff1bab 100644 --- a/gfitsync.go +++ b/gfitsync.go @@ -6,6 +6,7 @@ import ( "fmt" "io/ioutil" "net/http" + "sync" "time" "github.com/octo/gfitsync/app" @@ -251,29 +252,49 @@ func handleNotification(ctx context.Context, s *fitbit.Subscription) error { return err } - if err := gfitClient.SetSteps(ctx, summary.Summary.Steps, tm); err != nil { - return fmt.Errorf("gfitClient.SetSteps(%d) = %v", summary.Summary.Steps, err) - } + var errs appengine.MultiError + wg := &sync.WaitGroup{} - if err := gfitClient.SetCalories(ctx, summary.Summary.CaloriesOut, tm); err != nil { - return fmt.Errorf("gfitClient.SetCalories(%d) = %v", summary.Summary.CaloriesOut, err) - } + wg.Add(1) + go func() { + if err := gfitClient.SetSteps(ctx, summary.Summary.Steps, tm); err != nil { + errs = append(errs, fmt.Errorf("gfitClient.SetSteps(%d) = %v", summary.Summary.Steps, err)) + } + wg.Done() + }() - var distanceMeters float64 - for _, d := range summary.Summary.Distances { - if d.Activity != "total" { - continue + wg.Add(1) + go func() { + if err := gfitClient.SetCalories(ctx, summary.Summary.CaloriesOut, tm); err != nil { + errs = append(errs, fmt.Errorf("gfitClient.SetCalories(%d) = %v", summary.Summary.CaloriesOut, err)) } - distanceMeters = 1000.0 * d.Distance - break - } - if err := gfitClient.SetDistance(ctx, distanceMeters, tm); err != nil { - return fmt.Errorf("gfitClient.SetDistance(%d) = %v", distanceMeters, err) - } + wg.Done() + }() + + wg.Add(1) + go func() { + var distanceMeters float64 + for _, d := range summary.Summary.Distances { + if d.Activity != "total" { + continue + } + distanceMeters = 1000.0 * d.Distance + break + } + if err := gfitClient.SetDistance(ctx, distanceMeters, tm); err != nil { + errs = append(errs, fmt.Errorf("gfitClient.SetDistance(%d) = %v", distanceMeters, err)) + } + wg.Done() + }() - if err := gfitClient.SetHeartRate(ctx, summary.Summary.HeartRateZones, summary.Summary.RestingHeartRate, tm); err != nil { - return fmt.Errorf("gfitClient.SetHeartRate() = %v", err) - } + wg.Add(1) + go func() { + if err := gfitClient.SetHeartRate(ctx, summary.Summary.HeartRateZones, summary.Summary.RestingHeartRate, tm); err != nil { + errs = append(errs, fmt.Errorf("gfitClient.SetHeartRate() = %v", err)) + } + wg.Done() + }() - return nil + wg.Wait() + return errs }