-
-
Notifications
You must be signed in to change notification settings - Fork 2k
🔥 feat: Improve and Optimize ShutdownWithContext Func #3162
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
Changes from 7 commits
137da86
b41d084
e1ad8e9
a683166
a4a5831
424ecbb
98fbdff
796922f
e465b5b
ee866ec
e0a56be
d01a09e
750a7fa
2ea0bb3
b9509fe
c2792e7
18111e5
83ea43d
66dcb42
f3902c5
da193ac
0a92125
b0bc70c
44cbc62
0e99032
a32bddd
5df1c89
da8c54d
21169dc
be64a51
87b2aab
3389913
3703600
6aeb048
15a39cb
41487b5
e5a1ef5
bab3038
187ad51
e4de2c3
4df7e0f
827d70c
ec39306
7c01623
0f63b6b
294e1cd
ebdccd0
0860595
3954fb7
d5fa4e3
d8434e0
3cb0c12
c2e2377
ce9ecc7
05e14f5
8fde47f
cda210b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -860,45 +860,61 @@ func Test_App_ShutdownWithContext(t *testing.T) { | |||||||||||||||||
| t.Parallel() | ||||||||||||||||||
|
|
||||||||||||||||||
| app := New() | ||||||||||||||||||
| shutdownHookCalled := false | ||||||||||||||||||
| app.Hooks().OnShutdown(func() error { | ||||||||||||||||||
| shutdownHookCalled = true | ||||||||||||||||||
| return nil | ||||||||||||||||||
| }) | ||||||||||||||||||
|
Contributor
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. Possible Data Race on The Suggested Fix: Use an Atomic Variable Replace +import "sync/atomic"
- shutdownHookCalled := false
+ var shutdownHookCalled int32
app.Hooks().OnShutdown(func() error {
- shutdownHookCalled = true
+ atomic.StoreInt32(&shutdownHookCalled, 1)
return nil
})
// ...
- assert.True(t, shutdownHookCalled, "Shutdown hook was not called")
+ assert.Equal(t, int32(1), atomic.LoadInt32(&shutdownHookCalled), "Shutdown hook was not called")
|
||||||||||||||||||
|
|
||||||||||||||||||
| app.Get("/", func(ctx Ctx) error { | ||||||||||||||||||
| time.Sleep(5 * time.Second) | ||||||||||||||||||
| return ctx.SendString("body") | ||||||||||||||||||
| }) | ||||||||||||||||||
|
|
||||||||||||||||||
| ln := fasthttputil.NewInmemoryListener() | ||||||||||||||||||
|
|
||||||||||||||||||
| go func() { | ||||||||||||||||||
| err := app.Listener(ln) | ||||||||||||||||||
| assert.NoError(t, err) | ||||||||||||||||||
| }() | ||||||||||||||||||
|
|
||||||||||||||||||
| time.Sleep(1 * time.Second) | ||||||||||||||||||
|
|
||||||||||||||||||
| go func() { | ||||||||||||||||||
| conn, err := ln.Dial() | ||||||||||||||||||
| assert.NoError(t, err) | ||||||||||||||||||
|
|
||||||||||||||||||
| _, err = conn.Write([]byte("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")) | ||||||||||||||||||
| assert.NoError(t, err) | ||||||||||||||||||
| }() | ||||||||||||||||||
| serverErr := make(chan error, 1) | ||||||||||||||||||
| go func() { | ||||||||||||||||||
| serverErr <- app.Listener(ln) | ||||||||||||||||||
| }() | ||||||||||||||||||
|
|
||||||||||||||||||
| time.Sleep(100 * time.Millisecond) | ||||||||||||||||||
|
|
||||||||||||||||||
| clientDone := make(chan struct{}) | ||||||||||||||||||
| go func() { | ||||||||||||||||||
| conn, err := ln.Dial() | ||||||||||||||||||
| assert.NoError(t, err) | ||||||||||||||||||
| _, err = conn.Write([]byte("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")) | ||||||||||||||||||
| assert.NoError(t, err) | ||||||||||||||||||
| close(clientDone) | ||||||||||||||||||
| }() | ||||||||||||||||||
|
|
||||||||||||||||||
| <-clientDone | ||||||||||||||||||
| time.Sleep(100 * time.Millisecond) | ||||||||||||||||||
|
Member
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. why do we need a sleep here? i think it's arbitrary since we already wait for clientDone channel
Contributor
Author
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 sleep is to ensure that our server has started processing the request. Although we have confirmed that the client has sent the request through <-clientDone, this means that the request has been sent to the server's listening port, and there is no guarantee that the server's processing coroutine has been sent. Start processing the request, so I think sleep 100 * Millisecond is required here as well. |
||||||||||||||||||
|
|
||||||||||||||||||
| shutdownErr := make(chan error, 1) | ||||||||||||||||||
| go func() { | ||||||||||||||||||
| ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | ||||||||||||||||||
| defer cancel() | ||||||||||||||||||
| shutdownErr <- app.ShutdownWithContext(ctx) | ||||||||||||||||||
| }() | ||||||||||||||||||
|
Contributor
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. Ensure Proper Context Cancellation The context created with Suggested Improvement Move the ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
+ defer cancel()
shutdownErr <- app.ShutdownWithContext(ctx)
- defer cancel()📝 Committable suggestion
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| time.Sleep(1 * time.Second) | ||||||||||||||||||
| select { | ||||||||||||||||||
| case <-time.After(2 * time.Second): | ||||||||||||||||||
| t.Fatal("shutdown did not complete in time") | ||||||||||||||||||
| case err := <-shutdownErr: | ||||||||||||||||||
| assert.Error(t, err, "Expected shutdown to return an error due to timeout") | ||||||||||||||||||
| assert.True(t, errors.Is(err, context.DeadlineExceeded), "Expected DeadlineExceeded error") | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| shutdownErr := make(chan error) | ||||||||||||||||||
| go func() { | ||||||||||||||||||
| ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | ||||||||||||||||||
| defer cancel() | ||||||||||||||||||
| shutdownErr <- app.ShutdownWithContext(ctx) | ||||||||||||||||||
| }() | ||||||||||||||||||
| assert.True(t, shutdownHookCalled, "Shutdown hook was not called") | ||||||||||||||||||
|
|
||||||||||||||||||
| select { | ||||||||||||||||||
| case <-time.After(5 * time.Second): | ||||||||||||||||||
| t.Fatal("idle connections not closed on shutdown") | ||||||||||||||||||
| case err := <-shutdownErr: | ||||||||||||||||||
| if err == nil || !errors.Is(err, context.DeadlineExceeded) { | ||||||||||||||||||
| t.Fatalf("unexpected err %v. Expecting %v", err, context.DeadlineExceeded) | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| case err := <-serverErr: | ||||||||||||||||||
| assert.NoError(t, err, "Server should have shut down without error") | ||||||||||||||||||
| default: | ||||||||||||||||||
| // Server is still running, which is expected as the long-running request prevented full shutdown | ||||||||||||||||||
| } | ||||||||||||||||||
|
Contributor
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. 🛠️ Refactor suggestion Avoid Skipping Server Error Handling Using a Suggested Fix: Remove the This ensures the test waits for select {
case err := <-serverErr:
assert.NoError(t, err, "Server should have shut down without error")
- default:
- // Server is still running, which is expected as the long-running request prevented full shutdown
}📝 Committable suggestion
Suggested change
|
||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // go test -run Test_App_Mixed_Routes_WithSameLen | ||||||||||||||||||
|
|
||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.