Skip to content
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

Fix timeoutSeconds #15630

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions pkg/http/handler/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,8 @@ func (h *timeoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
case <-timeout.C():
timeoutDrained = true
if tw.tryTimeoutAndWriteError(h.body) {
return
}
tw.forceTimeoutAndWriteError(h.body)
return
case now := <-idleTimeoutCh:
timedOut, timeToNextTimeout := tw.tryIdleTimeoutAndWriteError(now, revIdleTimeout, h.body)
if timedOut {
Expand Down Expand Up @@ -214,22 +213,10 @@ func (tw *timeoutWriter) WriteHeader(code int) {
tw.w.WriteHeader(code)
}

// tryTimeoutAndWriteError writes an error to the responsewriter if
// nothing has been written to the writer before. Returns whether
// an error was written or not.
//
// If this writes an error, all subsequent calls to Write will
// result in http.ErrHandlerTimeout.
func (tw *timeoutWriter) tryTimeoutAndWriteError(msg string) bool {
func (tw *timeoutWriter) forceTimeoutAndWriteError(msg string) {
tw.mu.Lock()
defer tw.mu.Unlock()

if tw.lastWriteTime.IsZero() {
tw.timeoutAndWriteError(msg)
return true
}

return false
tw.timeoutAndWriteError(msg)
}

// tryResponseStartTimeoutAndWriteError writes an error to the responsewriter if
Expand Down
2 changes: 0 additions & 2 deletions pkg/http/handler/timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ func TestTimeoutWriterAllowsForAdditionalWritesBeforeTimeout(t *testing.T) {
clock := clock.RealClock{}
handler := &timeoutWriter{w: recorder, clock: clock}
handler.WriteHeader(http.StatusOK)
handler.tryTimeoutAndWriteError("error")
handler.tryResponseStartTimeoutAndWriteError("error")
handler.tryIdleTimeoutAndWriteError(clock.Now(), 10*time.Second, "error")
if _, err := io.WriteString(handler, "test"); err != nil {
t.Fatalf("handler.Write() = %v, want no error", err)
Expand Down
12 changes: 9 additions & 3 deletions test/conformance/api/v1/revision_timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (

// sendRequest send a request to "endpoint", returns error if unexpected response code, nil otherwise.
func sendRequest(t *testing.T, clients *test.Clients, endpoint *url.URL,
initialSleep, sleep time.Duration, expectedResponseCode int) error {
initialSleep, sleep time.Duration, expectedResponseCode int, expectedBody string) error {
client, err := pkgtest.NewSpoofingClient(context.Background(), clients.KubeClient, t.Logf, endpoint.Hostname(), test.ServingFlags.ResolvableDomain, test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS))
if err != nil {
return fmt.Errorf("error creating Spoofing client: %w", err)
Expand Down Expand Up @@ -68,7 +68,12 @@ func sendRequest(t *testing.T, clients *test.Clients, endpoint *url.URL,
if expectedResponseCode != resp.StatusCode {
return fmt.Errorf("response status code = %v, want = %v, response = %v", resp.StatusCode, expectedResponseCode, resp)
}

if expectedBody != "" {
gotBody := string(resp.Body)
if expectedBody != gotBody {
return fmt.Errorf("response body = %v, want = %v, response = %v", gotBody, expectedBody, resp)
}
}
return nil
}

Expand Down Expand Up @@ -99,6 +104,7 @@ func TestRevisionTimeout(t *testing.T) {
expectedStatus: http.StatusOK,
sleep: 15 * time.Second,
initialSleep: 0,
expectedBody: "activator request timeout",
}}

for _, tc := range testCases {
Expand Down Expand Up @@ -136,7 +142,7 @@ func TestRevisionTimeout(t *testing.T) {
t.Fatalf("Error probing %s: %v", serviceURL, err)
}

if err := sendRequest(t, clients, serviceURL, tc.initialSleep, tc.sleep, tc.expectedStatus); err != nil {
if err := sendRequest(t, clients, serviceURL, tc.initialSleep, tc.sleep, tc.expectedStatus, tc.expectedBody); err != nil {
t.Errorf("Failed request with initialSleep %v, sleep %v, with revision timeout %ds, expecting status %v: %v",
tc.initialSleep, tc.sleep, tc.timeoutSeconds, tc.expectedStatus, err)
}
Expand Down
Loading