导语 | 本文推选自腾讯云开发者社区-【技思广益 · 腾讯技术人原创集】专栏。该专栏是腾讯云开发者社区为腾讯技术人与宽泛开发者打造的分享交换窗口。栏目邀约腾讯技术人分享原创的技术积淀,与宽泛开发者互启迪共成长。本文作者是腾讯后端开发工程师陈雪锋。
context包比拟小,是浏览源码比拟现实的一个动手,并且外面也涵盖了许多go设计理念能够学习。
go的Context作为go并发形式的一种,无论是在源码net/http中,开源框架例如gin中,还是外部框架trpc-go中都是一个比拟重要的存在,而整个 context 的实现也就不到600行,所以也想借着这次机会来学习学习,本文基于go 1.18.4。话不多说,例:
为了使可能对context不太熟悉的同学有个相熟,先来个example ,摘自源码:
咱们利用WithCancel创立一个可勾销的Context,并且遍历频道输入,当 n==5时,被动调用cancel来勾销。
而在gen func中有个协程来监听ctx当监听到ctx.Done()即被勾销后就退出协程。
func main(){
gen := func(ctx context.Context) <-chan int {
dst := make(chan int)
n := 1
go func() {
for {
select {
case <-ctx.Done():
close(dst)
return // returning not to leak the goroutine
case dst <- n:
n++
}
}
}()
return dst
}
ctx, cancel := context.WithCancel(context.Background())
// defer cancel() // 理论应用中应该在这里调用 cancel
for n := range gen(ctx) {
fmt.Println(n)
if n == 5 {
cancel() // 这里为了使不相熟 go 的更能明确在这里调用了 cancel()
break
}
}
// Output:
// 1
// 2
// 3
// 4
// 5
}
这是最根本的应用办法。
概览
对于context包先上一张图,便于大家有个初步理解(外部函数并未全列举,后续会逐个解说):
最重要的就是左边的接口局部,能够看到有几个比拟重要的接口,上面逐个来说下:
type Context interface{
Deadline() (deadline time.Time, ok bool)
Done() <-chan struct{} Err() error
Value(key any) any
}
首先就是Context接口,这是整个context包的外围接口,就蕴含了四个 method,别离是:
Deadline() (deadline time.Time, ok bool) // 获取 deadline 工夫,如果没有的话 ok 会返回 false
Done() <-chan struct{} // 返回的是一个 channel ,用来利用监听工作是否曾经实现
Err() error // 返回勾销起因 例如:Canceled\DeadlineExceeded
Value(key any) any // 依据指定的 key 获取是否存在其 value 有则返回
能够看到这个接口十分清晰简单明了,并且没有过多的Method,这也是go 设计理念,接口尽量简略、玲珑,通过组合来实现丰盛的性能,前面会看到如何组合的。
再来看另一个接口canceler,这是一个勾销接口,其中一个非导出 method cancel,接管一个bool和一个error,bool用来决定是否将其从父Context中移除,err用来表明被勾销的起因。还有个Done()和Context接口一样,这个接口为何这么设计,前面再揭晓。
type canceler interface{
cancel(removeFromParent bool, err error)
Done() <-chan struct{}
}
接下来看这两个接口的实现者都有谁,首先Context间接实现者有 valueCtx(比较简单放最初讲)和emptyCtx
而canceler间接实现者有cancelCtx和timerCtx ,并且这两个同时也实现了Context接口(记住我后面说得另外两个是间接实现,这俩是嵌套接口实现松耦合,前面再说具体益处),上面逐个解说每个实现。
空的
见名知义,这是一个空实现,事实也的确如此,能够看到啥啥都没有,就是个空实现,为何要写呢?
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 any) any { return nil}
func (e *emptyCtx) String() string { switch e { case background: return "context.Background" case todo:
return "context.TODO" } return "unknown empty Context"}
再往下读源码会发现两个有意思的变量,底层截然不同,一个取名叫 background,一个取名叫todo,为何呢?急躁的能够看看解释,其实是为了不便大家辨别应用,背景 是在入口处来传递最初始的context,而todo 则是当你不晓得用啥,或者你的函数尽管接管ctontext参数,然而并没有做任何实现时,那么就应用todo即可。后续如果有具体实现再传入具体的上下文。所以下面才定义了一个空实现,就为了给这俩应用呢,这俩也是咱们最常在入口处应用的。
var (
background = new(emptyCtx)
todo = new(emptyCtx)
)
// Background returns a non-nil, empty Context. It is never canceled, has no
// values, and has no deadline. It is typically used by the main function,
// initialization, and tests, and as the top-level Context for incoming
// requests.
func Background() Context {
return background
}
// TODO returns a non-nil, empty Context. Code should use context.TODO when
// it's unclear which Context to use or it is not yet available (because the
// surrounding function has not yet been extended to accept a Context
// parameter).
func TODO() Context {
return todo
}
上面再看看具体的定义吧。
cancelCtx与timerCtx、valueCtx
type cancelCtx struct{
Context
mu sync.Mutex // 锁住上面字段的操作
// 寄存的是 chan struct{}, 懒创立,
// 只有第一次被 cancel 时才会敞开
done atomic.Value
// children 寄存的是子 Context canceler ,并且当第一次被 cancel 时会被
// 设为 nil
children map[canceler]struct{}
// 第一次被调用 cancel 时,会被设置
err error
}
type timerCtx struct{
cancelCtx
timer *time.Timer // 定时器,用来监听是否超时该勾销
deadline time.Time // 终止工夫
}
type valueCtx struct {
Context
key, val any
}
这里就看进去为何cancelCtx为非导出了,因为它通过内嵌Context接口也也是实现了Context的。并且通过这种形式实现了松耦合,能够通过 WithCancel(父Context) (ctx Context,cancel CancelFunc) 来传递任何自定义的Context实现。
而timerCtx是嵌套的cancelCtx,同样他也能够同时调用Context接口所有 method与cancelCtx所有method ,并且还能够重写局部办法。而 valueCtx和下面两个比拟独立,所以间接嵌套的Context。
这里应该也看明确了为何canceler为何一个可导出Done一个不可导出 cancel,Done是重写Context的method会由下层调用,所以要可导出, cancel则是由return func(){c.cancel(false,DeadlineExeceed) 相似的封装导出,所以不应该导出。
这是go中推崇的通过组合而非继承来编写代码。其中字段解释我已在前面注明,前面也会讲到。看懂了大的一个设计理念,上面咱们就逐个击破,通过下面能够看到timerCtx其实是复用了cancelCtx能力,所以cancelCtx最为重要,上面咱们就先将cancelCtx实现。
勾销
它非导出,是通过一个办法来间接返回Context类型的,这也是go理念之一,不裸露实现者,只裸露接口(前提是实现者中的可导出method不蕴含接口之外的method, 否则导出的method里面也无奈调用)。
先看看内部构造函数WithCancel,
- 先判断parent是否为nil,如果为nil就panic,这是为了防止到处判断是否为nil。所以永远不要应用nil来作为一个Context传递。
- 接着将父Context封装到cancelCtx并返回,这没啥说得,尽管只有一行代码,然而多处应用,所以做了封装,并且后续如果要更改行为调用者也无需更改。很不便。
- 调用propagateCancel,这个函数作用就是当parent是能够被勾销的时候就会对子Context也进行勾销的勾销或者筹备勾销动作。
- 返回Context与CancelFunc type >CancelFunc func()就是一个 type func别名,底层封装的是c.cancel办法,为何这么做呢?这是为了给下层利用一个对立的调用,cancelCtx与timerCtx以及其余能够实现不同的cancel然而对下层是通明并且统一的行为就可。这个func应该是协程平安并且屡次调用只有第一次调用才有成果。
func WithCancel(parent Context) (ctx Context, cancel CancelFunc){
if parent == nil {
panic("cannot create context from nil parent")
}
c := newCancelCtx(parent)
propagateCancel(parent, &c)
return&c, func() { c.cancel(true, Canceled) }
}
func newCancelCtx(parent Context) cancelCtx {
return cancelCtx{Context: parent}
}
接下来就来到比拟重要的func propagateCancel,咱们看看它做了啥,
首先是判断父context的Done()办法返回的channel是否为nil,如果是则间接返回啥也不做了。这是因为父Context从来不会被勾销的话,那就没必要进行上面动作。这也表名咱们应用.与猫(上下文。Background()) 这个函数是不会做任何动作的。
done := parent.Done()
if done == nil {
return // parent is never canceled
}
接下里就是一个select ,如果父Context曾经被勾销了的话,那就间接勾销子Context就好了,这个也理所应当,父亲都被勾销了,儿子当然也应该勾销,没有存在必要了。
select {
case <-done:
// parent is already canceled
child.cancel(false, parent.Err())
return
default:
}
如果父 Context 没有被勾销,这里就会做个判断,
- 看看parent是否是一个*cancelCtx,如果是的话就返回其p,再次查看 p.err是否为nil,如果不为nil就阐明parent被勾销,接着勾销 子 Context,如果没被勾销的话,就将其退出到p.children中,看到这里的 map是个canceler,能够接管任何实现勾销器 的类型。这里为何要加锁呢?因为要对p.err以及p.children进行读取与写入操作,要确保协程平安所以才加的锁。
- 如果不是*cancelCtx类型就阐明parent是个被封装的其余实现 Context 接口的类型,则会将goroutines是个int加1这是为了测试应用的,能够不论它。并且会启动个协程,监听父Context ,如果父Context被勾销,则勾销子Context,如果监听到子Context曾经完结(可能是下层被动调用CancelFunc)则就啥也不必做了。
if p, ok := parentCancelCtx(parent); ok {
p.mu.Lock()
if p.err != nil {
// parent has already been canceled
child.cancel(false, p.err)
} else {
if p.children == nil {
p.children = make(map[canceler]struct{})
}
p.children[child] = struct{}{}
}
p.mu.Unlock()
} else {
atomic.AddInt32(&goroutines, +1)
go func() {
select {
case <-parent.Done():
child.cancel(false, parent.Err())
case <-child.Done():
}
}()
}
接下来看看parentCancelCtx的实现:它是为了找寻parent底下的 *cancelCtx,
它首先查看parent.Done()如果是一个closedchan这个频道 在初始化时曾经是个一个被敞开的通道或者未nil的话(emptyCtx)那就间接返回 nil,false。
func parentCancelCtx(parent Context) (*cancelCtx, bool) {
done := parent.Done()
if done == closedchan || done == nil {
return nil, false
}
var closedchan = make(chan struct{})
func init() {
close(closedchan)
p, ok := parent.Value(&cancelCtxKey).(*cancelCtx)
if !ok {
return nil, false
}
接着判断是否parent是*cancelCtx类型,如果不是则返回nil,false,这里调用了parent.Value办法,并最终可能会落到value办法:
func value(c Context, key any) any {
for {
switch ctx := c.(type) {
case *valueCtx:
if key == ctx.key {
return ctx.val
}
c = ctx.Context
case *cancelCtx:
if key == &cancelCtxKey {
return c
}
c = ctx.Context
case *timerCtx:
if key == &cancelCtxKey {
return &ctx.cancelCtx
}
c = ctx.Context
case *emptyCtx:
return nil
default:
return c.Value(key)
}
}
}
- 如果是*valueCtx,并且key==ctx.key则返回,否则会将c赋值为 ctx.Context,持续下一个循环
- 如果是*cancelCtx并且key==&cancelCtxKey则阐明找到了,间接返回,否则c= ctx.上下文持续
- 如果是timerCtx,并且key== &cancelCtxKey则会返回外部的cancelCtx
- 如果是*emptyCtx 则间接返回nil,
- 默认即如果是用户自定义实现则调用对应的Value找寻
能够发现如果嵌套实现过多的话这个办法其实是一个递归调用。
如果是则要持续判断p.done与parent.Done()是否相等,如果没有则阐明:*cancelCtx曾经被包装在一个自定义实现中,提供了一个不同的包装,在这种状况下就返回nil,false:
pdone, _ := p.done.Load().(chan struct{})
if pdone != done {
return nil, false
}
return p, true
结构算是完结了,接下来看看如何勾销的:
- 查看err是否为nil
if err == nil {
panic("context: internal error: missing cancel error")
}
- 因为要对err、cancelCtx.done以及children进行操作,所以要加锁
- 如果c.err不为nil则阐明曾经勾销过了,间接返回。否则将c.err=err赋值,这里看到只有第一次调用才会赋值,屡次调用因为曾经有 != nil+锁的查看,所以会间接返回,不会反复赋值
c.mu.Lock()
if c.err != nil {
c.mu.Unlock()
return // already canceled
}
c.err = err
- 会尝试从c.done获取,如果为nil,则保留一个closedchan,否则就敞开d,这样当你context.Done()办法返回的channel才会返回。
d, _ := c.done.Load().(chan struct{})
if d == nil {
c.done.Store(closedchan)
} else {
close(d)
}
- 循环遍历c.children去敞开子Context,能够看到开释子context时会获取 子Context的锁,同时也会获取父Context的锁。所以才是线程平安的。完结后开释锁
d, _ := c.done.Load().(chan struct{})
if d == nil {
c.done.Store(closedchan)
} else {
close(d)
}
- 如果要将其从父Context删除为true,则将其从父上下文删除
if removeFromParent {
removeChild(c.Context, c)
}
removeChild也比较简单,当为*cancelCtx就将其从Children内删除,为了保障线程平安也是加锁的。
func removeChild(parent Context, child canceler) {
p, ok := parentCancelCtx(parent)
if !ok {
return
}
p.mu.Lock()
if p.children != nil {
delete(p.children, child)
}
p.mu.Unlock()
}
Done就是返回一个channel用于告知应用程序工作曾经终止:这一步是只读没有加锁,如果没有读取到则尝试加锁,再读一次,还没读到则创立一个chan,能够看到这是一个懒创立的过程。所以当用户被动调用CancelFunc时,其实根本就是将c.done内存储的chan close掉,这其中可能牵扯到父敞开,也要循环敞开子Context过程。
func (c *cancelCtx) Done() <-chan struct{} {
d := c.done.Load()
if d != nil {
return d.(chan struct{})
}
c.mu.Lock()
defer c.mu.Unlock()
d = c.done.Load()
if d == nil {
d = make(chan struct{})
c.done.Store(d)
}
return d.(chan struct{})
}
cancelCtx次要内容就这么多,接下里就是timerCtx了
计时器
回顾下timerCtx定义,就是内嵌了一个cancelCtx另外多了两个字段timer和deadline,这也是组合的体现。
type timerCtx struct {
cancelCtx
timer *time.Timer // Under cancelCtx.mu.
deadline time.Time
}
上面就看看两个构造函数,WithDeadline与WithTimeout,WithTimeout就是对WithDealine的一层简略封装。
查看不多说了, 第二个查看如果父context的截止工夫比传递进来的早的话,这个工夫就无用了,那么就进化成cancelCtx了。
func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) {
if parent == nil {
panic("cannot create context from nil parent")
}
if cur, ok := parent.Deadline(); ok && cur.Before(d) {
return WithCancel(parent)
}
func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
return WithDeadline(parent, time.Now().Add(timeout))
}
结构timerCtx并调用propagateCancel,这个曾经在下面介绍过了。
c := &timerCtx{
cancelCtx: newCancelCtx(parent),
deadline: d,
}
propagateCancel(parent, c)
接着会看,会先利用time.直到(d.分时。Now()) 来判断传入的 deadlineTime与以后工夫差值,如果在以后工夫之前的话阐明曾经该勾销了,所以会间接调用cancel函数进行勾销,并且将其从父Context中删除。否则就创立一个定时器,当工夫达到会调用勾销函数,这里是定时调用,也可能用户被动调用。
dur := time.Until(d)
if dur <= 0 {
c.cancel(true, DeadlineExceeded)
return c, func() { c.cancel(false, Canceled) }
}
c.mu.Lock()
defer c.mu.Unlock()
if c.err == nil {
c.timer = time.AfterFunc(dur, func() {
c.cancel(true, DeadlineExceeded)
})
}
return c, func() { c.cancel(true, Canceled) }
上面看看cancel实现吧,相比拟cancelCtx就比较简单了,先勾销 cancelCtx,也要加锁,将c.timer进行并赋值nil,这里也是第一次调用才会赋值nil,因为外层还有个c.timer !=nil的判断,所以屡次调用只有一次赋值。
func (c *timerCtx) cancel(removeFromParent bool, err error) {
c.cancelCtx.cancel(false, err)
if removeFromParent {
// Remove this timerCtx from its parent cancelCtx's children.
removeChild(c.cancelCtx.Context, c)
}
c.mu.Lock()
if c.timer != nil {
c.timer.Stop()
c.timer = nil
}
c.mu.Unlock()
}
相比拟于cancelCtx还笼罩实现了一个Deadline(),就是返回以后 Context的终止工夫。
func (c *timerCtx) Deadline() (deadline time.Time, ok bool) {
return c.deadline, true
}
上面就到了最初一个内置的valueCtx了。
值
构造器就更加加单,就多了key,val
type valueCtx struct {
Context
key, val any
}
也就有个Value method不同,能够看到底层应用的就是咱们下面介绍的value函数,反复复用
func (c *valueCtx) Value(key any) any {
if c.key == key {
return c.val
}
return value(c.Context, key)
}
几个次要的解说完了,能够看到不到600行代码,就实现了这么多功能,其中蕴含了组合、封装、构造体嵌套接口等许多理念,值得好好推敲。上面咱们再看看其中有些有意思的中央。咱们个别打印字符串都是应用 fmt 包,那么不应用fmt包该如何打印呢?context包里就有相应实现,也很简略,就是 switch case来判断v类型并返回,它这么做的起因也有说:
“因为咱们不心愿上下文依赖于unicode表”,这句话我还没了解,有晓得的小伙伴能够在底下评论,或者等我有工夫看看fmt包实现。
func stringify(v any) string {
switch s := v.(type) {
case stringer:
return s.String()
case string:
return s
}
return "<not Stringer>"
}
func (c *valueCtx) String() string {
return contextName(c.Context) + ".WithValue(type " +
reflectlite.TypeOf(c.key).String() +
", val " + stringify(c.val) + ")"
}
应用Context的几个准则
间接在函数参数传递,不要在struct传递,要明确传递,并且作为第一个参数,因为这样能够由调用方来传递不同的上下文在不同的办法上,如果你在 struct内应用context则一个实例是专用一个context也就导致了协程不平安,这也是为何net包Request要拷贝一个新的Request WithRequest(context go 1.7 才被引入),net包牵扯过多,要做到兼容才嵌入到 struct内。
不要应用nil而当你不晓得应用什么时则应用TODO,如果你用了nil则会 panic。防止到处判断是否为nil。
WithValue不应该传递业务信息,只应该传递相似request-id之类的申请信息。
无论用哪个类型的Context,在构建后,肯定要加上:defer cancel(),因为这个函数是能够屡次调用的,然而如果没有调用则可能导致Context没有被勾销继而其关联的上下文资源也得不到开释。
在应用WithValue时,包应该将键定义为未导出的类型以防止产生碰撞,这里贴个官网的例子:
// package user 这里为了演示间接在 main 包定义
// User 是存储在 Context 值
type User struct {
Name string
Age int
}
// key 是非导出的,能够避免碰撞
type key int
// userKey 是存储 User 类型的键值,也是非导出的。
var userKey key
// NewContext 创立一个新的 Context,携带 *User
func NewContext(ctx context.Context, u *User) context.Context {
return context.WithValue(ctx, userKey, u)
}
// FromContext 返回存储在 ctx 中的 *User
func FromContext(ctx context.Context) (*User, bool) {
u, ok := ctx.Value(userKey).(*User)
return u, ok
}
那怎么可能避免碰撞呢?能够做个示例:看最初输入,咱们在第一行就用 userKey的值0,存储了一个值“a”。
而后再利用NewContext存储了&User,底层理论用的是 context.WithValue(ctx,userKey,u)
读取时用的是FromContext,两次存储即便底层的key值都为0, 然而互不影响,这是为什么呢?
还记得WithValue怎么实现的么?你每调用一次都会包一层,并且一层一层解析,而且它会比拟c.key==key,这里记住go的==比拟是比拟值和类型的,二者都相等才为true,而咱们应用type key int所以userKey与0底层值尽管一样,然而类型曾经不一样了(这里就是main.userKey与0),所以内部无论定义何种类型都无奈影响包内的类型。这也是容易令人蛊惑的中央
package main
import (
"context"
"fmt"
)
func main() {
ctx := context.WithValue(context.Background(), , "a")
ctx = NewContext(ctx, &User{})
v, _ := FromContext(ctx)
fmt.Println(ctx.Value(0), v) // Output: a, &{ 0}
}
如果你是腾讯技术内容创作者,腾讯云开发者社区诚邀您退出【腾讯云原创分享打算】,支付礼品,助力职级降职。
发表回复