首页 > 编程技术 > Golang

详解Golang中Context的三个常见应用场景

发布时间:2022-12-30 14:05 作者:梦想画家

超时取消

假设我们希望HTTP请求在给定时间内完成,超时自动取消。

首先定义超时上下文,设定时间返回取消函数(一旦超时用于清理资源)。调用取消函数取消后续操作,删除子上下文对父的引用。

    ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*80)
    defer cancel()
    req = req.WithContext(ctx)

还可以通过特定时间进行设定:

/ 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)

    // 执行请求
	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))
}

超时输出结果:

2022/12/27 14:36:00 Get "https://www.baidu.com": context deadline exceeded

我们可以调大超时时间,则能正常看到输出结果。

取消后续操作

有时请求被取消后,需要阻止系统继续做后续比必要的工作。请看下面用户发起的http请求,应用程序接收请求后查询数据库并返回查询结果:

正常流程如下:

但如果客户端取消了请求,如果没有取消,应用服务和数据库仍然继续工作,然后结果却不能反馈给客户端。理想状况为所有下游过程停止,如图所示:

考虑有两个相关操作的情况,“相关”的意思是如果一个失败了,另一个即使完成也没有意义了。如果已经知道前一个操作失败了,则希望取消所有相关的操作。请看示例:

func operation1(ctx context.Context) error {
	// 假设该操作因某种原因而失败
	// 下面模拟业务执行一定时间
	time.Sleep(100 * time.Millisecond)
	return errors.New("failed")
}

func operation2(ctx context.Context) {
	// 该方法要么正常执行完成
	// 要么取消,不再继续执行
	select {
	case <-time.After(500 * time.Millisecond):
		fmt.Println("done")
	case <-ctx.Done():
		fmt.Println("halted operation2")
	}
}

func main() {
	// 创建上下文
	ctx := context.Background()

	// 基于上下文创建需求上下文
	ctx, cancel := context.WithCancel(ctx)

	// 在不同协程中执行两个操作
	go func() {
		err := operation1(ctx)
		// 如果该方法返回错误,则取消该上下文中的后续操作
		if err != nil {
			cancel()
		}
	}()

	// 实用相同上下文执行操作2
	operation2(ctx)
}

由于我们设置操作2执行时间较长,而操作1很快就报错,因此输出结果为操作2被取消:

halted operation2

上下文传值

我们可以实用上下文变量在不同协程中传递值。

假设一个操作需要调用函数多次,其中用于标识的公共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函数中创建上下文,并采用键值对方式存储id值,从而后续函数调用时可以从上下文中获取该值。如图所示:

使用context变量在不同操作中传递信息非常有用,主要原因包括:

到此这篇关于详解Golang中Context的三个常见应用场景的文章就介绍到这了,更多相关Golang Context内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

原文出处:https://blog.csdn.net/neweastsun/article/details/128458477

标签:[!--infotagslink--]

您可能感兴趣的文章: