这篇文章主要讲解了“golang中Context的常见应用场景有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Golang中Context的常见应用场景有哪些”吧!超时取消假设我们希望H
这篇文章主要讲解了“golang中Context的常见应用场景有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Golang中Context的常见应用场景有哪些”吧!
假设我们希望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 beforectx, 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:00ctx, 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()}}()// 实用相同上下文执行操作2operation2(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 keylog.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 nextlog.Println("operation2 for id:", ctx.Value(keyID), " completed")}
这里在main函数中创建上下文,并采用键值对方式存储id值,从而后续函数调用时可以从上下文中获取该值。如图所示:
使用context变量在不同操作中传递信息非常有用,主要原因包括:
感谢各位的阅读,以上就是“Golang中Context的常见应用场景有哪些”的内容了,经过本文的学习后,相信大家对Golang中Context的常见应用场景有哪些这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!
--结束END--
本文标题: Golang中Context的常见应用场景有哪些
本文链接: https://www.lsjlt.com/news/347685.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-05
2024-04-05
2024-04-05
2024-04-04
2024-04-05
2024-04-05
2024-04-05
2024-04-05
2024-04-04
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0