golang如何去除 context 的 deadline
golang 去除 context 的 deadline
背景
在使用 context 的時(shí)候遇到了開(kāi)協(xié)程處理任務(wù)的情況,但是直接在協(xié)程里使用主線程的 context 會(huì)導(dǎo)致當(dāng)主線程返回時(shí)協(xié)程任務(wù)也會(huì)因?yàn)?context cancel 而失敗。
本文提供了兩種辦法可以取消掉 context 里的 timeout 和 deadline,再設(shè)置一個(gè)新的 timeout 上去。
方法一,創(chuàng)建一個(gè)新 context
最簡(jiǎn)單的方案是通過(guò)創(chuàng)建一個(gè)新的 context 來(lái)解決這個(gè)問(wèn)題。
func main() { ctx := context.WithValue(context.Background(), "trace", "123") ctx, cancel := context.WithTimeout(ctx, time.Second) defer cancel() go func(ctx context.Context) { newCtx := context.WithValue(context.Background(), "trace", ctx.Value("trace")) // do something with newCtx }(ctx) fmt.Println("main finished") }
但是這種方案有一個(gè)缺點(diǎn),當(dāng)生成一個(gè)新的 context 的時(shí)候,需要手動(dòng)把老 context 中的 value 手動(dòng)拿出來(lái),再設(shè)置到新的里面去。 但是在很多情況下,這個(gè) context 是上游傳過(guò)來(lái)的,并不知道 value 里面有哪些具體的 key?;蛘呃锩娴?value 過(guò)多,寫(xiě)起來(lái)很麻煩。
方法二,使用自定義結(jié)構(gòu)體
通過(guò)看 context 的源碼,其實(shí)可以發(fā)現(xiàn) context 是一個(gè) interface,這就給了我們操作的空間。
type Context interface { Deadline() (deadline time.Time, ok bool) Done() <-chan struct{} Err() error Value(key any) any }
Context 是通過(guò) Deadline() 這個(gè)函數(shù)控制整個(gè) ctx 是否超時(shí)了的。那么我們就可以通過(guò)重寫(xiě)這個(gè)函數(shù)來(lái)規(guī)避超時(shí)。
// contextWithoutDeadline 偽造了一個(gè)沒(méi)有 deadline 的 context type contextWithoutDeadline struct { ctx context.Context } func (*contextWithoutDeadline) Deadline() (time.Time, bool) { return time.Time{}, false } func (*contextWithoutDeadline) Done() <-chan struct{} { return nil } func (*contextWithoutDeadline) Err() error { return nil } func (l *contextWithoutDeadline) Value(key interface{}) interface{} { return l.ctx.Value(key) } // DetachDeadline 從 context 剝離 deadline func DetachDeadline(ctx context.Context) context.Context { return &contextWithoutDeadline{ctx: ctx} } // SetNewTimeout 設(shè)置新的超時(shí)時(shí)間 func SetNewTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc) { return context.WithTimeout(DetachDeadline(ctx), timeout) }
可以通過(guò) DetachDeadline() 方法來(lái)將原 ctx 的 deadline 取消掉,或者直接通過(guò) SetNewTimeout 的方法設(shè)置一個(gè)新的超時(shí)時(shí)間上去。
演示
func main() { ctx, _ := context.WithTimeout(context.Background(), time.Duration(10)*time.Second) fmt.Println(ctx.Deadline()) newCtx := contextwarp.DetachDeadline(ctx) fmt.Println(newCtx.Deadline()) newCtx2, _ := contextwarp.SetNewTimeout(ctx, time.Duration(1)*time.Second) fmt.Println(newCtx2.Deadline()) }
符合預(yù)期。
到此這篇關(guān)于golang如何去除 context 的 deadline的文章就介紹到這了,更多相關(guān)go去除 context 的 deadline內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 提升Golang應(yīng)用性能:深入理解Context的應(yīng)用
- golang context接口類(lèi)型方法介紹
- Golang服務(wù)中context超時(shí)處理的方法詳解
- Golang中context包使用場(chǎng)景和示例詳解
- 一文帶你深入理解Golang Context包
- Golang上下文Context的常見(jiàn)應(yīng)用場(chǎng)景
- GoLang context包的使用方法介紹
- 詳解Golang中Context的原理和使用技巧
- golang中context的作用詳解
- 深入Golang之context的用法詳解
- golang中context使用小結(jié)
相關(guān)文章
Golang關(guān)鍵字select的常用用法總結(jié)
這篇文章主要為大家詳細(xì)介紹了golang中select關(guān)鍵字的常用用法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-10-10Golang如何快速構(gòu)建一個(gè)CLI小工具詳解
這篇文章主要為大家介紹了Golang如何快速構(gòu)建一個(gè)CLI小工具詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11golang switch語(yǔ)句的靈活寫(xiě)法介紹
這篇文章主要介紹了golang switch語(yǔ)句的靈活寫(xiě)法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-05-05Golang設(shè)計(jì)模式之工廠方法模式講解和代碼示例
工廠方法是一種創(chuàng)建型設(shè)計(jì)模式, 解決了在不指定具體類(lèi)的情況下創(chuàng)建產(chǎn)品對(duì)象的問(wèn)題,本文將通過(guò)代碼示例詳細(xì)給大家介紹一下Golang工廠方法模式,感興趣的同學(xué)可以參考一下2023-06-06Go中各種newreader和newbuffer的使用總結(jié)
這篇文章主要為大家詳細(xì)介紹了Go語(yǔ)言中各種newreader和newbuffer的使用的相關(guān)資料,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴可以了解下2023-11-11