Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to gracefully shut down or restart golang

2025-03-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how to gracefully shut down or restart golang". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to gracefully shut down or restart golang" can help you solve your doubts.

Gracefully shut down or restart

After the deployment of the Web project, we often restart the service because of configuration changes or functional iterations. The simple kill-9 pid method will force the process to shut down, which will cause the request currently being processed by the server to fail. Is there a more elegant way to shut down or restart?

To read this article, you need to understand some of the concepts of signals in the UNIX system. Please refer to the materials in advance.

Graceful shutdown what is elegant shutdown?

Elegant shutdown means that the server does not shut down immediately after the shutdown command is issued, but waits for all the requests currently being processed before exiting the program. It is a client-friendly shutdown method. When you execute Ctrl+C to shut down the server, it will force the end of the process and cause problems with the request being accessed.

How to achieve graceful shutdown?

After Go 1.8, http.Server 's built-in Shutdown () method supports graceful shutdown, as shown in the following example:

/ + build go1.8package mainimport ("context"log"net/http"os"os/signal"syscall"time"github.com/gin-gonic/gin") func main () {router: = gin.Default () router.GET ("/") Func (c * gin.Context) {time.Sleep (5 * time.Second) c.String (http.StatusOK, "Welcome Gin Server")}) srv: = & http.Server {Addr: ": 8080", Handler: router } go func () {/ / enable a goroutine startup service if err: = srv.ListenAndServe () Err! = nil & & err! = http.ErrServerClosed {log.Fatalf ("listen:% s\ n", err)}} () / / wait for the interrupt signal to gracefully shut down the server Set a 5-second timeout quit for shutting down the server: = make (chan os.Signal, 1) / / create a channel to receive the signal / / kill sends syscall.SIGTERM signal / / kill-2 send syscall.SIGINT signal by default. Our commonly used Ctrl+C is to trigger the system SIGINT signal / / kill-9 to send syscall.SIGKILL signal, but it cannot be captured. So there is no need to add it / / signal.Notify to forward received syscall.SIGINT or syscall.SIGTERM signals to quit signal.Notify (quit, syscall.SIGINT, syscall.SIGTERM) / / there is no blocking here

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report