-
Notifications
You must be signed in to change notification settings - Fork 44
fix: wait for HTTP server drain during graceful shutdown #1745
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
4 commits
Select commit
Hold shift + click to select a range
05a9de9
fix: wait for HTTP server drain during graceful shutdown
AmanGIT07 2075906
fix: use "err" log key in shutdown error logs
AmanGIT07 86a2168
docs: note why the listen error path skips the shutdown wait
AmanGIT07 a068555
fix: cover h2c connections in graceful shutdown and prove the drain
AmanGIT07 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,150 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "log/slog" | ||
| "net" | ||
| "net/http" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/raystack/frontier/internal/api" | ||
| ) | ||
|
|
||
| func TestGracefulShutdownDrainsInflightRequests(t *testing.T) { | ||
| logger := slog.New(slog.NewTextHandler(io.Discard, nil)) | ||
|
|
||
| entered := make(chan struct{}) | ||
| requestDone := make(chan struct{}) | ||
| mux := http.NewServeMux() | ||
| mux.HandleFunc("/slow", func(w http.ResponseWriter, r *http.Request) { | ||
| close(entered) | ||
| time.Sleep(300 * time.Millisecond) | ||
| close(requestDone) | ||
| w.WriteHeader(http.StatusOK) | ||
| }) | ||
|
|
||
| l, err := net.Listen("tcp", "127.0.0.1:0") | ||
| if err != nil { | ||
| t.Fatalf("listen: %v", err) | ||
| } | ||
| srv := &http.Server{Handler: mux} | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
|
|
||
| var wg sync.WaitGroup | ||
| wg.Add(1) | ||
| go gracefulShutdown(ctx, logger, &wg, srv, "test server", make(chan struct{})) | ||
|
|
||
| serveReturned := make(chan struct{}) | ||
| go func() { | ||
| defer close(serveReturned) | ||
| if err := srv.Serve(l); err != nil && err != http.ErrServerClosed { | ||
| t.Errorf("serve failed: %v", err) | ||
| } | ||
| }() | ||
|
|
||
| requestErr := make(chan error, 1) | ||
| go func() { | ||
| resp, err := http.Get(fmt.Sprintf("http://%s/slow", l.Addr())) | ||
| if err == nil { | ||
| resp.Body.Close() | ||
| if resp.StatusCode != http.StatusOK { | ||
| err = fmt.Errorf("unexpected status %d", resp.StatusCode) | ||
| } | ||
| } | ||
| requestErr <- err | ||
| }() | ||
|
|
||
| <-entered | ||
| cancel() | ||
|
|
||
| <-serveReturned | ||
| wg.Wait() | ||
|
|
||
| select { | ||
| case <-requestDone: | ||
| default: | ||
| t.Fatal("shutdown wait released before the in-flight request finished") | ||
| } | ||
| if err := <-requestErr; err != nil { | ||
| t.Fatalf("in-flight request failed: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func freePort(t *testing.T) int { | ||
| t.Helper() | ||
| l, err := net.Listen("tcp", "127.0.0.1:0") | ||
| if err != nil { | ||
| t.Fatalf("could not find a free port: %v", err) | ||
| } | ||
| defer l.Close() | ||
| return l.Addr().(*net.TCPAddr).Port | ||
| } | ||
|
|
||
| func waitForHTTP(t *testing.T, url string) { | ||
| t.Helper() | ||
| deadline := time.Now().Add(10 * time.Second) | ||
| for time.Now().Before(deadline) { | ||
| resp, err := http.Get(url) | ||
| if err == nil { | ||
| resp.Body.Close() | ||
| if resp.StatusCode == http.StatusOK { | ||
| return | ||
| } | ||
| } | ||
| time.Sleep(50 * time.Millisecond) | ||
| } | ||
| t.Fatalf("server did not respond at %s in time", url) | ||
| } | ||
|
|
||
| func TestServeConnectReturnsAfterShutdownOnContextCancel(t *testing.T) { | ||
|
rohilsurana marked this conversation as resolved.
|
||
| tests := []struct { | ||
| name string | ||
| withMetrics bool | ||
| }{ | ||
| {name: "connect server only", withMetrics: false}, | ||
| {name: "connect and metrics servers", withMetrics: true}, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| logger := slog.New(slog.NewTextHandler(io.Discard, nil)) | ||
|
|
||
| var cfg Config | ||
| cfg.Connect.Port = freePort(t) | ||
| if tc.withMetrics { | ||
| cfg.MetricsPort = freePort(t) | ||
| } | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
|
|
||
| served := make(chan error, 1) | ||
| go func() { | ||
| served <- ServeConnect(ctx, logger, cfg, api.Deps{}, prometheus.NewRegistry()) | ||
| }() | ||
|
|
||
| waitForHTTP(t, fmt.Sprintf("http://127.0.0.1:%d/ping", cfg.Connect.Port)) | ||
| if tc.withMetrics { | ||
| waitForHTTP(t, fmt.Sprintf("http://127.0.0.1:%d/metrics", cfg.MetricsPort)) | ||
| } | ||
|
|
||
| cancel() | ||
|
|
||
| select { | ||
| case err := <-served: | ||
| if err != nil { | ||
| t.Fatalf("ServeConnect returned error: %v", err) | ||
| } | ||
| case <-time.After(15 * time.Second): | ||
| t.Fatal("ServeConnect did not return after context cancel") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.