-
-
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 31 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
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 |
---|---|---|
|
@@ -21,6 +21,7 @@ import ( | |
"regexp" | ||
"runtime" | ||
"strings" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -861,45 +862,61 @@ func Test_App_ShutdownWithContext(t *testing.T) { | |
t.Parallel() | ||
|
||
app := New() | ||
var shutdownHookCalled atomic.Int32 | ||
// TODO: add test | ||
// app.Hooks().OnShutdown(func() error { | ||
// shutdownHookCalled.Store(1) | ||
// return nil | ||
// }) | ||
|
||
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 Uncomment and update shutdown hook test The TODO comment and commented-out shutdown hook test should be uncommented and updated to use the new pre/post shutdown hooks. var shutdownHookCalled atomic.Int32
-// TODO: add test
-// app.Hooks().OnShutdown(func() error {
-// shutdownHookCalled.Store(1)
-// return nil
-// })
+app.Hooks().OnPreShutdown(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) | ||
efectn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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") | ||
ReneWerner87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 |
---|---|---|
|
@@ -228,7 +228,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. Not sure about this behavior. I think we should only execute them during successful shutdown. Maybe we can OnShutdownError hook. What do you think @gaby @ReneWerner87 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'm not sure either. Also not sure about the defer of the hook 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 would prefer to add Post Pre hook 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. The add Post Pre hook is a good idea, but how do I tell which hooks are pre closed and which are post closed? @efectn 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. You can add new hook called PostShutdown, PreShutdown and replace the old one with them 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. sound good, @JIeJaitt can you do the suggested change 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 means making a change below the 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.
Yes it is. You can also add err parameter to PostShutdown hook, so that we can also get rid of OnShutdownError, OnShutdownSuccess properties of ListenerConfig. 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.
OK, I will press on with this idea to realize it again in my free time, by the way what is the v3 deadline as I am busy with my final exams these days. 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.
We want to release v3 rc1 in the beginning of January. If you can't look at the PR this week, i can also look no problem |
||
|
||
```go | ||
func (app *App) Shutdown() error | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,9 +11,11 @@ type ( | |
OnGroupHandler = func(Group) error | ||
OnGroupNameHandler = OnGroupHandler | ||
OnListenHandler = func(ListenData) error | ||
OnShutdownHandler = func() error | ||
OnForkHandler = func(int) error | ||
OnMountHandler = func(*App) error | ||
// OnShutdownHandler = func() error | ||
OnPreShutdownHandler = func() error | ||
OnPostShutdownHandler = func(error) error | ||
OnForkHandler = func(int) error | ||
OnMountHandler = func(*App) error | ||
) | ||
|
||
// Hooks is a struct to use it with App. | ||
|
@@ -27,9 +29,11 @@ type Hooks struct { | |
onGroup []OnGroupHandler | ||
onGroupName []OnGroupNameHandler | ||
onListen []OnListenHandler | ||
onShutdown []OnShutdownHandler | ||
onFork []OnForkHandler | ||
onMount []OnMountHandler | ||
// onShutdown []OnShutdownHandler | ||
onPreShutdown []OnPreShutdownHandler | ||
onPostShutdown []OnPostShutdownHandler | ||
onFork []OnForkHandler | ||
onMount []OnMountHandler | ||
} | ||
|
||
// ListenData is a struct to use it with OnListenHandler | ||
|
@@ -47,9 +51,11 @@ func newHooks(app *App) *Hooks { | |
onGroupName: make([]OnGroupNameHandler, 0), | ||
onName: make([]OnNameHandler, 0), | ||
onListen: make([]OnListenHandler, 0), | ||
onShutdown: make([]OnShutdownHandler, 0), | ||
onFork: make([]OnForkHandler, 0), | ||
onMount: make([]OnMountHandler, 0), | ||
// onShutdown: make([]OnShutdownHandler, 0), | ||
onPreShutdown: make([]OnPreShutdownHandler, 0), | ||
onPostShutdown: make([]OnPostShutdownHandler, 0), | ||
onFork: make([]OnForkHandler, 0), | ||
onMount: make([]OnMountHandler, 0), | ||
} | ||
} | ||
|
||
|
@@ -96,10 +102,25 @@ func (h *Hooks) OnListen(handler ...OnListenHandler) { | |
h.app.mutex.Unlock() | ||
} | ||
|
||
// TODO:To be deleted, replaced by OnPreShutdown and OnPostShutdown | ||
// OnShutdown is a hook to execute user functions after Shutdown. | ||
func (h *Hooks) OnShutdown(handler ...OnShutdownHandler) { | ||
// func (h *Hooks) OnShutdown(handler ...OnShutdownHandler) { | ||
// h.app.mutex.Lock() | ||
// h.onShutdown = append(h.onShutdown, handler...) | ||
// h.app.mutex.Unlock() | ||
// } | ||
|
||
// OnPreShutdown is a hook to execute user functions before Shutdown. | ||
func (h *Hooks) OnPreShutdown(handler ...OnPreShutdownHandler) { | ||
h.app.mutex.Lock() | ||
h.onShutdown = append(h.onShutdown, handler...) | ||
h.onPreShutdown = append(h.onPreShutdown, handler...) | ||
h.app.mutex.Unlock() | ||
} | ||
|
||
// OnPostShutdown is a hook to execute user functions after Shutdown. | ||
func (h *Hooks) OnPostShutdown(handler ...OnPostShutdownHandler) { | ||
h.app.mutex.Lock() | ||
h.onPostShutdown = append(h.onPostShutdown, handler...) | ||
h.app.mutex.Unlock() | ||
} | ||
|
||
|
@@ -191,10 +212,26 @@ func (h *Hooks) executeOnListenHooks(listenData ListenData) error { | |
return nil | ||
} | ||
|
||
func (h *Hooks) executeOnShutdownHooks() { | ||
for _, v := range h.onShutdown { | ||
// func (h *Hooks) executeOnShutdownHooks() { | ||
// for _, v := range h.onShutdown { | ||
// if err := v(); err != nil { | ||
// log.Errorf("failed to call shutdown hook: %v", err) | ||
// } | ||
// } | ||
// } | ||
|
||
func (h *Hooks) executeOnPreShutdownHooks() { | ||
for _, v := range h.onPreShutdown { | ||
if err := v(); err != nil { | ||
log.Errorf("failed to call shutdown hook: %v", err) | ||
log.Errorf("failed to call pre shutdown hook: %v", err) | ||
} | ||
} | ||
} | ||
|
||
func (h *Hooks) executeOnPostShutdownHooks(err error) { | ||
for _, v := range h.onPostShutdown { | ||
if err := v(err); err != nil { | ||
log.Errorf("failed to call pre shutdown hook: %v", err) | ||
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. Fix error message in log The error message incorrectly states "pre shutdown hook" when it should be "post shutdown hook". - log.Errorf("failed to call pre shutdown hook: %v", err)
+ log.Errorf("failed to call post shutdown hook: %v", err) Also applies to: 234-234 |
||
} | ||
} | ||
} | ||
|
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.
Here we can define executeOnPostShutdownHooks hook with defer as well as PreShutdownHook
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.
Is that what you mean?I think
PreShutdownHook
andPostShutdownHook
set outside the function and then executed atShutdownWithContext
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.
You can do it like:
So that, we won't need to add another additional check to listen.go and this is going to work even if you use app.Shutdown() separately. However, this hook wouldn't work if app.Listen() hadn't been started in another goroutine. Maybe we can also add a note to indicate it -> https://github.com/valyala/fasthttp/blob/master/server.go#L1830