-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
feat: Improve and Optimize ShutdownWithContext Func #3162
base: main
Are you sure you want to change the base?
Changes from 11 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
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 |
---|---|---|
|
@@ -20,6 +20,7 @@ import ( | |
"regexp" | ||
"runtime" | ||
"strings" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -860,45 +861,60 @@ func Test_App_ShutdownWithContext(t *testing.T) { | |
t.Parallel() | ||
|
||
app := New() | ||
var shutdownHookCalled atomic.Int32 | ||
app.Hooks().OnShutdown(func() error { | ||
shutdownHookCalled.Store(1) | ||
return nil | ||
}) | ||
|
||
app.Get("/", func(ctx Ctx) error { | ||
time.Sleep(5 * time.Second) | ||
return ctx.SendString("body") | ||
}) | ||
|
||
ln := fasthttputil.NewInmemoryListener() | ||
|
||
serverErr := make(chan error, 1) | ||
go func() { | ||
err := app.Listener(ln) | ||
assert.NoError(t, err) | ||
serverErr <- app.Listener(ln) | ||
}() | ||
|
||
time.Sleep(1 * time.Second) | ||
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: google.com\r\n\r\n")) | ||
_, err = conn.Write([]byte("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")) | ||
assert.NoError(t, err) | ||
close(clientDone) | ||
}() | ||
|
||
time.Sleep(1 * time.Second) | ||
<-clientDone | ||
// Sleep to ensure the server has started processing the request | ||
time.Sleep(100 * time.Millisecond) | ||
Comment on lines
+893
to
+895
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 Suggestion: Replace sleep with synchronization for test reliability Using |
||
|
||
shutdownErr := make(chan error) | ||
shutdownErr := make(chan error, 1) | ||
go func() { | ||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | ||
defer cancel() | ||
shutdownErr <- app.ShutdownWithContext(ctx) | ||
}() | ||
|
||
select { | ||
case <-time.After(5 * time.Second): | ||
t.Fatal("idle connections not closed on shutdown") | ||
case <-time.After(2 * time.Second): | ||
t.Fatal("shutdown did not complete in time") | ||
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. Is it "in time" or "on time" 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. I prefer to use it |
||
case err := <-shutdownErr: | ||
if err == nil || !errors.Is(err, context.DeadlineExceeded) { | ||
t.Fatalf("unexpected err %v. Expecting %v", err, context.DeadlineExceeded) | ||
} | ||
require.Error(t, err, "Expected shutdown to return an error due to timeout") | ||
require.ErrorIs(t, err, context.DeadlineExceeded, "Expected DeadlineExceeded error") | ||
} | ||
|
||
assert.Equal(t, int32(1), shutdownHookCalled.Load(), "Shutdown hook was not called") | ||
|
||
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 | ||
} | ||
|
||
// go test -run Test_App_Mixed_Routes_WithSameLen | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -205,7 +205,7 @@ Shutdown gracefully shuts down the server without interrupting any active connec | |
|
||
ShutdownWithTimeout will forcefully close any active connections after the timeout expires. | ||
|
||
ShutdownWithContext shuts down the server including by force if the context's deadline is exceeded. | ||
ShutdownWithContext shuts down the server including by force if the context's deadline is exceeded.Shutdown hooks will still be executed, even if an error occurs during the shutdown process, as they are deferred to ensure cleanup happens regardless of errors. | ||
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. Missing space before "Shutdown 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. Thank you, I have solved this problem |
||
|
||
```go | ||
func (app *App) Shutdown() error | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ require ( | |
github.com/philhofer/fwd v1.1.3-0.20240612014219-fbbf4953d986 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/valyala/tcplisten v1.0.0 // indirect | ||
golang.org/dl v0.0.0-20241001165935-bedb0f791d00 // indirect | ||
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 is this here? 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 an official multi-version control function of the go language, because the Go version I use locally is higher than the go version in fiber's go mod. In order to keep the version consistent, I rely on this method to use it locally in the same way as the fiber framework. Go language version, you can see this to get more information: https://pkg.go.dev/golang.org/dl and https://pkg.go.dev/golang.org/[email protected]/go1.22.6 . It will not have any impact on the function of the fiber |
||
golang.org/x/net v0.29.0 // indirect | ||
golang.org/x/sys v0.25.0 // indirect | ||
golang.org/x/text v0.18.0 // indirect | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,8 @@ github.com/valyala/fasthttp v1.56.0 h1:bEZdJev/6LCBlpdORfrLu/WOZXXxvrUQSiyniuaoW | |
github.com/valyala/fasthttp v1.56.0/go.mod h1:sReBt3XZVnudxuLOx4J/fMrJVorWRiWY2koQKgABiVI= | ||
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= | ||
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= | ||
golang.org/dl v0.0.0-20241001165935-bedb0f791d00 h1:OX0WPBB1pQPZy1SL0+q5C/VuuM6e1wv6uEuB9iyBi/I= | ||
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. And these? 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. Same as my answer above |
||
golang.org/dl v0.0.0-20241001165935-bedb0f791d00/go.mod h1:fwQ+hlTD8I6TIzOGkQqxQNfE2xqR+y7SzGaDkksVFkw= | ||
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= | ||
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= | ||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
|
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.
It would be nice to update the docs to explain shutdown & hook execution order
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.
done