@@ -7,6 +7,7 @@ package apiv1
7
7
import (
8
8
"context"
9
9
"fmt"
10
+ "sync"
10
11
11
12
connect "github.com/bufbuild/connect-go"
12
13
"github.com/gitpod-io/gitpod/common-go/log"
@@ -95,15 +96,45 @@ func (s *TeamService) ListTeams(ctx context.Context, req *connect.Request[v1.Lis
95
96
return nil , proxy .ConvertError (err )
96
97
}
97
98
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 ))
99
106
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 {
102
127
log .WithError (err ).Error ("Failed to populate team with details." )
103
128
return nil , err
104
129
}
105
130
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 ])
107
138
}
108
139
109
140
return connect .NewResponse (& v1.ListTeamsResponse {
0 commit comments