Skip to content

Commit

Permalink
Merge pull request #68042 from tossmilestone/automated-cherry-pick-of…
Browse files Browse the repository at this point in the history
…-#67288-upstream-release-1.10

Automated cherry pick of #67288: Immediate close the other half of the connection when
  • Loading branch information
k8s-ci-robot committed Dec 12, 2018
2 parents 54a061a + 6ca6096 commit a1bf62a
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions staging/src/k8s.io/apimachinery/pkg/util/proxy/upgradeaware.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"net/http/httputil"
"net/url"
"strings"
"sync"
"time"

"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -323,9 +322,12 @@ func (h *UpgradeAwareHandler) tryUpgrade(w http.ResponseWriter, req *http.Reques
}
}

// Proxy the connection.
wg := &sync.WaitGroup{}
wg.Add(2)
// Proxy the connection. This is bidirectional, so we need a goroutine
// to copy in each direction. Once one side of the connection exits, we
// exit the function which performs cleanup and in the process closes
// the other half of the connection in the defer.
writerComplete := make(chan struct{})
readerComplete := make(chan struct{})

go func() {
var writer io.WriteCloser
Expand All @@ -338,7 +340,7 @@ func (h *UpgradeAwareHandler) tryUpgrade(w http.ResponseWriter, req *http.Reques
if err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
glog.Errorf("Error proxying data from client to backend: %v", err)
}
wg.Done()
close(writerComplete)
}()

go func() {
Expand All @@ -352,10 +354,17 @@ func (h *UpgradeAwareHandler) tryUpgrade(w http.ResponseWriter, req *http.Reques
if err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
glog.Errorf("Error proxying data from backend to client: %v", err)
}
wg.Done()
close(readerComplete)
}()

wg.Wait()
// Wait for one half the connection to exit. Once it does the defer will
// clean up the other half of the connection.
select {
case <-writerComplete:
case <-readerComplete:
}
glog.V(6).Infof("Disconnecting from backend proxy %s\n Headers: %v", &location, clone.Header)

return true
}

Expand Down

0 comments on commit a1bf62a

Please sign in to comment.