Issue background
When the application restarts, http if the kill -9 program exits directly and then starts, there are several issues:
- The old request has not been processed and if the server process exits directly, it will cause the client link to be interrupted (received
RST );
- New request to call, the service has not been restarted, causing
connection refused
These problems can lead to bad customer experience, serious and even affect the customer's business. So, we need to gracefully restart our application.
Solving problems--Elegance
Github:gracehttp
Principle
- signal mechanism;
- The child process inherits the resources of the parent process;
Support Features
- Smooth restart (zero-downtime restart server);
- Smooth off;
Serveradd more (include HTTP , HTTPS );
- Custom log components;
User Guide
Add Server
// http srv1 := &http.Server{ Addr: ":80", Handler: sc, } gracehttp.AddServer(srv1, false, "", "") // https srv2 := &http.Server{ Addr: ":443", Handler: sc, } gracehttp.AddServer(srv2, true, "../config/https.crt", "../config/https.key") gracehttp.Run() // 此方法会阻塞,直到所有的 HTTP 服务退出
As shown above, just create a good Server object and call gracehttp.AddServer Add.
Adding custom log Components
gracehttp.SetErrorLogCallback(logger.LogConfigLoadError)
There are three methods available here Set* that correspond to different log levels:
- Setinfologcallback
- Setnoticelogcallback
- Seterrorlogcallback