Hertz supports graceful shutdown, which is executed as follows:
engine
to closed
[]OnShutDown
(consistent with standard library net/http), Select
waits them until wait timeout or finishConnection:Close
header to request packets in the process of closingSelect
waits for the business coroutine to exit:ticker
with default 1s (set in netpoll, not changeable at the moment) and check if active 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.ExitWaitTime
, default 5sIf 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:
Version < v0.9.6
Version >= v0.9.6
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.