Skip to content
This repository has been archived by the owner on Jun 24, 2020. It is now read-only.

Use WaitGroup and goroutines to parallelize enabling GCP APIs. #203

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions installer/pkg/gcp/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os/exec"
"strconv"
"strings"
"sync"
"time"
)

Expand All @@ -38,19 +39,36 @@ func EnableAPIs(apis []string) error {
if err != nil {
return err
}
//use WaitGroup to wait for every enable API call to finish
var wg sync.WaitGroup
errs := make(chan error, 1)

for _, api := range apis {
if _, found := existingAPIs[api]; !found {
// Each enableAPI() can take more than a minute, so we want to show the status per API.
fmt.Printf("enabling a GCP API: %s\n", api)
err = enableAPI(api)
if err != nil {
return err
}
wg.Add(1)
go func(api string) {
defer wg.Done()
err = enableAPI(api)
if err != nil {
errs <- err
} else {
//Finish response for whenever an API has successfully returned
fmt.Printf("Enabled GCP API: %s\n", api)
}
}(api)
}
}

return nil
//Block until all goroutines finish. Check if any errors were returned
wg.Wait()
select {
case err := <-errs:
return err
default:
return nil
}
}

// enabledAPIs returned set of enabled GCP APIs.
Expand Down