-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support http1 full duplex per workload #14568
Changes from 1 commit
5304ed6
94e1651
71c220d
5b21d8e
7ea1974
77f79ee
fbe2861
c88d865
7d436e4
558ae15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,16 +18,22 @@ package handler | |
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"sync" | ||
"testing" | ||
|
||
netprobe "knative.dev/networking/pkg/http/probe" | ||
"knative.dev/pkg/logging" | ||
pkgnet "knative.dev/pkg/network" | ||
rtesting "knative.dev/pkg/reconciler/testing" | ||
"knative.dev/serving/pkg/activator" | ||
apiconfig "knative.dev/serving/pkg/apis/config" | ||
asmetrics "knative.dev/serving/pkg/autoscaler/metrics" | ||
pkghttp "knative.dev/serving/pkg/http" | ||
) | ||
|
@@ -114,3 +120,152 @@ func BenchmarkHandlerChain(b *testing.B) { | |
}) | ||
}) | ||
} | ||
|
||
func TestProxyWithChainHandler(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the issue reproducer utilizing the activator's handler. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reading this test in isolation it's not clear what it is doing - can you add a detailed comment and maybe link out to the knative and golang issue? |
||
ctx, cancel, _ := rtesting.SetupFakeContextWithCancel(t) | ||
rev := revision(testNamespace, testRevName) | ||
rev.Annotations = map[string]string{apiconfig.AllowHTTPFullDuplexFeatureKey: "Enabled"} | ||
t.Cleanup(cancel) | ||
|
||
logger := logging.FromContext(ctx) | ||
configStore := setupConfigStore(t, logger) | ||
revisionInformer(ctx, rev) | ||
|
||
// Buffer equal to the activator. | ||
statCh := make(chan []asmetrics.StatMessage) | ||
concurrencyReporter := NewConcurrencyReporter(ctx, activatorPodName, statCh) | ||
go concurrencyReporter.Run(ctx.Done()) | ||
|
||
// Just read and ignore all stat messages. | ||
go func() { | ||
for { | ||
select { | ||
case <-statCh: | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
}() | ||
|
||
// The server responding with the sent body. | ||
echoServer := httptest.NewServer(http.HandlerFunc( | ||
func(w http.ResponseWriter, req *http.Request) { | ||
body, err := ioutil.ReadAll(req.Body) | ||
if err != nil { | ||
log.Printf("error reading body: %v", err) | ||
http.Error(w, fmt.Sprintf("error reading body: %v", err), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if _, err := w.Write(body); err != nil { | ||
log.Printf("error writing body: %v", err) | ||
} | ||
}, | ||
)) | ||
defer echoServer.Close() | ||
|
||
// The server proxying requests to the echo server. | ||
echoURL, err := url.Parse(echoServer.URL) | ||
if err != nil { | ||
t.Fatalf("Failed to parse echo URL: %v", err) | ||
} | ||
|
||
proxy := pkghttp.NewHeaderPruningReverseProxy(echoURL.Host, "", []string{}, false) | ||
proxy.FlushInterval = 0 | ||
proxyWithMiddleware := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
proxy.ServeHTTP(w, r) | ||
}) | ||
var ah http.Handler | ||
ah = concurrencyReporter.Handler(proxyWithMiddleware) | ||
ah = NewTracingHandler(ah) | ||
ah, _ = pkghttp.NewRequestLogHandler(ah, io.Discard, "", nil, false) | ||
ah = NewMetricHandler(activatorPodName, ah) | ||
ah = WrapActivatorHandlerWithFullDuplex(ah, logger) | ||
ah = NewContextHandler(ctx, ah, configStore) | ||
ah = &ProbeHandler{NextHandler: ah} | ||
ah = netprobe.NewHandler(ah) | ||
ah = &HealthHandler{HealthCheck: func() error { return nil }, NextHandler: ah, Logger: logger} | ||
|
||
bodySize := 32 * 1024 | ||
parallelism := 32 | ||
|
||
proxyServer := httptest.NewServer(ah) | ||
|
||
defer proxyServer.Close() | ||
|
||
transport := http.DefaultTransport.(*http.Transport).Clone() | ||
|
||
// Turning on this will hide the issue | ||
transport.DisableKeepAlives = false | ||
c := &http.Client{ | ||
Transport: transport, | ||
} | ||
|
||
body := make([]byte, bodySize) | ||
for i := 0; i < cap(body); i++ { | ||
body[i] = 42 | ||
} | ||
|
||
for i := 0; i < 10; i++ { | ||
var wg sync.WaitGroup | ||
wg.Add(parallelism) | ||
for i := 0; i < parallelism; i++ { | ||
go func(i int) { | ||
defer wg.Done() | ||
|
||
for i := 0; i < 1000; i++ { | ||
if err := send(c, proxyServer.URL, body, "test-host"); err != nil { | ||
t.Errorf("error during request: %v", err) | ||
} | ||
} | ||
}(i) | ||
} | ||
|
||
wg.Wait() | ||
} | ||
|
||
} | ||
|
||
func send(client *http.Client, url string, body []byte, rHost string) error { | ||
r := bytes.NewBuffer(body) | ||
req, err := http.NewRequest("POST", url, r) | ||
|
||
if rHost != "" { | ||
req.Host = rHost | ||
} | ||
|
||
req.Header.Set(activator.RevisionHeaderNamespace, testNamespace) | ||
req.Header.Set(activator.RevisionHeaderName, testRevName) | ||
|
||
if err != nil { | ||
return fmt.Errorf("failed to create request: %w", err) | ||
} | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("failed to execute request: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
bd := io.Reader(resp.Body) | ||
|
||
rec, err := ioutil.ReadAll(bd) | ||
|
||
if err != nil { | ||
return fmt.Errorf("failed to read body: %w", err) | ||
} | ||
|
||
if _, err = io.Copy(io.Discard, resp.Body); err != nil { | ||
return fmt.Errorf("failed to discard body: %w", err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
} | ||
|
||
if len(rec) != len(body) { | ||
return fmt.Errorf("unexpected body length: %d", len(rec)) | ||
} | ||
|
||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine as the handler that consumes the body is not run yet. The error we try to eliminate is btw:
Even without the activator wrapper the error at the activator side happens vary rarely, unlike the QP side.