Skip to content

feat(support): fetch data concurrently #12385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
do not early exit on error
  • Loading branch information
johnstcn committed Mar 5, 2024
commit a1e1dc4f21856ed2a3675d941a9a792dfb95f4a8
56 changes: 25 additions & 31 deletions support/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io"
"net/http"
"strings"
"sync"

"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"
Expand Down Expand Up @@ -64,8 +63,11 @@ type Deps struct {

func DeploymentInfo(ctx context.Context, client *codersdk.Client, log slog.Logger) Deployment {
// Note: each goroutine assigns to a different struct field, hence no mutex.
var d Deployment
eg, ctx := errgroup.WithContext(ctx)
var (
d Deployment
eg errgroup.Group
)

eg.Go(func() error {
bi, err := client.BuildInfo(ctx)
if err != nil {
Expand Down Expand Up @@ -110,9 +112,11 @@ func DeploymentInfo(ctx context.Context, client *codersdk.Client, log slog.Logge
}

func NetworkInfo(ctx context.Context, client *codersdk.Client, log slog.Logger, agentID uuid.UUID) Network {
var n Network
var (
n Network
eg errgroup.Group
)

eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
coordResp, err := client.Request(ctx, http.MethodGet, "/api/v2/debug/coordinator", nil)
if err != nil {
Expand Down Expand Up @@ -162,7 +166,10 @@ func NetworkInfo(ctx context.Context, client *codersdk.Client, log slog.Logger,
}

func WorkspaceInfo(ctx context.Context, client *codersdk.Client, log slog.Logger, workspaceID, agentID uuid.UUID) Workspace {
var w Workspace
var (
w Workspace
eg errgroup.Group
)

if workspaceID == uuid.Nil {
log.Error(ctx, "no workspace id specified")
Expand All @@ -181,8 +188,6 @@ func WorkspaceInfo(ctx context.Context, client *codersdk.Client, log slog.Logger
}
w.Workspace = ws

eg, ctx := errgroup.WithContext(ctx)

eg.Go(func() error {
agt, err := client.WorkspaceAgent(ctx, agentID)
if err != nil {
Expand Down Expand Up @@ -265,35 +270,24 @@ func Run(ctx context.Context, d *Deps) (*Bundle, error) {
}
}

var (
wg sync.WaitGroup
m sync.Mutex
)
wg.Add(1)
go func() {
defer wg.Done()
var eg errgroup.Group
eg.Go(func() error {
di := DeploymentInfo(ctx, d.Client, d.Log)
m.Lock()
b.Deployment = di
m.Unlock()
}()
wg.Add(1)
go func() {
defer wg.Done()
return nil
})
eg.Go(func() error {
wi := WorkspaceInfo(ctx, d.Client, d.Log, d.WorkspaceID, d.AgentID)
m.Lock()
b.Workspace = wi
m.Unlock()
}()
wg.Add(1)
go func() {
defer wg.Done()
return nil
})
eg.Go(func() error {
ni := NetworkInfo(ctx, d.Client, d.Log, d.AgentID)
m.Lock()
b.Network = ni
m.Unlock()
}()
wg.Wait()
return nil
})

_ = eg.Wait()

return &b, nil
}