Skip to content
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

Merged
Changes from 1 commit
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
137da86
feat: Optimize ShutdownWithContext method in app.go
Oct 10, 2024
b41d084
feat: Enhance ShutdownWithContext test for improved reliability
Oct 10, 2024
e1ad8e9
Merge branch 'gofiber:main' into jiejaitt-feature/improve-shutdown-wi…
JIeJaitt Oct 12, 2024
a683166
📚 Doc: update the docs to explain shutdown & hook execution order
JIeJaitt Oct 12, 2024
a4a5831
Merge branch 'main' into jiejaitt-feature/improve-shutdown-with-context
JIeJaitt Oct 12, 2024
424ecbb
Merge branch 'main' of github.com:JIeJaitt/fiber into jiejaitt-featur…
JIeJaitt Oct 12, 2024
98fbdff
Merge branch 'jiejaitt-feature/improve-shutdown-with-context' of gith…
JIeJaitt Oct 12, 2024
796922f
🩹 Fix: Possible Data Race on shutdownHookCalled Variable
JIeJaitt Oct 12, 2024
e465b5b
🩹 Fix: Remove the default Case
JIeJaitt Oct 12, 2024
ee866ec
🩹 Fix: Import sync/atomic
JIeJaitt Oct 12, 2024
e0a56be
🩹 Fix: golangci-lint problem
JIeJaitt Oct 13, 2024
d01a09e
Merge branches 'jiejaitt-feature/improve-shutdown-with-context' and '…
JIeJaitt Oct 13, 2024
750a7fa
🎨 Style: add block in api.md
JIeJaitt Oct 28, 2024
2ea0bb3
Merge branch 'main' of github.com:JIeJaitt/fiber into jiejaitt-featur…
JIeJaitt Oct 28, 2024
b9509fe
🩹 Fix: go mod tidy
JIeJaitt Oct 28, 2024
c2792e7
feat: Optimize ShutdownWithContext method in app.go
Oct 10, 2024
18111e5
feat: Enhance ShutdownWithContext test for improved reliability
Oct 10, 2024
83ea43d
📚 Doc: update the docs to explain shutdown & hook execution order
JIeJaitt Oct 12, 2024
66dcb42
🩹 Fix: Possible Data Race on shutdownHookCalled Variable
JIeJaitt Oct 12, 2024
f3902c5
🩹 Fix: Remove the default Case
JIeJaitt Oct 12, 2024
da193ac
🩹 Fix: Import sync/atomic
JIeJaitt Oct 12, 2024
0a92125
🩹 Fix: golangci-lint problem
JIeJaitt Oct 13, 2024
b0bc70c
🎨 Style: add block in api.md
JIeJaitt Oct 28, 2024
44cbc62
🩹 Fix: go mod tidy
JIeJaitt Oct 28, 2024
0e99032
Merge branch 'jiejaitt-feature/improve-shutdown-with-context' of gith…
JIeJaitt Oct 28, 2024
a32bddd
Merge branch 'main' of github.com:gofiber/fiber into jiejaitt-feature…
JIeJaitt Oct 29, 2024
5df1c89
Merge branch 'main' of github.com:gofiber/fiber into jiejaitt-feature…
JIeJaitt Nov 14, 2024
da8c54d
Merge branch 'main' of github.com:gofiber/fiber into jiejaitt-feature…
JIeJaitt Nov 15, 2024
21169dc
Merge branch 'main' into jiejaitt-feature/improve-shutdown-with-context
gaby Nov 30, 2024
be64a51
Merge branch 'main' of github.com:JIeJaitt/fiber into jiejaitt-featur…
JIeJaitt Dec 10, 2024
87b2aab
♻️ Refactor: replaced OnShutdown by OnPreShutdown and OnPostShutdown
JIeJaitt Dec 10, 2024
3389913
♻️ Refactor: streamline post-shutdown hook execution in graceful shut…
JIeJaitt Dec 11, 2024
3703600
Merge branch 'main' of github.com:JIeJaitt/fiber into jiejaitt-featur…
JIeJaitt Dec 11, 2024
6aeb048
🚨 Test: add test for gracefulShutdown
JIeJaitt Dec 11, 2024
15a39cb
Merge branch 'main' of github.com:gofiber/fiber into jiejaitt-feature…
JIeJaitt Feb 11, 2025
41487b5
🔥 Feature: Using executeOnPreShutdownHooks and executeOnPostShutdownH…
JIeJaitt Feb 11, 2025
e5a1ef5
🩹 Fix: deal Listener err
JIeJaitt Feb 11, 2025
bab3038
🩹 Fix: go lint error
JIeJaitt Feb 11, 2025
187ad51
🩹 Fix: reduced memory alignment
JIeJaitt Feb 11, 2025
e4de2c3
🩹 Fix: reduced memory alignment
JIeJaitt Feb 11, 2025
4df7e0f
🩹 Fix: context should be created inside the concatenation.
JIeJaitt Feb 12, 2025
827d70c
Merge branch 'main' of github.com:gofiber/fiber into jiejaitt-feature…
JIeJaitt Feb 12, 2025
ec39306
📚 Doc: update what_new.md and hooks.md
JIeJaitt Feb 13, 2025
7c01623
♻️ Refactor: use blocking channel instead of time.Sleep
JIeJaitt Feb 13, 2025
0f63b6b
🩹 Fix: Improve synchronization in error propagation test.
JIeJaitt Feb 13, 2025
294e1cd
🩹 Fix: Replace sleep with proper synchronization.
JIeJaitt Feb 13, 2025
ebdccd0
Merge branch 'main' of github.com:gofiber/fiber into jiejaitt-feature…
JIeJaitt Feb 13, 2025
0860595
🩹 Fix: Server but not shut down properly
JIeJaitt Feb 14, 2025
3954fb7
🩹 Fix: Using channels to synchronize and pass results
JIeJaitt Feb 14, 2025
d5fa4e3
🩹 Fix: timeout with long running request
JIeJaitt Feb 14, 2025
d8434e0
📚 Doc: remove OnShutdownError and OnShutdownSuccess from fiber.md
JIeJaitt Feb 17, 2025
3cb0c12
Update hooks.md
gaby Feb 18, 2025
c2e2377
🚨 Test: Add graceful shutdown timeout error test case
JIeJaitt Feb 22, 2025
ce9ecc7
📝 Doc: Restructure hooks documentation for OnPreShutdown and OnPostSh…
JIeJaitt Feb 22, 2025
05e14f5
Merge branch 'main' into jiejaitt-feature/improve-shutdown-with-context
JIeJaitt Feb 22, 2025
8fde47f
📝 Doc: Remove extra whitespace in hooks documentation
JIeJaitt Feb 22, 2025
cda210b
Merge branch 'jiejaitt-feature/improve-shutdown-with-context' of gith…
JIeJaitt Feb 22, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
🩹 Fix: golangci-lint problem
JIeJaitt committed Oct 28, 2024
commit 0a921254c20725c07edcbbe2b23fcc5e99c65c40
81 changes: 40 additions & 41 deletions app_test.go
Original file line number Diff line number Diff line change
@@ -20,9 +20,9 @@ import (
"regexp"
"runtime"
"strings"
"sync/atomic"
"testing"
"time"
"sync/atomic"

"github.com/gofiber/utils/v2"

@@ -861,11 +861,11 @@ func Test_App_ShutdownWithContext(t *testing.T) {
t.Parallel()

app := New()
var shutdownHookCalled int32
app.Hooks().OnShutdown(func() error {
atomic.StoreInt32(&shutdownHookCalled, 1)
return nil
})
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)
@@ -874,48 +874,47 @@ func Test_App_ShutdownWithContext(t *testing.T) {

ln := fasthttputil.NewInmemoryListener()

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)
}()
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)
// Sleep to ensure the server has started processing the request
time.Sleep(100 * time.Millisecond)

shutdownErr := make(chan error, 1)
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
shutdownErr <- app.ShutdownWithContext(ctx)
}()
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
shutdownErr <- app.ShutdownWithContext(ctx)
}()

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")
}
case <-time.After(2 * time.Second):
t.Fatal("shutdown did not complete in time")
case err := <-shutdownErr:
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), atomic.LoadInt32(&shutdownHookCalled), "Shutdown hook was not called")
assert.Equal(t, int32(1), shutdownHookCalled.Load(), "Shutdown hook was not called")

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
}
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
1 change: 1 addition & 0 deletions go.mod
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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/dl@v0.0.0-20241001165935-bedb0f791d00/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
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -33,6 +33,8 @@ github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVS
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
golang.org/dl v0.0.0-20241001165935-bedb0f791d00 h1:OX0WPBB1pQPZy1SL0+q5C/VuuM6e1wv6uEuB9iyBi/I=
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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=