Immediate close the other half of the connection when proxying

- don't wait for the both sides of the connection to close on their
   own. If one closes there doesn't seem to be any point in continuing
   the other half.
 - for port-forward, this prevents the api-server and kubelet both
   waiting on the other with no result. Normally the connection
   eventually times out. This cleans up the connection appropriately.
 - add a log message on closing the connection that corresponds to
   opening the connection
This commit is contained in:
Morgan Bauer 2018-08-10 18:23:30 -07:00
parent cf89c466cc
commit 28376c7032
No known key found for this signature in database
GPG Key ID: 262BF1E28B0B5BE6

View File

@ -27,7 +27,6 @@ import (
"net/http/httputil"
"net/url"
"strings"
"sync"
"time"
"k8s.io/apimachinery/pkg/api/errors"
@ -294,9 +293,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
@ -309,7 +311,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() {
@ -323,10 +325,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
}