溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Go語(yǔ)言中的上下文取消操作詳解

發(fā)布時(shí)間:2020-09-16 07:14:22 來(lái)源:腳本之家 閱讀:178 作者:HULK一線技術(shù)雜談 欄目:編程語(yǔ)言

前言

許多使用Go的人,都會(huì)用到它的上下文庫(kù)。大多數(shù)使用 context 進(jìn)行下游操作,比如發(fā)出HTTP調(diào)用,或者從數(shù)據(jù)庫(kù)獲取數(shù)據(jù),或者在協(xié)程中執(zhí)行異步操作。最常見(jiàn)的用法是傳遞可由所有下游操作使用的公共數(shù)據(jù)。然而,一個(gè)不太為人所知,但非常有用的上下文特性是,它能夠在中途取消或停止一個(gè)操作。

本篇文章將解釋我們?nèi)绾卫蒙舷挛膸?kù)的取消特性,并通過(guò)一些模式和最佳實(shí)踐來(lái)使用取消,使你的程序更快、更健壯。

為什么需要取消?

簡(jiǎn)而言之,我們需要取消,以防止我們的系統(tǒng)做不不需要的工作。

考慮HTTP服務(wù)器對(duì)數(shù)據(jù)庫(kù)的調(diào)用的常見(jiàn)情況,并將查詢的數(shù)據(jù)返回給客戶端:

Go語(yǔ)言中的上下文取消操作詳解

時(shí)間圖,如果一切都很完美,就會(huì)是這樣的:

Go語(yǔ)言中的上下文取消操作詳解

但是,如果客戶端取消了中間的請(qǐng)求,會(huì)發(fā)生什么呢?例如,如果客戶端關(guān)閉了他們的瀏覽器,這可能會(huì)發(fā)生。如果沒(méi)有取消,應(yīng)用服務(wù)器和數(shù)據(jù)庫(kù)將繼續(xù)執(zhí)行它們的工作,即使工作的結(jié)果將被浪費(fèi):

Go語(yǔ)言中的上下文取消操作詳解

理想情況下,如果我們知道進(jìn)程(在本例中是HTTP請(qǐng)求)停止了,我們希望流程的所有下游組件停止工作:

Go語(yǔ)言中的上下文取消操作詳解

1、上下文取消

現(xiàn)在我們知道了為什么需要取消,讓我們來(lái)看看如何實(shí)現(xiàn)它。因?yàn)椤叭∠钡氖录c交易或正在執(zhí)行的操作高度相關(guān),所以它與上下文捆綁在一起是很自然的。

取消的有兩個(gè)方面,你可能想要實(shí)現(xiàn):

  • 監(jiān)聽(tīng)取消事件
  • 提交取消事件

2、監(jiān)聽(tīng)取消事件

上下文類型提供了 Done() 方法,每當(dāng)上下文收到取消事件時(shí),它都會(huì)返回接收空 struct{} 類型的通道。監(jiān)聽(tīng)取消事件就像等待 <-ctx.done() 一樣簡(jiǎn)單。

例如,讓我們考慮一個(gè)HTTP服務(wù)器,它需要兩秒鐘來(lái)處理一個(gè)事件。如果在此之前請(qǐng)求被取消,我們希望立即返回:

func main() {
  // Create an HTTP server that listens on port 8000
 http.ListenAndServe(":8000", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 ctx := r.Context()
 // This prints to STDOUT to show that processing has started
 fmt.Fprint(os.Stdout, "processing request\n")
 // We use `select` to execute a peice of code depending on which
 // channel receives a message first
 select {
   case <-time.After(2 * time.Second):
 // If we receive a message after 2 seconds
 // that means the request has been processed
 // We then write this as the response
 w.Write([]byte("request processed"))
   case <-ctx.Done():
 // If the request gets cancelled, log it
 // to STDERR
 fmt.Fprint(os.Stderr, "request cancelled\n")
 }
 }))
}

你可以通過(guò)運(yùn)行服務(wù)器并在瀏覽器上打開(kāi)localhost:8000來(lái)測(cè)試。如果你在2秒前關(guān)閉瀏覽器,你應(yīng)該會(huì)看到在終端窗口上打印的“請(qǐng)求取消”。

3、提交取消事件

如果你有一個(gè)可以被取消的操作,你將不得不通過(guò)上下文發(fā)出取消事件。這可以通過(guò) context 包中的 WithCancel 函數(shù)來(lái)完成,它返回一個(gè)上下文對(duì)象和一個(gè)函數(shù)。這個(gè)函數(shù)沒(méi)有參數(shù),也不返回任何東西,當(dāng)你想要取消上下文時(shí)調(diào)用。

考慮兩個(gè)從屬操作的情況。在這里,“依賴”意味著如果一個(gè)失敗了,另一個(gè)就沒(méi)有意義了。在這種情況下,如果我們?cè)谠缙诰椭榔渲幸粋€(gè)操作失敗了,我們想要取消所有的依賴操作。

func operation1(ctx context.Context) error {
 // Let's assume that this operation failed for some reason
 // We use time.Sleep to simulate a resource intensive operation
 time.Sleep(100 * time.Millisecond)
 return errors.New("failed")
}

func operation2(ctx context.Context) {
 // We use a similar pattern to the HTTP server
 // that we saw in the earlier example
 select {
  case <-time.After(500 * time.Millisecond):
 fmt.Println("done")
  case <-ctx.Done():
 fmt.Println("halted operation2")
 }
}

func main() {
 // Create a new context
 ctx := context.Background()

 // Create a new context, with its cancellation function
 // from the original context
 ctx, cancel := context.WithCancel(ctx)

 // Run two operations: one in a different go routine
 go func() {
 err := operation1(ctx)
 // If this operation returns an error
 // cancel all operations using this context
 if err != nil {
 cancel()
 }
 }()
 // Run operation2 with the same context we use for operation1
 operation2(ctx)
}

4、基于時(shí)間取消

任何需要在請(qǐng)求的最大持續(xù)時(shí)間內(nèi)維護(hù)SLA(服務(wù)水平協(xié)議)的應(yīng)用程序都應(yīng)該使用基于時(shí)間的取消。該API幾乎與前面的示例相同,并添加了一些內(nèi)容:

// The context will be cancelled after 3 seconds
// If it needs to be cancelled earlier, the `cancel` function can
// be used, like before

ctx, cancel := context.WithTimeout(ctx, 3*time.Second)

// The context will be cancelled on 2009-11-10 23:00:00
ctx, cancel := context.WithDeadline(ctx, time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC))

例如,考慮對(duì)外部服務(wù)進(jìn)行HTTP API調(diào)用。如果服務(wù)花費(fèi)的時(shí)間太長(zhǎng),最好是盡早失敗并取消請(qǐng)求:

func main() {
 // Create a new context
 // With a deadline of 100 milliseconds
 ctx := context.Background()
 ctx, _ = context.WithTimeout(ctx, 100*time.Millisecond)
 // Make a request, that will call the google homepage
 req, _ := http.NewRequest(http.MethodGet, "http://google.com", nil)
 // Associate the cancellable context we just created to the request
 req = req.WithContext(ctx)

 // Create a new HTTP client and execute the request
 client := &http.Client{}
 res, err := client.Do(req)

 // If the request failed, log to STDOUT
 if err != nil {
 fmt.Println("Request failed:", err)
 return
 }

 // Print the statuscode if the request succeeds
 fmt.Println("Response received, status code:", res.StatusCode)
}

根據(jù)谷歌主頁(yè)對(duì)你的請(qǐng)求的響應(yīng)速度,你將收到:

Response received, status code: 200

或者

Request failed: Get http://google.com: context deadline exceeded

你可以使用超時(shí)來(lái)實(shí)現(xiàn)上述兩個(gè)結(jié)果。

陷阱和警告

盡管Go的上下文取消是一個(gè)通用的工具,但是在繼續(xù)之前,有一些事情是你應(yīng)該記住的。其中最重要的一點(diǎn)是, 上下文只能被取消一次 。

如果你想在同一個(gè)操作中提出多個(gè)錯(cuò)誤,那么使用上下文取消可能不是最好的選擇。使用取消的最慣用的方法是,當(dāng)你真正想要取消某些東西時(shí),而不僅僅是通知下游進(jìn)程,錯(cuò)誤已經(jīng)發(fā)生了。

你需要記住的另一件事是,相同的上下文實(shí)例應(yīng)該傳遞給所有你可能想要取消的功能和例程。用 WithTimeout 或 WithCancel 來(lái)包裝已經(jīng)可取消的上下文將會(huì)導(dǎo)致多種可能性,你的上下文可以被取消,并且應(yīng)該避免。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI