Go 标准库 - context 源码解析

本文深入解析Go标准库中的context包,主要关注其在goroutine间传递上下文信息的功能,包括取消信号、超时管理和键值对。context接口包含Deadline、Done、Err和Value方法,而canceler接口用于分离取消操作。文章详细介绍了emptyCtx、cancelCtx、timerCtx和valueCtx四个结构体的实现和用途,如emptyCtx作为默认和根节点,cancelCtx支持取消功能,timerCtx处理超时,valueCtx则用于存储键值对。
摘要由CSDN通过智能技术生成

context 主要用来在goroutine 之间传递上下文信息,包括取消信号、超时时间、截止时间、k-v等。标准库的许多接口加上了 context 参数,来实现并发控制和超时控制。

 一、源码解析

1. 接口

(1)Context

type Context interface {
	Deadline() (deadline time.Time, ok bool)
	Done() <-chan struct{}
	Err() error
	Value(key interface{}) interface{}
}

该接口定义了4个方法,分别是:

  • Deadline():返回 context 的截止时间及是否会被取消,根据这个时间决定是否执行接下来的操作
  • Done():当 context 被取消或到了 deadline 时,返回一个被关闭的 channel
  • Err():返回 channel 被关闭的原因
  • Value():获取之前设置的 key 对应的value

(2)canceler

type canceler interface {
	cancel(removeFromParent bool, err error)
	Done() <-chan struct{}
}

该接口将 context 与取消操作分离开 

2. 结构体

(1)emptyCtx

type emptyCtx int

func (*emptyCtx) Deadline() (deadline time.Time, ok bool) {
	return
}

func (*emptyCtx) Done() <-chan struct{} {
	return nil
}

func (*emptyCtx) Err() error {
	return nil
}

func (*emptyCtx) Value(key interface{}) interface{} {
	return nil
}

给出的一个默认实现,该context不会被取消、没有截止时间、没有存键值对,它被包装成两个结构,并提供导出函数对外公开

var (
	background = new(emptyCtx)
	todo       = new(emptyCtx)
)

func Background() Context {
	return background
}

func TODO() Context {
	return todo
}
  • background:通常用在 main 函数,作为所有 context 的根节点
  • <
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值