use the same context for aggregated and proxy requests

This commit is contained in:
David Eads 2019-08-29 16:37:06 -04:00
parent 5521bf27c5
commit 275f5cf5a0
2 changed files with 7 additions and 19 deletions

View File

@ -19,7 +19,6 @@ package proxy
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
@ -222,15 +221,8 @@ func (h *UpgradeAwareHandler) ServeHTTP(w http.ResponseWriter, req *http.Request
h.Transport = h.defaultProxyTransport(req.URL, h.Transport)
}
// WithContext creates a shallow clone of the request with the new context.
ctx := context.Background()
// if the original request has a deadline, we should honor that deadline for our proxied request
if deadline, ok := req.Context().Deadline(); ok {
var cancelFn context.CancelFunc
ctx, cancelFn = context.WithDeadline(ctx, deadline)
defer cancelFn()
}
newReq := req.WithContext(ctx)
// WithContext creates a shallow clone of the request with the same context.
newReq := req.WithContext(req.Context())
newReq.Header = utilnet.CloneHeader(req.Header)
if !h.UseRequestLocation {
newReq.URL = &loc

View File

@ -178,23 +178,19 @@ func (r *proxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// newRequestForProxy returns a shallow copy of the original request with a context that may include a timeout for discovery requests
func newRequestForProxy(location *url.URL, req *http.Request, enableAggregatedDiscoveryTimeout bool) (*http.Request, context.CancelFunc) {
newCtx := context.Background()
newCtx := req.Context()
cancelFn := func() {}
// if the original request has a deadline, we should honor that deadline for our proxied request
if deadline, ok := req.Context().Deadline(); ok {
newCtx, cancelFn = context.WithDeadline(newCtx, deadline)
// trim leading and trailing slashes. Then "/apis/group/version" requests are for discovery, so if we have exactly three
// segments that we are going to proxy, we have a discovery request.
} else if enableAggregatedDiscoveryTimeout && len(strings.Split(strings.Trim(req.URL.Path, "/"), "/")) == 3 {
// trim leading and trailing slashes. Then "/apis/group/version" requests are for discovery, so if we have exactly three
// segments that we are going to proxy, we have a discovery request.
if enableAggregatedDiscoveryTimeout && len(strings.Split(strings.Trim(req.URL.Path, "/"), "/")) == 3 {
// discovery requests are used by kubectl and others to determine which resources a server has. This is a cheap call that
// should be fast for every aggregated apiserver. Latency for aggregation is expected to be low (as for all extensions)
// so forcing a short timeout here helps responsiveness of all clients.
newCtx, cancelFn = context.WithTimeout(newCtx, aggregatedDiscoveryTimeout)
}
// WithContext creates a shallow clone of the request with the new context.
// WithContext creates a shallow clone of the request with the same context.
newReq := req.WithContext(newCtx)
newReq.Header = utilnet.CloneHeader(req.Header)
newReq.URL = location