主頁 > 知識庫 > Go語言中的上下文取消操作詳解

Go語言中的上下文取消操作詳解

熱門標(biāo)簽:浙江高頻外呼系統(tǒng)多少錢一個月 濱州自動電銷機器人排名 汕頭小型外呼系統(tǒng) 惠州電銷防封電話卡 釘釘有地圖標(biāo)注功能嗎 鄭州亮點科技用的什么外呼系統(tǒng) 黃岡人工智能電銷機器人哪個好 建造者2地圖標(biāo)注 阿里云ai電話機器人

前言

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

本篇文章將解釋我們?nèi)绾卫蒙舷挛膸斓娜∠匦?,并通過一些模式和最佳實踐來使用取消,使你的程序更快、更健壯。

為什么需要取消?

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

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


時間圖,如果一切都很完美,就會是這樣的:

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

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

1、上下文取消

現(xiàn)在我們知道了為什么需要取消,讓我們來看看如何實現(xiàn)它。因為“取消”的事件與交易或正在執(zhí)行的操作高度相關(guān),所以它與上下文捆綁在一起是很自然的。

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

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

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

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

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

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")
 }
 }))
}

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

3、提交取消事件

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

考慮兩個從屬操作的情況。在這里,“依賴”意味著如果一個失敗了,另一個就沒有意義了。在這種情況下,如果我們在早期就知道其中一個操作失敗了,我們想要取消所有的依賴操作。

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、基于時間取消

任何需要在請求的最大持續(xù)時間內(nèi)維護(hù)SLA(服務(wù)水平協(xié)議)的應(yīng)用程序都應(yīng)該使用基于時間的取消。該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))

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

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īng)速度,你將收到:

Response received, status code: 200

或者

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

你可以使用超時來實現(xiàn)上述兩個結(jié)果。

陷阱和警告

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

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

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

總結(jié)

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

您可能感興趣的文章:
  • 在Django的上下文中設(shè)置變量的方法
  • Go語言命令行操作命令詳細(xì)介紹
  • Go語言運行環(huán)境安裝詳細(xì)教程
  • Golang極簡入門教程(一):基本概念
  • Golang極簡入門教程(四):編寫第一個項目
  • GO語言(golang)基礎(chǔ)知識

標(biāo)簽:昭通 晉中 泰安 瀘州 駐馬店 滄州 東營 阿壩

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Go語言中的上下文取消操作詳解》,本文關(guān)鍵詞  語言,中的,上下文,取消,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Go語言中的上下文取消操作詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于Go語言中的上下文取消操作詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章