Skip to content

Commit

Permalink
remove unnecessary chan in Downloader struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Lance Liu committed Jul 24, 2020
1 parent ddd4594 commit b6a98b0
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions plugins/admin/pkg/command/profiling/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ type DownloadOptions interface {
type Downloader struct {
podName string
namespace string
stopCh <-chan struct{} // Close this will trigger closing for the endCh create by our own and then cancel all sub goroutines
endCh chan struct{}
readyCh chan struct{}
readyCh chan struct{} // closed by portforward.ForwardPorts when connection is ready
stopCh chan struct{} // our private chan which can be closed by us safely
restConfig *rest.Config
localPort uint32
client *http.Client
Expand All @@ -71,20 +70,20 @@ func NewDownloader(cfg *rest.Config, podName, namespace string, endCh <-chan str
podName: podName,
namespace: namespace,
readyCh: make(chan struct{}),
stopCh: endCh,
stopCh: make(chan struct{}),
restConfig: cfg,
localPort: 18008,
client: http.DefaultClient,
}
err := d.connect()
err := d.connect(endCh)
if err != nil {
return nil, err
}
return d, nil
}

// non-blocking call to forward remote port in pod to localhost
func (d *Downloader) connect() error {
func (d *Downloader) connect(endCh <-chan struct{}) error {
path := fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/portforward", d.namespace, d.podName)
transport, upgrader, err := spdy.RoundTripperFor(d.restConfig)
if err != nil {
Expand All @@ -100,12 +99,12 @@ func (d *Downloader) connect() error {
Path: path,
}
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, http.MethodPost, url)
fw, err := portforward.New(dialer, []string{fmt.Sprintf("%d:%d", d.localPort, pprofPort)}, d.stopCh, d.readyCh, os.Stdout, os.Stderr)
fw, err := portforward.New(dialer, []string{fmt.Sprintf("%d:%d", d.localPort, pprofPort)}, endCh, d.readyCh, os.Stdout, os.Stderr)
if err != nil {
return err
}
go func() {
defer close(d.endCh)
defer close(d.stopCh)
err := fw.ForwardPorts()
// if the func ForwardPorts() returns, the connection should not be available.
if err != nil {
Expand Down Expand Up @@ -148,7 +147,7 @@ func (d *Downloader) Download(t ProfileType, output io.Writer, options ...Downlo
// request succeeded
case <-ctx.Done():
break
case <-d.endCh:
case <-d.stopCh:
cancel()
}
}()
Expand All @@ -171,7 +170,7 @@ func (d *Downloader) Download(t ProfileType, output io.Writer, options ...Downlo
return err
}
return nil
case <-d.endCh:
case <-d.stopCh:
return nil
}
}

0 comments on commit b6a98b0

Please sign in to comment.