詳解Golang中Context的三個常見應(yīng)用場景
超時取消
假設(shè)我們希望HTTP請求在給定時間內(nèi)完成,超時自動取消。
首先定義超時上下文,設(shè)定時間返回取消函數(shù)(一旦超時用于清理資源)。調(diào)用取消函數(shù)取消后續(xù)操作,刪除子上下文對父的引用。
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*80)
defer cancel()
req = req.WithContext(ctx)
還可以通過特定時間進(jìn)行設(shè)定:
/ 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) ???????// Setting a context deadline is similar to setting a timeout, except // you specify a time when you want the context to cancel, rather than a duration. // Here, the context will be cancelled on 2022-11-10 23:00:00 ctx, cancel := context.WithDeadline(ctx, time.Date(2022, time.November, 10, 23, 0, 0, 0, time.UTC))
完整實例如下:
func main() {
//定義請求
req, err := http.NewRequest(http.MethodGet, "https://www.baidu.com", nil)
if err != nil {
log.Fatal(err)
}
// 定義上下文
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*80)
defer cancel()
req = req.WithContext(ctx)
// 執(zhí)行請求
c := &http.Client{}
res, err := c.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
// 輸出日志
out, err := io.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
log.Println(string(out))
}超時輸出結(jié)果:
2022/12/27 14:36:00 Get "https://www.baidu.com": context deadline exceeded
我們可以調(diào)大超時時間,則能正常看到輸出結(jié)果。
取消后續(xù)操作
有時請求被取消后,需要阻止系統(tǒng)繼續(xù)做后續(xù)比必要的工作。請看下面用戶發(fā)起的http請求,應(yīng)用程序接收請求后查詢數(shù)據(jù)庫并返回查詢結(jié)果:
正常流程如下:

但如果客戶端取消了請求,如果沒有取消,應(yīng)用服務(wù)和數(shù)據(jù)庫仍然繼續(xù)工作,然后結(jié)果卻不能反饋給客戶端。理想狀況為所有下游過程停止,如圖所示:

考慮有兩個相關(guān)操作的情況,“相關(guān)”的意思是如果一個失敗了,另一個即使完成也沒有意義了。如果已經(jīng)知道前一個操作失敗了,則希望取消所有相關(guān)的操作。請看示例:
func operation1(ctx context.Context) error {
// 假設(shè)該操作因某種原因而失敗
// 下面模擬業(yè)務(wù)執(zhí)行一定時間
time.Sleep(100 * time.Millisecond)
return errors.New("failed")
}
func operation2(ctx context.Context) {
// 該方法要么正常執(zhí)行完成
// 要么取消,不再繼續(xù)執(zhí)行
select {
case <-time.After(500 * time.Millisecond):
fmt.Println("done")
case <-ctx.Done():
fmt.Println("halted operation2")
}
}
func main() {
// 創(chuàng)建上下文
ctx := context.Background()
// 基于上下文創(chuàng)建需求上下文
ctx, cancel := context.WithCancel(ctx)
// 在不同協(xié)程中執(zhí)行兩個操作
go func() {
err := operation1(ctx)
// 如果該方法返回錯誤,則取消該上下文中的后續(xù)操作
if err != nil {
cancel()
}
}()
// 實用相同上下文執(zhí)行操作2
operation2(ctx)
}
由于我們設(shè)置操作2執(zhí)行時間較長,而操作1很快就報錯,因此輸出結(jié)果為操作2被取消:
halted operation2
上下文傳值
我們可以實用上下文變量在不同協(xié)程中傳遞值。

假設(shè)一個操作需要調(diào)用函數(shù)多次,其中用于標(biāo)識的公共ID需要被 日志記錄,請看示例:
// 定義key,用于保存上下文值的鍵
const keyID = "id"
func main() {
// 定義上下文值
rand.Seed(time.Now().Unix())
ctx := context.WithValue(context.Background(), keyID, rand.Int())
operation1(ctx)
}
func operation1(ctx context.Context) {
// do some work
// we can get the value from the context by passing in the key
log.Println("operation1 for id:", ctx.Value(keyID), " completed")
operation2(ctx)
}
func operation2(ctx context.Context) {
// do some work
// this way, the same ID is passed from one function call to the next
log.Println("operation2 for id:", ctx.Value(keyID), " completed")
}
這里在main函數(shù)中創(chuàng)建上下文,并采用鍵值對方式存儲id值,從而后續(xù)函數(shù)調(diào)用時可以從上下文中獲取該值。如圖所示:
使用context變量在不同操作中傳遞信息非常有用,主要原因包括:
- 線程安全: 一旦設(shè)置了上下文鍵,就不能修改它的值,可以使用context.WithValue方法可以設(shè)置新的值
- 通用方法: 在Go的官方庫和應(yīng)用程序中大量使用上下文傳遞數(shù)據(jù),我們當(dāng)然最好也使用這種模式
到此這篇關(guān)于詳解Golang中Context的三個常見應(yīng)用場景的文章就介紹到這了,更多相關(guān)Golang Context內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go標(biāo)準(zhǔn)庫Flag庫和Log庫的使用
本文主要介紹了Go標(biāo)準(zhǔn)庫Flag庫和Log庫的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05
Go標(biāo)準(zhǔn)庫http與fasthttp服務(wù)端性能對比場景分析
這篇文章主要介紹了Go標(biāo)準(zhǔn)庫http與fasthttp服務(wù)端性能比較,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06

