Skip to content

Commit 6291b6c

Browse files
authored
[public-api] List teams concurrently (#16848)
* [public-api] List teams concurrently * Fix
1 parent 0819f99 commit 6291b6c

File tree

1 file changed

+35
-4
lines changed
  • components/public-api-server/pkg/apiv1

1 file changed

+35
-4
lines changed

components/public-api-server/pkg/apiv1/team.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package apiv1
77
import (
88
"context"
99
"fmt"
10+
"sync"
1011

1112
connect "github.com/bufbuild/connect-go"
1213
"github.com/gitpod-io/gitpod/common-go/log"
@@ -95,15 +96,45 @@ func (s *TeamService) ListTeams(ctx context.Context, req *connect.Request[v1.Lis
9596
return nil, proxy.ConvertError(err)
9697
}
9798

98-
var response []*v1.Team
99+
type result struct {
100+
team *v1.Team
101+
err error
102+
}
103+
104+
wg := sync.WaitGroup{}
105+
resultsChan := make(chan result, len(teams))
99106
for _, t := range teams {
100-
team, err := s.toTeamAPIResponse(ctx, conn, t)
101-
if err != nil {
107+
wg.Add(1)
108+
go func(t *protocol.Team) {
109+
team, err := s.toTeamAPIResponse(ctx, conn, t)
110+
resultsChan <- result{
111+
team: team,
112+
err: err,
113+
}
114+
defer wg.Done()
115+
}(t)
116+
}
117+
118+
// Block until we've fetched all teams
119+
wg.Wait()
120+
close(resultsChan)
121+
122+
// We want to maintain the order of results that we got from server
123+
// So we convert our concurrent results to a map, so we can index into it
124+
resultMap := map[string]*v1.Team{}
125+
for res := range resultsChan {
126+
if res.err != nil {
102127
log.WithError(err).Error("Failed to populate team with details.")
103128
return nil, err
104129
}
105130

106-
response = append(response, team)
131+
resultMap[res.team.GetId()] = res.team
132+
}
133+
134+
// Map the original order of teams against the populated results
135+
var response []*v1.Team
136+
for _, t := range teams {
137+
response = append(response, resultMap[t.ID])
107138
}
108139

109140
return connect.NewResponse(&v1.ListTeamsResponse{

0 commit comments

Comments
 (0)