@@ -76,15 +76,15 @@ func ports(opts *portsOptions) error {
7676
7777 devContainerCh := getDevContainer (ctx , apiClient , codespace )
7878
79- liveShareClient , err := codespaces .ConnectToLiveshare (ctx , log , apiClient , user .Login , token , codespace )
79+ session , err := codespaces .ConnectToLiveshare (ctx , log , apiClient , user .Login , token , codespace )
8080 if err != nil {
8181 return fmt .Errorf ("error connecting to Live Share: %v" , err )
8282 }
8383
8484 log .Println ("Loading ports..." )
85- ports , err := getPorts (ctx , liveShareClient )
85+ ports , err := session . GetSharedServers (ctx )
8686 if err != nil {
87- return fmt .Errorf ("error getting ports: %v" , err )
87+ return fmt .Errorf ("error getting ports of shared servers : %v" , err )
8888 }
8989
9090 devContainerResult := <- devContainerCh
@@ -116,20 +116,6 @@ func ports(opts *portsOptions) error {
116116 return nil
117117}
118118
119- func getPorts (ctx context.Context , lsclient * liveshare.Client ) (liveshare.Ports , error ) {
120- server , err := liveshare .NewServer (lsclient )
121- if err != nil {
122- return nil , fmt .Errorf ("error creating server: %v" , err )
123- }
124-
125- ports , err := server .GetSharedServers (ctx )
126- if err != nil {
127- return nil , fmt .Errorf ("error getting shared servers: %v" , err )
128- }
129-
130- return ports , nil
131- }
132-
133119type devContainerResult struct {
134120 devContainer * devContainer
135121 err error
@@ -219,22 +205,17 @@ func updatePortVisibility(log *output.Logger, codespaceName, sourcePort string,
219205 return fmt .Errorf ("error getting Codespace: %v" , err )
220206 }
221207
222- lsclient , err := codespaces .ConnectToLiveshare (ctx , log , apiClient , user .Login , token , codespace )
208+ session , err := codespaces .ConnectToLiveshare (ctx , log , apiClient , user .Login , token , codespace )
223209 if err != nil {
224210 return fmt .Errorf ("error connecting to Live Share: %v" , err )
225211 }
226212
227- server , err := liveshare .NewServer (lsclient )
228- if err != nil {
229- return fmt .Errorf ("error creating server: %v" , err )
230- }
231-
232213 port , err := strconv .Atoi (sourcePort )
233214 if err != nil {
234215 return fmt .Errorf ("error reading port number: %v" , err )
235216 }
236217
237- if err := server .UpdateSharedVisibility (ctx , port , public ); err != nil {
218+ if err := session .UpdateSharedVisibility (ctx , port , public ); err != nil {
238219 return fmt .Errorf ("error update port to public: %v" , err )
239220 }
240221
@@ -285,36 +266,36 @@ func forwardPorts(log *output.Logger, codespaceName string, ports []string) erro
285266 return fmt .Errorf ("error getting Codespace: %v" , err )
286267 }
287268
288- lsclient , err := codespaces .ConnectToLiveshare (ctx , log , apiClient , user .Login , token , codespace )
269+ session , err := codespaces .ConnectToLiveshare (ctx , log , apiClient , user .Login , token , codespace )
289270 if err != nil {
290271 return fmt .Errorf ("error connecting to Live Share: %v" , err )
291272 }
292273
293- server , err := liveshare .NewServer (lsclient )
294- if err != nil {
295- return fmt .Errorf ("error creating server: %v" , err )
296- }
297-
298274 g , gctx := errgroup .WithContext (ctx )
299275 for _ , portPair := range portPairs {
300276 pp := portPair
301277
278+ // TODO(adonovan): fix data race on Session between
279+ // StartSharing and NewPortForwarder.
302280 srcstr := strconv .Itoa (portPair .src )
303- if err := server .StartSharing (gctx , "share-" + srcstr , pp .src ); err != nil {
281+ if err := session .StartSharing (gctx , "share-" + srcstr , pp .src ); err != nil {
304282 return fmt .Errorf ("start sharing port: %v" , err )
305283 }
306284
307285 g .Go (func () error {
308286 log .Println ("Forwarding port: " + srcstr + " ==> " + strconv .Itoa (pp .dst ))
309- portForwarder := liveshare .NewPortForwarder (lsclient , server , pp .dst )
310- if err := portForwarder .Start (gctx ); err != nil {
287+ portForwarder := liveshare .NewPortForwarder (session , pp .dst )
288+ if err := portForwarder .Forward (gctx ); err != nil {
311289 return fmt .Errorf ("error forwarding port: %v" , err )
312290 }
313291
314292 return nil
315293 })
316294 }
317295
296+ // TODO(adonovan): fix: the waits for _all_ goroutines to terminate.
297+ // If there are multiple ports, one long-lived successful connection
298+ // will hide errors from any that fail.
318299 if err := g .Wait (); err != nil {
319300 return err
320301 }
0 commit comments