在 Go 编程语言中,context 包提供了一个用于在 goroutine 之间传递上下文信息的方法。它通常用于控制 goroutine 的生命周期、传递请求范围内的数据、以及处理超时或取消信号。context 包的核心是 Context 接口和与之相关的函数和方法。

Context 接口

Context 接口定义如下:

type Context interface {
Deadline() (deadline time.Time, ok bool)
Done() <-chan struct{}
Err() error
Value(key interface{}) interface{}
}
  • Deadline() 返回上下文会被自动取消的时间,以及是否存在这样的时间。
  • Done() 返回一个 chan struct{},当上下文被取消或超时时,这个通道会被关闭。
  • Err() 返回上下文被取消的原因。如果 Done 通道已经关闭,它会返回一个非 nil 的错误。
  • Value(key interface{}) 返回与上下文关联的键对应的值。

创建 Context

通常有四种方式来创建一个 Context

  1. context.Background()
  2. context.TODO()
  3. context.WithCancel(parent Context)
  4. context.WithTimeout(parent Context, timeout time.Duration)
  5. context.WithDeadline(parent Context, deadline time.Time)
  6. context.WithValue(parent Context, key, val interface{})

例子

使用 context.Background()

context.Background() 返回一个空的上下文,通常用于主函数、初始化和测试。

ctx := context.Background()

使用 context.WithCancel

context.WithCancel 返回一个派生上下文和一个取消函数。当调用取消函数时,派生上下文的 Done 通道会被关闭。

ctx, cancel := context.WithCancel(context.Background())
defer cancel() // 确保资源被释放 go func() {
// 模拟一些工作
time.Sleep(2 * time.Second)
cancel() // 取消上下文
}() <-ctx.Done() // 等待上下文被取消
fmt.Println("Context canceled:", ctx.Err())

使用 context.WithTimeout

context.WithTimeout 返回一个派生上下文和一个取消函数。指定的超时时间过后,派生上下文的 Done 通道会被关闭。

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel() select {
case <-time.After(5 * time.Second):
fmt.Println("Operation completed")
case <-ctx.Done():
fmt.Println("Timeout:", ctx.Err())
}

使用 context.WithValue

context.WithValue 返回一个带有键值对的派生上下文,用于在请求的生命周期中传递数据。

type keyType string

func main() {
ctx := context.WithValue(context.Background(), keyType("userID"), 12345)
ProcessRequest(ctx)
} func ProcessRequest(ctx context.Context) {
userID := ctx.Value(keyType("userID")).(int)
fmt.Println("User ID:", userID)
}

典型应用场景

  1. API 请求处理: 在处理 HTTP 请求时,传递上下文以管理超时和取消。
  2. 并发任务管理: 使用上下文来控制和取消多个并发任务。
  3. 传递请求范围的数据: 例如用户身份验证信息、跟踪 ID 等。

注意事项

  • Context 是不可变的,应当通过 context.WithCancel, context.WithTimeout, context.WithDeadline, context.WithValue 等函数创建新的派生上下文。
  • 不要将 Context 存储在结构体中,应当作为函数的第一个参数传递。
  • 尽量在短生命周期的请求中使用 Context,避免长时间持有上下文。

通过正确使用 context 包,可以编写出更健壮、更易于维护的并发程序。

Go context 介绍的更多相关文章

  1. Golang的Context介绍及其源码分析

    简介 在Go服务中,对于每个请求,都会起一个协程去处理.在处理协程中,也会起很多协程去访问资源,比如数据库,比如RPC,这些协程还需要访问请求维度的一些信息比如说请求方的身份,授权信息等等.当一个请求 ...

  2. Tomcat下server.xml中context介绍

    conf/Context.xml是Tomcat公用的环境配置;若在server.xml中增加<Context path="/test" docBase="D:\te ...

  3. Go context 介绍和使用

    context 上下文管理 context 翻译过来就是上下文管理,主要作用有两个: 控制 goroutine 的超时 保存上下文数据 WithTimeout 通过下面的一个简单的 http 例子进行 ...

  4. Android Context介绍

    转载(Android Context完全解析与各种获取Context方法):https://www.cnblogs.com/chenxibobo/p/6136693.html

  5. 理解Go Context机制

    1 什么是Context 最近在公司分析gRPC源码,proto文件生成的代码,接口函数第一个参数统一是ctx context.Context接口,公司不少同事都不了解这样设计的出发点是什么,其实我也 ...

  6. Android上下文Context

    Android上下文Context介绍 在应用开发中最熟悉而陌生的朋友-----Context类 ,说它熟悉,是应为我们在开发中时刻的在与它打交道,例如:Service.BroadcastReceiv ...

  7. Golang 高效实践之并发实践context篇

    前言 在上篇Golang高效实践之并发实践channel篇中我给大家介绍了Golang并发模型,详细的介绍了channel的用法,和用select管理channel.比如说我们可以用channel来控 ...

  8. Go实现海量日志收集系统(三)

    再次整理了一下这个日志收集系统的框,如下图 这次要实现的代码的整体逻辑为: 完整代码地址为: https://github.com/pythonsite/logagent etcd介绍 高可用的分布式 ...

  9. Flask类的属性和方法大全

    Flask Property__class____dict____doc____module__app_ctx_globals_classconfig_classdebugdefault_config ...

  10. OpenGL Windows 窗口程序环境搭建

    OpenGL环境搭建步骤: Downloading OpenGL 根据官网的说法: In all three major desktop platforms (Linux, macOS, and Wi ...

随机推荐

  1. 虚拟 DOM 的优缺点?

    什么是虚拟dom用js模拟一颗dom树,放在浏览器内存中.当你要变更时,虚拟dom使用diff算法进行新旧虚拟dom的比较,将变更放到变更队列中, 反应到实际的dom树,减少了dom操作. 虚拟DOM ...

  2. Microsoft Compatibility telemetry占cpu资源高

    1.在Windows10系统卡的时候,打开任务管理器,发现Microsoft Compatibility telemetry占用了大量的系统资源,特别是CPU占用率非常高. 位置:控制面板->管 ...

  3. ELK Stack - Elasticsearch · 搜索引擎 · 部署应用 · 内部结构 · 倒排索引 · 服务接入

    系列目录 ELK Stack - Elasticsearch · 搜索引擎 · 全文检索 · 部署应用 · 内部结构 · 倒排索引 · 服务接入 ELK Stack - Kibana (待续) ELK ...

  4. leetcode简单(双指针):[88, 202, 345, 392, 455, 905, 922, 917, 925, 942]

    [toc 88. 合并两个有序数组 var merge = function(nums1, m, nums2, n) { let A1 = nums1.slice(0, m) let A2 = num ...

  5. CCF 无线网络

    题目原文 问题描述(题目链接登陆账号有问题,要从这个链接登陆,然后点击"模拟考试",进去找本题目) 试题编号: 201403-4 试题名称: 无线网络 时间限制: 1.0s 内存限 ...

  6. 搭建lnmp环境-redis(第四步)

    1.下载epel仓库 (前面安装过了) yum install epel-release -y 2.下载redis数据库 yum install redis -y 3.启动redis服务 system ...

  7. WorPress基础之谷歌GSC与GA统计代码安装

    本篇文章讲介绍什么是GSC和GA以及如何安装. 什么是GSC GSC,全称为Google Search Console,由谷歌官方提供的网站管理工具,可帮助监控和维护网站在Google 搜索结果中的展 ...

  8. perf 性能分析工具

    perf 性能分析工具 perf topperf recordperf reportperf listperf stat perf top -p <pid> 例如查看redis进程的内核调 ...

  9. 【Dos-BatchPrograming】04

    --1.PING 主机联通性检测 Microsoft Windows [版本 10.0.19041.746] (c) 2020 Microsoft Corporation. 保留所有权利. C:\Us ...

  10. NVIDIA 创始人黄仁勋谈创建公司的最初6个月 —— 不要去写商业计划书

    地址: https://www.youtube.com/watch?v=yU3GUHDf0mk