Write Google Fit entries concurrently.
authorFlorian Forster <ff@octo.it>
Mon, 22 Jan 2018 07:37:22 +0000 (08:37 +0100)
committerFlorian Forster <ff@octo.it>
Mon, 22 Jan 2018 07:37:22 +0000 (08:37 +0100)
gfitsync.go

index 28c0685..4ff1bab 100644 (file)
@@ -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
 }