網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
詳解Golang中Context的三個(gè)常見(jiàn)應(yīng)用場(chǎng)景_Golang
作者:夢(mèng)想畫(huà)家 ? 更新時(shí)間: 2023-02-05 編程語(yǔ)言超時(shí)取消
假設(shè)我們希望HTTP請(qǐng)求在給定時(shí)間內(nèi)完成,超時(shí)自動(dòng)取消。
首先定義超時(shí)上下文,設(shè)定時(shí)間返回取消函數(shù)(一旦超時(shí)用于清理資源)。調(diào)用取消函數(shù)取消后續(xù)操作,刪除子上下文對(duì)父的引用。
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*80) defer cancel() req = req.WithContext(ctx)
還可以通過(guò)特定時(shí)間進(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))
完整實(shí)例如下:
func main() { //定義請(qǐng)求 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í)行請(qǐng)求 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)) }
超時(shí)輸出結(jié)果:
2022/12/27 14:36:00 Get "https://www.baidu.com": context deadline exceeded
我們可以調(diào)大超時(shí)時(shí)間,則能正常看到輸出結(jié)果。
取消后續(xù)操作
有時(shí)請(qǐng)求被取消后,需要阻止系統(tǒng)繼續(xù)做后續(xù)比必要的工作。請(qǐng)看下面用戶發(fā)起的http請(qǐng)求,應(yīng)用程序接收請(qǐng)求后查詢數(shù)據(jù)庫(kù)并返回查詢結(jié)果:
正常流程如下:
但如果客戶端取消了請(qǐng)求,如果沒(méi)有取消,應(yīng)用服務(wù)和數(shù)據(jù)庫(kù)仍然繼續(xù)工作,然后結(jié)果卻不能反饋給客戶端。理想狀況為所有下游過(guò)程停止,如圖所示:
考慮有兩個(gè)相關(guān)操作的情況,“相關(guān)”的意思是如果一個(gè)失敗了,另一個(gè)即使完成也沒(méi)有意義了。如果已經(jīng)知道前一個(gè)操作失敗了,則希望取消所有相關(guān)的操作。請(qǐng)看示例:
func operation1(ctx context.Context) error { // 假設(shè)該操作因某種原因而失敗 // 下面模擬業(yè)務(wù)執(zhí)行一定時(shí)間 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í)行兩個(gè)操作 go func() { err := operation1(ctx) // 如果該方法返回錯(cuò)誤,則取消該上下文中的后續(xù)操作 if err != nil { cancel() } }() // 實(shí)用相同上下文執(zhí)行操作2 operation2(ctx) }
由于我們?cè)O(shè)置操作2執(zhí)行時(shí)間較長(zhǎng),而操作1很快就報(bào)錯(cuò),因此輸出結(jié)果為操作2被取消:
halted operation2
上下文傳值
我們可以實(shí)用上下文變量在不同協(xié)程中傳遞值。
假設(shè)一個(gè)操作需要調(diào)用函數(shù)多次,其中用于標(biāo)識(shí)的公共ID需要被 日志記錄,請(qǐng)看示例:
// 定義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)建上下文,并采用鍵值對(duì)方式存儲(chǔ)id值,從而后續(xù)函數(shù)調(diào)用時(shí)可以從上下文中獲取該值。如圖所示:
使用context變量在不同操作中傳遞信息非常有用,主要原因包括:
- 線程安全: 一旦設(shè)置了上下文鍵,就不能修改它的值,可以使用context.WithValue方法可以設(shè)置新的值
- 通用方法: 在Go的官方庫(kù)和應(yīng)用程序中大量使用上下文傳遞數(shù)據(jù),我們當(dāng)然最好也使用這種模式
原文鏈接:https://blog.csdn.net/neweastsun/article/details/128458477
相關(guān)推薦
- 2023-01-17 在1個(gè)Matlab?m文件中定義多個(gè)函數(shù)直接運(yùn)行的操作方法_C 語(yǔ)言
- 2023-01-11 jQuery綁定點(diǎn)擊事件與改變事件的方式總結(jié)及多個(gè)元素綁定多個(gè)事件_jquery
- 2022-12-05 kill一條TCP連接實(shí)現(xiàn)方法詳解_相關(guān)技巧
- 2022-07-04 python如何輸入根號(hào)_python
- 2022-10-11 ingress-nginx-url重寫(xiě)的經(jīng)驗(yàn)總結(jié)
- 2022-06-16 Kotlin操作符重載實(shí)例詳解_Android
- 2022-09-10 ELK收集Tomcat日志的實(shí)現(xiàn)_Tomcat
- 2022-11-25 C++關(guān)鍵字之likely和unlikely詳解_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支