In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you the relevant knowledge about how Golang elegantly terminates a service. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Preface
When starting a Golang http service in a conventional way, if the service is unexpectedly terminated or interrupted, that is, it does not wait for the service to process the existing request connection and return normally, and does not do some necessary processing before the service is stopped, it will result in hard termination of the service. This way is not very elegant.
See the following code, the http service request path is the root path, request this path, it will return hello after 2 seconds.
Var addr = flag.String ("server addr", ": 8080", "server address") func main () {http.HandleFunc ("/", func (w http.ResponseWriter, r * http.Request) {time.Sleep (2 * time.Second) fmt.Fprintln (w, "hello")}) http.ListenAndServe (* addr, nil)}
If you request http://localhost:8080/ after the service is started, and then use Ctrl+C to interrupt the service immediately, the service will exit immediately (exit status 2), and the request will not be returned normally (ERR_CONNECTION_REFUSED), and the connection will be broken immediately.
Next, we introduce the use of http.Server 's Shutdown method combined with signal.Notify to gracefully terminate the service.
1 Shutdown method
The Golang http.Server structure has a method, Shutdown, to terminate the service, and its go doc is as follows.
Func (srv * Server) Shutdown (ctx context.Context) error
Shutdown gracefully shuts down the server without interrupting any active
Connections. Shutdown works by first closing all open listeners, then
Closing all idle connections, and then waiting indefinitely for connections
To return to idle and then shut down. If the provided context expires before
The shutdown is complete, Shutdown returns the context's error, otherwise it
Returns any error returned from closing the Server's underlying Listener (s).
When Shutdown is called, Serve, ListenAndServe, and ListenAndServeTLS
Immediately return ErrServerClosed. Make sure the program doesn't exit and
Waits instead for Shutdown to return.
Shutdown does not attempt to close nor wait for hijacked connections such as
WebSockets. The caller of Shutdown should separately notify such long-lived
Connections of shutdown and wait for them to close, if desired. See
RegisterOnShutdown for a way to register shutdown notification functions.
Once Shutdown has been called on a server, it may not be reused; future
Calls to methods such as Serve will return ErrServerClosed.
As can be seen from the document:
With Shutdown, the service can be terminated gracefully without interrupting active connections.
Its working process is: first turn off all open listeners, then close all idle connections, and finally wait for the active connections to be idle before terminating the service.
If the passed context times out before the service finishes terminating, the Shutdown method returns the error of context, otherwise it returns any errors caused by shutting down the service listener.
When the Shutdown method is called, the Serve, ListenAndServe, and ListenAndServeTLS methods immediately return an ErrServerClosed error. Make sure that you do not exit the program when Shutdown is not returned.
For persistent connections such as WebSocket, Shutdown does not attempt to close or wait for these connections. If necessary, additional processing is required separately by the caller (such as notifying persistent connections or waiting for them to close, using the RegisterOnShutdown registration termination notification function).
Once Shutdown is called on server, it can no longer be used (an ErrServerClosed error is reported).
With the Shutdown method, we know that calling this method can wait for the active connection to return normally before the service is terminated, and then gracefully close.
But at some point after the service starts, how does the program know that the service has been interrupted? How to notify the program when the service is interrupted, and then call Shutdown to handle it? Next, take a look at the function of the system signal notification function.
2 signal.Notify function
The Notify function of the signal package provides the ability to notify the system signal, and its go doc is as follows.
Func Notify (c chan
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.