X Tutup
Skip to content

Commit 9616706

Browse files
authored
review suggestion
1 parent 7656943 commit 9616706

File tree

2 files changed

+31
-39
lines changed

2 files changed

+31
-39
lines changed

pkg/cmd/codespace/ports.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ func (a *App) UpdatePortVisibility(ctx context.Context, codespaceName string, ar
275275
}
276276
defer safeClose(session, &err)
277277

278-
errc := make(chan error, 1)
278+
g, ctx := errgroup.WithContext(ctx)
279279

280280
// TODO: check if port visibility can be updated in parallel instead of sequentially
281281
for _, port := range ports {
@@ -285,36 +285,33 @@ func (a *App) UpdatePortVisibility(ctx context.Context, codespaceName string, ar
285285
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
286286
defer cancel()
287287

288-
go func(port portVisibility) {
288+
g.Go(func() error {
289289
updateNotif, err := session.WaitForPortNotification(ctx, port.number, liveshare.PortChangeKindUpdate)
290290
if err != nil {
291-
errc <- fmt.Errorf("error waiting for port %d to update: %w", port.number, err)
292-
return
291+
return fmt.Errorf("error waiting for port %d to update: %w", port.number, err)
292+
293293
}
294294
if !updateNotif.Success {
295295
if updateNotif.StatusCode == http.StatusForbidden {
296-
errc <- newErrUpdatingPortVisibility(port.number, port.visibility, errUpdatePortVisibilityForbidden)
297-
return
296+
return newErrUpdatingPortVisibility(port.number, port.visibility, errUpdatePortVisibilityForbidden)
298297
}
299-
errc <- newErrUpdatingPortVisibility(port.number, port.visibility, errors.New(updateNotif.ErrorDetail))
300-
return
301-
}
302-
errc <- nil // success
303-
}(port)
298+
return newErrUpdatingPortVisibility(port.number, port.visibility, errors.New(updateNotif.ErrorDetail))
304299

305-
err := session.UpdateSharedServerPrivacy(ctx, port.number, port.visibility)
306-
if err != nil {
307-
return fmt.Errorf("error updating port %d to %s: %w", port.number, port.visibility, err)
308-
}
300+
}
301+
return nil // success
302+
})
309303

310-
// wait for success or failure
311-
select {
312-
case <-ctx.Done():
313-
return ctx.Err()
314-
case err := <-errc:
304+
g.Go(func() error {
305+
err := session.UpdateSharedServerPrivacy(ctx, port.number, port.visibility)
315306
if err != nil {
316-
return err
307+
return fmt.Errorf("error updating port %d to %s: %w", port.number, port.visibility, err)
317308
}
309+
return nil
310+
})
311+
312+
// wait for success or failure
313+
if err := g.Wait(); err != nil {
314+
return err
318315
}
319316

320317
a.StopProgressIndicator()

pkg/liveshare/ports.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77

88
"github.com/sourcegraph/jsonrpc2"
9+
"golang.org/x/sync/errgroup"
910
)
1011

1112
// Port describes a port exposed by the container.
@@ -41,33 +42,27 @@ type PortUpdate struct {
4142
// It returns an identifier that can be used to open an SSH channel to the remote port.
4243
func (s *Session) startSharing(ctx context.Context, sessionName string, port int) (channelID, error) {
4344
args := []interface{}{port, sessionName, fmt.Sprintf("http://localhost:%d", port)}
44-
errc := make(chan error, 1)
45+
g, ctx := errgroup.WithContext(ctx)
4546

46-
go func() {
47+
g.Go(func() error {
4748
startNotification, err := s.WaitForPortNotification(ctx, port, PortChangeKindStart)
4849
if err != nil {
49-
errc <- fmt.Errorf("error while waiting for port notification: %w", err)
50-
return
50+
return fmt.Errorf("error while waiting for port notification: %w", err)
51+
5152
}
5253
if !startNotification.Success {
53-
errc <- fmt.Errorf("error while starting port sharing: %s", startNotification.ErrorDetail)
54-
return
54+
return fmt.Errorf("error while starting port sharing: %s", startNotification.ErrorDetail)
5555
}
56-
errc <- nil // success
57-
}()
56+
return nil // success
57+
})
5858

5959
var response Port
60-
if err := s.rpc.do(ctx, "serverSharing.startSharing", args, &response); err != nil {
61-
return channelID{}, err
62-
}
60+
g.Go(func() error {
61+
return s.rpc.do(ctx, "serverSharing.startSharing", args, &response)
62+
})
6363

64-
select {
65-
case <-ctx.Done():
66-
return channelID{}, ctx.Err()
67-
case err := <-errc:
68-
if err != nil {
69-
return channelID{}, err
70-
}
64+
if err := g.Wait(); err != nil {
65+
return channelID{}, err
7166
}
7267

7368
return channelID{response.StreamName, response.StreamCondition}, nil

0 commit comments

Comments
 (0)
X Tutup