詳解Golang如何優(yōu)雅的終止一個(gè)服務(wù)
前言
采用常規(guī)方式啟動(dòng)一個(gè) Golang http 服務(wù)時(shí),若服務(wù)被意外終止或中斷,即未等待服務(wù)對(duì)現(xiàn)有請(qǐng)求連接處理并正常返回且亦未對(duì)服務(wù)停止前作一些必要的處理工作,這樣即會(huì)造成服務(wù)硬終止。這種方式不是很優(yōu)雅。
參看如下代碼,該 http 服務(wù)請(qǐng)求路徑為根路徑,請(qǐng)求該路徑,其會(huì)在 2s 后返回 hello。
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) }
若服務(wù)啟動(dòng)后,請(qǐng)求http://localhost:8080/,然后使用 Ctrl+C 立即中斷服務(wù),服務(wù)即會(huì)立即退出(exit status 2),請(qǐng)求未正常返回(ERR_CONNECTION_REFUSED),連接即馬上斷了。
接下來(lái)介紹使用 http.Server 的 Shutdown 方法結(jié)合 signal.Notify 來(lái)優(yōu)雅的終止服務(wù)。
1 Shutdown 方法
Golang http.Server 結(jié)構(gòu)體有一個(gè)終止服務(wù)的方法 Shutdown,其 go doc 如下。
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.
由文檔可知:
使用 Shutdown 可以優(yōu)雅的終止服務(wù),其不會(huì)中斷活躍連接。
其工作過(guò)程為:首先關(guān)閉所有開(kāi)啟的監(jiān)聽(tīng)器,然后關(guān)閉所有閑置連接,最后等待活躍的連接均閑置了才終止服務(wù)。
若傳入的 context 在服務(wù)完成終止前已超時(shí),則 Shutdown 方法返回 context 的錯(cuò)誤,否則返回任何由關(guān)閉服務(wù)監(jiān)聽(tīng)器所引起的錯(cuò)誤。
當(dāng) Shutdown 方法被調(diào)用時(shí),Serve、ListenAndServe 及 ListenAndServeTLS 方法會(huì)立刻返回 ErrServerClosed 錯(cuò)誤。請(qǐng)確保 Shutdown 未返回時(shí),勿退出程序。
對(duì)諸如 WebSocket 等的長(zhǎng)連接,Shutdown 不會(huì)嘗試關(guān)閉也不會(huì)等待這些連接。若需要,需調(diào)用者分開(kāi)額外處理(諸如通知諸長(zhǎng)連接或等待它們關(guān)閉,使用 RegisterOnShutdown 注冊(cè)終止通知函數(shù))。
一旦對(duì) server 調(diào)用了 Shutdown,其即不可再使用了(會(huì)報(bào) ErrServerClosed 錯(cuò)誤)。
有了 Shutdown 方法,我們知道在服務(wù)終止前,調(diào)用該方法即可等待活躍連接正常返回,然后優(yōu)雅的關(guān)閉。
但服務(wù)啟動(dòng)后的某一時(shí)刻,程序如何知道服務(wù)被中斷了呢?服務(wù)被中斷時(shí)如何通知程序,然后調(diào)用 Shutdown 作處理呢?接下來(lái)看一下系統(tǒng)信號(hào)通知函數(shù)的作用。
2 signal.Notify 函數(shù)
signal 包的 Notify 函數(shù)提供系統(tǒng)信號(hào)通知的能力,其 go doc 如下。
func Notify(c chan<- os.Signal, sig ...os.Signal)
Notify causes package signal to relay incoming signals to c. If no signals
are provided, all incoming signals will be relayed to c. Otherwise, just the
provided signals will.Package signal will not block sending to c: the caller must ensure that c
has sufficient buffer space to keep up with the expected signal rate. For a
channel used for notification of just one signal value, a buffer of size 1
is sufficient.It is allowed to call Notify multiple times with the same channel: each call
expands the set of signals sent to that channel. The only way to remove
signals from the set is to call Stop.It is allowed to call Notify multiple times with different channels and the
same signals: each channel receives copies of incoming signals
independently.
由文檔可知:
參數(shù) c 是調(diào)用者的信號(hào)接收通道,Notify 可將進(jìn)入的信號(hào)轉(zhuǎn)到 c。sig 參數(shù)為需要轉(zhuǎn)發(fā)的信號(hào)類型,若不指定,所有進(jìn)入的信號(hào)都將會(huì)轉(zhuǎn)到 c。
信號(hào)不會(huì)阻塞式的發(fā)給 c:調(diào)用者需確保 c 有足夠的緩沖空間,以應(yīng)對(duì)指定信號(hào)的高頻發(fā)送。對(duì)于用于通知僅一個(gè)信號(hào)值的通道,緩沖大小為 1 即可。
同一個(gè)通道可以調(diào)用 Notify 多次:每個(gè)調(diào)用擴(kuò)展了發(fā)送至該通道的信號(hào)集合。僅可調(diào)用 Stop 來(lái)從信號(hào)集合移除信號(hào)。
允許不同的通道使用同樣的信號(hào)參數(shù)調(diào)用 Notify 多次:每個(gè)通道獨(dú)立的接收進(jìn)入信號(hào)的副本。
綜上,有了 signal.Notify,傳入一個(gè) chan 并指定中斷參數(shù),這樣當(dāng)系統(tǒng)中斷時(shí),即可接收到信號(hào)。
參看如下代碼,當(dāng)使用 Ctrl+C 時(shí),c 會(huì)接收到中斷信號(hào),程序會(huì)在打印“program interrupted”語(yǔ)句后退出。
func main() { ? ? c := make(chan os.Signal) ? ? signal.Notify(c, os.Interrupt) ? ? <-c ? ? log.Fatal("program interrupted") }
$ go run main.go
Ctrl+C
2019/06/11 17:59:11 program interrupted
exit status 1
3 Server 優(yōu)雅的終止
接下來(lái)我們使用如上 signal.Notify 結(jié)合 http.Server 的 Shutdown 方法實(shí)現(xiàn)服務(wù)優(yōu)雅的終止。
如下代碼,Handler 與文章開(kāi)始時(shí)的處理邏輯一樣,其會(huì)在2s后返回 hello。
創(chuàng)建一個(gè) http.Server 實(shí)例,指定端口與 Handler。
聲明一個(gè) processed chan,其用來(lái)保證服務(wù)優(yōu)雅的終止后再退出主 goroutine。
新啟一個(gè) goroutine,其會(huì)監(jiān)聽(tīng) os.Interrupt 信號(hào),一旦服務(wù)被中斷即調(diào)用服務(wù)的 Shutdown 方法,確?;钴S連接的正常返回(本代碼使用的 Context 超時(shí)時(shí)間為 3s,大于服務(wù) Handler 的處理時(shí)間,所以不會(huì)超時(shí))。
處理完成后,關(guān)閉 processed 通道,最后主 goroutine 退出。
代碼同時(shí)托管在 GitHub,歡迎關(guān)注(github.com/olzhy/go-excercises)。
var addr = flag.String("server addr", ":8080", "server address") func main() { ? ? // handler ? ? handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ? ? ? ? time.Sleep(2 * time.Second) ? ? ? ? fmt.Fprintln(w, "hello") ? ? }) ? ? // server ? ? srv := http.Server{ ? ? ? ? Addr: ? ?*addr, ? ? ? ? Handler: handler, ? ? } ? ? // make sure idle connections returned ? ? processed := make(chan struct{}) ? ? go func() { ? ? ? ? c := make(chan os.Signal, 1) ? ? ? ? signal.Notify(c, os.Interrupt) ? ? ? ? <-c ? ? ? ? ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) ? ? ? ? defer cancel() ? ? ? ? if err := srv.Shutdown(ctx); nil != err { ? ? ? ? ? ? log.Fatalf("server shutdown failed, err: %v\n", err) ? ? ? ? } ? ? ? ? log.Println("server gracefully shutdown") ? ? ? ? close(processed) ? ? }() ? ? // serve ? ? err := srv.ListenAndServe() ? ? if http.ErrServerClosed != err { ? ? ? ? log.Fatalf("server not gracefully shutdown, err :%v\n", err) ? ? } ? ? // waiting for goroutine above processed ? ? <-processed }
總結(jié)
到此這篇關(guān)于Golang如何優(yōu)雅的終止一個(gè)服務(wù)的文章就介紹到這了,更多相關(guān)Golang終止服務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
go程序測(cè)試CPU占用率統(tǒng)計(jì)ps?vs?top兩種不同方式對(duì)比
這篇文章主要為大家介紹了go程序測(cè)試CPU占用率統(tǒng)計(jì)ps?vs?top兩種不同方式對(duì)比,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05基于HLS創(chuàng)建Golang視頻流服務(wù)器的優(yōu)缺點(diǎn)
HLS 是 HTTP Live Streaming 的縮寫(xiě),是蘋(píng)果開(kāi)發(fā)的一種基于 HTTP 的自適應(yīng)比特率流媒體傳輸協(xié)議。這篇文章主要介紹了基于 HLS 創(chuàng)建 Golang 視頻流服務(wù)器,需要的朋友可以參考下2021-08-08Go語(yǔ)言學(xué)習(xí)之操作MYSQL實(shí)現(xiàn)CRUD
Go官方提供了database包,database包下有sql/driver。該包用來(lái)定義操作數(shù)據(jù)庫(kù)的接口,這保證了無(wú)論使用哪種數(shù)據(jù)庫(kù),操作方式都是相同的。本文就來(lái)和大家聊聊Go語(yǔ)言如何操作MYSQL實(shí)現(xiàn)CRUD,希望對(duì)大家有所幫助2023-02-02go并發(fā)實(shí)現(xiàn)素?cái)?shù)篩的代碼
這篇文章主要介紹了go并發(fā)實(shí)現(xiàn)素?cái)?shù)篩的代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03淺析Go語(yǔ)言中的緩沖區(qū)及其在fmt包中的應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Go語(yǔ)言中的緩沖區(qū)及其在fmt包中的應(yīng)用的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2024-01-01Go中時(shí)間與時(shí)區(qū)問(wèn)題的深入講解
go語(yǔ)言中如果不設(shè)置指定的時(shí)區(qū),通過(guò)time.Now()獲取到的就是本地時(shí)區(qū),下面這篇文章主要給大家介紹了關(guān)于Go中時(shí)間與時(shí)區(qū)問(wèn)題的相關(guān)資料,需要的朋友可以參考下2021-12-12