最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

golang如何去除 context 的 deadline

 更新時(shí)間:2023年03月21日 11:04:45   作者:Immortal_s  
在使用 context 的時(shí)候遇到了開協(xié)程處理任務(wù)的情況,但是直接在協(xié)程里使用主線程的 context 會(huì)導(dǎo)致當(dāng)主線程返回時(shí)協(xié)程任務(wù)也會(huì)因?yàn)?nbsp;context cancel 而失敗,本文提供了兩種辦法可以取消掉 context 里的 timeout 和 deadline,再設(shè)置一個(gè)新的 timeout 上去

golang 去除 context 的 deadline

背景

在使用 context 的時(shí)候遇到了開協(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

最簡單的方案是通過創(chuàng)建一個(gè)新的 context 來解決這個(gè)問題。

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)拿出來,再設(shè)置到新的里面去。 但是在很多情況下,這個(gè) context 是上游傳過來的,并不知道 value 里面有哪些具體的 key?;蛘呃锩娴?value 過多,寫起來很麻煩。

方法二,使用自定義結(jié)構(gòu)體

通過看 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 是通過 Deadline() 這個(gè)函數(shù)控制整個(gè) ctx 是否超時(shí)了的。那么我們就可以通過重寫這個(gè)函數(shù)來規(guī)避超時(shí)。

// contextWithoutDeadline 偽造了一個(gè)沒有 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)
}

可以通過 DetachDeadline() 方法來將原 ctx 的 deadline 取消掉,或者直接通過 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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

黄骅市| 昭觉县| 新和县| 屏南县| 汕尾市| 益阳市| 始兴县| 灵宝市| 鲁山县| 全椒县| 大丰市| 历史| 盱眙县| 富蕴县| 泾阳县| 申扎县| 千阳县| 陕西省| 都江堰市| 富宁县| 富蕴县| 通江县| 晴隆县| 临澧县| 白山市| 琼结县| 横峰县| 天峨县| 石狮市| 高雄市| 子洲县| 昌平区| 宁阳县| 潞城市| 泰安市| 雅安市| 凉城县| 巩义市| 类乌齐县| 安吉县| 华容县|