gin
gin copied to clipboard
feat(gracefulstop): add wrapper of Shutdown
We start httpServer by gin as example below In my project. Here is a case:
- Start gin http server
- Receive data from gin http server and send task to worker to process.
- The worker stop at first when my process go to exit. But my gin http server is running, some error happened.
We expect to stop gin http server at first by calling func 'http.Server.Shutdown(context.TODO())' to avoid process error. So, can i make a mr and add wrapper func 'Shutdown()' to gin for graceful stop my process.
Here are my use case.
func main() {
// Set up a http server.
r := gin.Default()
// ...
go func() {
// Run http server
if err := r.Run(":8899"); err != nil {
log.Fatalf("could not run server: %v", err)
}
}()
// Run worker
go RunWorker()
// wait signal to stop
WaitSignal()
}
// RunWorker run worker in one goroutine
func RunWorker() {
for !isStop {
// ... some request received by gin http server ...
// ... do some work ...
}
}
// WaitSignal stop signal handle
func WaitSignal() {
shutdownHook := make(chan os.Signal, 1)
signal.Notify(shutdownHook, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, os.Interrupt)
<-shutdownHook
GracefulStop()
// wait 2 second for graceful stop.
time.Sleep(2 * time.Second)
os.Exit(0)
}
// GracefulStop graceful stop http.Server at first, then stop all workers.
func GracefulStop() {
// 1. stop http.Server and refuse new data are received.
err := server.Shutdown(context.TODO())
// 2. stop worker and finish tasks in process.
// StopWorker() ...
}
- With pull requests:
- Open your pull request against
master
- Your pull request should have no more than two commits, if not you should squash them.
- It should pass all tests in the available continuous integration systems such as GitHub Actions.
- You should add/modify tests to cover your proposed code changes.
- If your pull request contains a new feature, please document it on the README.
- Open your pull request against
Codecov Report
Merging #3202 (970402c) into master (05caa5c) will decrease coverage by
0.24%
. The diff coverage is60.00%
.
:exclamation: Current head 970402c differs from pull request most recent head b60c029. Consider uploading reports for the commit b60c029 to get more accurate results
@@ Coverage Diff @@
## master #3202 +/- ##
==========================================
- Coverage 98.22% 97.97% -0.25%
==========================================
Files 43 43
Lines 3148 3164 +16
==========================================
+ Hits 3092 3100 +8
- Misses 44 52 +8
Partials 12 12
Flag | Coverage Δ | |
---|---|---|
go-1.14 | 97.97% <60.00%> (-0.25%) |
:arrow_down: |
go-1.15 | 97.81% <60.00%> (-0.25%) |
:arrow_down: |
go-1.16 | 97.78% <60.00%> (-0.25%) |
:arrow_down: |
go-1.17 | 97.72% <60.00%> (-0.25%) |
:arrow_down: |
go-1.18 | 97.72% <60.00%> (-0.25%) |
:arrow_down: |
macos-latest | 97.97% <60.00%> (-0.25%) |
:arrow_down: |
nomsgpack | 97.94% <60.00%> (-0.25%) |
:arrow_down: |
ubuntu-latest | 97.97% <60.00%> (-0.25%) |
:arrow_down: |
Flags with carried forward coverage won't be shown. Click here to find out more.
Impacted Files | Coverage Δ | |
---|---|---|
gin.go | 97.14% <60.00%> (-2.05%) |
:arrow_down: |
Continue to review full report at Codecov.
Legend - Click here to learn more
Δ = absolute <relative> (impact)
,ø = not affected
,? = missing data
Powered by Codecov. Last update 05caa5c...b60c029. Read the comment docs.