Graceful Shutdown
The graceful shutdown function provided by Hertz when stopping service.
Hertz supports graceful shutdown, which is executed as follows:
- Set the state of
engine
toclosed
- Sequential non-blocking trigger callback function
[]OnShutDown
(consistent with standard library net/http),Select
waits them until wait timeout or finish Select
waits for the business coroutine to exit:- For netpoll network library, turn on
ticker
with default 1s (set in netpoll, not changeable at the moment) and check ifactive conn
(business handle exits and connection is not in blocking read state) is 0 at regular intervals; for go net network library, turn off listening and do not process the connection. - Triggered by the context of
ExitWaitTime
, default 5s
- For netpoll network library, turn on
- Uniformly add
Connection:Close header
to request packets in the process of closing - Registration Center deregisters this service.
- Shut down the signal listening of the network library
If you want to modify the wait timeout, you can configure it with server.WithExitWaitTime()
.
If you want to register the hook
function, you can do so by getting the Engine
and registering it:
h.Engine.OnShutdown = append(h.Engine.OnShutdown, shutDownFunc)
waitSignal
is default implementation for signal waiter,which is executed as follows:
- SIGTERM triggers immediately close.
- SIGHUP|SIGINT triggers graceful shutdown.
If Default one is not met the requirement, SetCustomSignalWaiter
set this function to customize.
package main
import (
"github.com/cloudwego/hertz/pkg/app/server"
)
func main() {
h := server.New()
h.SetCustomSignalWaiter(func(err chan error) error {
return nil
})
...
}
Hertz will exit immediately if f returns an error,otherwise it will exit gracefully.
Last modified
October 17, 2024
: Fix multiple typos in shumei.md (#1159) (d3919b5)