-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Shutdown Websocket Connections Gracefully in the queue-proxy #16362
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
81dc57d
include new hijack handler to help drain hijacked requests by polling
dprotaso 4845575
Update echo websocket server to gracefully exit on term signal
dprotaso 55df508
include new test to ensure websockets connections gracefully exit via…
dprotaso 19e3e0a
increase test timeout to 35 min
dprotaso 00f0b3c
fix copyright date of new files
dprotaso 0d5c031
don't use sleeps but delete the service after we write and read the f…
dprotaso 6b068a0
send correct message in e2e websocket test
dprotaso 63019ba
add godoc
dprotaso 2baffbf
drop use of an extra channel
dprotaso 1e11ed1
fix typo
dprotaso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| Copyright 2026 The Knative Authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package handler | ||
|
|
||
| import ( | ||
| "cmp" | ||
| "context" | ||
| "net/http" | ||
| "sync/atomic" | ||
| "time" | ||
| ) | ||
|
|
||
| // HijackTracker is used to track Websocket Connections | ||
| // Go net/http by default will not manage connections that | ||
| // are hijacked. Thus http.Server::Shutdown will not wait | ||
| // for those connections to finish. | ||
| // | ||
| // What this handler does is track inflight requests | ||
| // using a counter and drain will loop and poll until | ||
| // all the requests are finished. | ||
| type HijackTracker struct { | ||
| Handler http.Handler | ||
| PollInterval time.Duration | ||
|
|
||
| inflight atomic.Int64 | ||
| } | ||
|
|
||
| // Drain should be called after http.Server:Shutdown returns | ||
| func (s *HijackTracker) Drain(ctx context.Context) error { | ||
| pollInterval := cmp.Or(s.PollInterval, time.Second) | ||
|
|
||
| ticker := time.NewTicker(pollInterval) | ||
| defer ticker.Stop() | ||
|
|
||
| for { | ||
| if s.inflight.Load() == 0 { | ||
| return nil | ||
| } | ||
| select { | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| case <-ticker.C: | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (s *HijackTracker) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
| s.inflight.Add(1) | ||
| defer s.inflight.Add(-1) | ||
|
dprotaso marked this conversation as resolved.
|
||
|
|
||
| s.Handler.ServeHTTP(w, r) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| /* | ||
| Copyright 2026 The Knative Authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package handler | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestHijackTrackerNoHijack(t *testing.T) { | ||
| w := httptest.NewRecorder() | ||
| r := httptest.NewRequest(http.MethodGet, "http://somehost.com", nil) | ||
|
|
||
| h := &HijackTracker{ | ||
| Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| w.WriteHeader(http.StatusOK) | ||
| }), | ||
| } | ||
| h.ServeHTTP(w, r) | ||
|
|
||
| err := h.Drain(context.Background()) | ||
| if err != nil { | ||
| t.Fatal("unexpected error while draining", err) | ||
| } | ||
| } | ||
|
|
||
| func TestHijackTrackerConnectionHijacked(t *testing.T) { | ||
| w := httptest.NewRecorder() | ||
| r := httptest.NewRequest(http.MethodGet, "http://somehost.com", nil) | ||
|
|
||
| inHandler := make(chan struct{}) | ||
| handlerWait := make(chan struct{}) | ||
| drainResult := make(chan error, 1) | ||
|
|
||
| h := &HijackTracker{ | ||
| PollInterval: 10 * time.Millisecond, | ||
| Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| close(inHandler) | ||
| <-handlerWait | ||
| }), | ||
| } | ||
|
|
||
| go func() { | ||
| h.ServeHTTP(w, r) | ||
| }() | ||
|
|
||
| select { | ||
| case <-inHandler: | ||
| case <-time.After(250 * time.Millisecond): | ||
| t.Fatal("control flow never reached the http handler") | ||
| } | ||
|
|
||
| go func() { | ||
| drainResult <- h.Drain(context.Background()) | ||
| }() | ||
|
|
||
| select { | ||
| case <-time.After(250 * time.Millisecond): | ||
| case <-drainResult: | ||
| t.Fatal("drain returned befoce handler was finished") | ||
| } | ||
|
|
||
| close(handlerWait) | ||
|
|
||
| var err error | ||
| select { | ||
| case <-time.After(1 * time.Second): | ||
| t.Fatal("Drain was not unblocked when the handler returned") | ||
| case err = <-drainResult: | ||
| } | ||
|
|
||
| if err != nil { | ||
| t.Fatal("unexpected error draining", err) | ||
| } | ||
| } | ||
|
|
||
| func TestHijackTrackerConnectionHijackedTimeout(t *testing.T) { | ||
| w := httptest.NewRecorder() | ||
| r := httptest.NewRequest(http.MethodGet, "http://somehost.com", nil) | ||
|
|
||
| inHandler := make(chan struct{}) | ||
| handlerWait := make(chan struct{}) | ||
| drainStarted := make(chan struct{}) | ||
| drainResult := make(chan error, 1) | ||
|
|
||
| h := &HijackTracker{ | ||
| PollInterval: 10 * time.Millisecond, | ||
| Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| close(inHandler) | ||
| <-handlerWait | ||
| }), | ||
| } | ||
|
|
||
| go func() { | ||
| h.ServeHTTP(w, r) | ||
| }() | ||
|
|
||
| go func() { | ||
| <-inHandler | ||
| close(drainStarted) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 15*time.Millisecond) | ||
| defer cancel() | ||
| drainResult <- h.Drain(ctx) | ||
| }() | ||
|
|
||
| <-drainStarted | ||
| // note: this is defered to unblock the go-routine | ||
| // to clean up the test | ||
| defer close(handlerWait) | ||
|
|
||
| var err error | ||
| select { | ||
| case <-time.After(1 * time.Second): | ||
| t.Fatal("Drain did not timeout") | ||
| case err = <-drainResult: | ||
| } | ||
|
|
||
| if !errors.Is(err, context.DeadlineExceeded) { | ||
| t.Fatal("unexpected error draining", err) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
With the extra test we need to bump this since we run things with
-parallel=1due to github actions default runners being crappy.Uh oh!
There was an error while loading. Please reload this page.
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.
I adjusted the test to not use time.Sleep to speed it up
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.
Maybe not that relevant for this PR but timing tests could also be sped up using synctest after requiring go 1.25, see https://go.dev/blog/testing-time
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.
Not for e2e. It would run faster if we ran tests in parallel - but with kind+github action runners they are so under provisioned that tests flake out etc.