X Tutup
Skip to content

Commit ca7e2d3

Browse files
authored
review suggestion: non-blocking send
1 parent a03e9d3 commit ca7e2d3

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

pkg/liveshare/ports.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,26 @@ type PortNotification struct {
7777
// or an error if the notification is not received before the context is cancelled or it fails
7878
// to parse the notification.
7979
func (s *Session) WaitForPortNotification(ctx context.Context, port int, notifType PortChangeKind) (*PortNotification, error) {
80-
notificationUpdate := make(chan PortNotification, 1)
81-
errc := make(chan error, 1)
80+
notificationCh := make(chan *PortNotification, 1)
81+
errCh := make(chan error, 1)
8282

8383
h := func(success bool) func(*jsonrpc2.Conn, *jsonrpc2.Request) {
8484
return func(conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
85-
var notification PortNotification
85+
notification := new(PortNotification)
8686
if err := json.Unmarshal(*req.Params, &notification); err != nil {
87-
errc <- fmt.Errorf("error unmarshaling notification: %w", err)
87+
select {
88+
case errCh <- fmt.Errorf("error unmarshaling notification: %w", err):
89+
default:
90+
}
8891
return
8992
}
9093
notification.Success = success
91-
notificationUpdate <- notification
94+
if notification.Port == port && notification.ChangeKind == notifType {
95+
select {
96+
case notificationCh <- notification:
97+
default:
98+
}
99+
}
92100
}
93101
}
94102
deregisterSuccess := s.registerRequestHandler("serverSharing.sharingSucceeded", h(true))
@@ -100,12 +108,10 @@ func (s *Session) WaitForPortNotification(ctx context.Context, port int, notifTy
100108
select {
101109
case <-ctx.Done():
102110
return nil, ctx.Err()
103-
case err := <-errc:
111+
case err := <-errCh:
104112
return nil, err
105-
case notification := <-notificationUpdate:
106-
if notification.Port == port && notification.ChangeKind == notifType {
107-
return &notification, nil
108-
}
113+
case notification := <-notificationCh:
114+
return notification, nil
109115
}
110116
}
111117
}

0 commit comments

Comments
 (0)
X Tutup