欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Go中Context使用源碼解析

 更新時間:2023年04月16日 10:49:11   作者:程序員wall  
這篇文章主要為大家介紹了Go中Context使用源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

前言

本篇內(nèi)容的主題是Go中Context,想必已學(xué)習(xí)Go語言的大家在熟悉不過了。工作中我們也常會用到,但有時很少去注意它。

本打算將相關(guān)知識點(diǎn)歸總一下,發(fā)現(xiàn)其源碼不多,就打算對其源碼進(jìn)行分析一下。

context包是在go1.17是引入到標(biāo)準(zhǔn)庫中,且標(biāo)準(zhǔn)庫中大部分接口都將context.Context作為第一個參數(shù)。

context中文譯為“上下文”,實(shí)際代表的是goroutine的上下文。且多用于超時控制和多個goroutine間的數(shù)據(jù)傳遞。

本篇文章將帶領(lǐng)大家深入了解其內(nèi)部的工作原理。

1、Context定義

Context 接口定義如下

type Context interface {
  // Deadline returns the time when this Context will be canceled, if any.
	Deadline() (deadline time.Time, ok bool)
  // Done returns a channel that is closed when this Context is canceled
  // or times out.
	Done() <-chan struct{}
  // Err indicates why this context was canceled, after the Done channel
  // is closed.
	Err() error
  // Value returns the value associated with key or nil if none.
	Value(key any) any
}

Deadline(): 返回的第一個值是 截止時間,到了這個時間點(diǎn),Context 會自動觸發(fā) Cancel 動作。返回的第二個值是 一個布爾值,true 表示設(shè)置了截止時間,false 表示沒有設(shè)置截止時間,如果沒有設(shè)置截止時間,就要手動調(diào)用 cancel 函數(shù)取消 Context。

Done(): 返回一個只讀的通道(只有在被cancel后才會返回),類型為 struct{}。當(dāng)這個通道可讀時,意味著parent context已經(jīng)發(fā)起了取消請求,根據(jù)這個信號,開發(fā)者就可以做一些清理動作,退出goroutine。這里就簡稱信號通道吧!

Err():返回Context 被取消的原因

Value: 從Context中獲取與Key關(guān)聯(lián)的值,如果沒有就返回nil

2、Context的派生

2.1、創(chuàng)建Context對象

context包提供了四種方法來創(chuàng)建context對象,具體方法如下:

func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {}
func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) {}
func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {}
func WithValue(parent Context, key, val any) Context {}

由以上方法可知:新的context對象都是基于父context對象衍生的.

  • WithCancel:創(chuàng)建可以取消的Context
  • WithDeadline: 創(chuàng)建帶有截止時間的Context
  • WithTimeout:創(chuàng)建帶有超時時間的Context,底層調(diào)用的是WithDeadline方法
  • WithValue:創(chuàng)建可以攜帶KV型數(shù)據(jù)的Context

簡單的樹狀關(guān)系如下(實(shí)際可衍生很多中):

2.2、parent Context

context包默認(rèn)提供了兩個根context 對象backgroundtodo;看實(shí)現(xiàn)兩者都是由emptyCtx創(chuàng)建的,兩者的區(qū)別主要在語義上,

  • context.Background 是上下文的默認(rèn)值,所有其他的上下文都應(yīng)該從它衍生出來;
  • context.TODO 應(yīng)該僅在不確定應(yīng)該使用哪種上下文時使用
var (
	background = new(emptyCtx)
	todo       = new(emptyCtx)
)
// Background 創(chuàng)建background context
func Background() Context {
	return background
}
// TODO 創(chuàng)建todo context
func TODO() Context {
	return todo
}

3、context 接口四種實(shí)現(xiàn)

具體結(jié)構(gòu)如下,我們大致看下相關(guān)結(jié)構(gòu)體中包含的字段,具體字段的含義及作用將在下面分析中會提及。

  • emptyCtx
type emptyCtx int // 空context
  • cancelCtx
type cancelCtx struct {
	Context // 父context
	mu       sync.Mutex            // protects following fields
	done     atomic.Value          // of chan struct{}, created lazily, closed by first cancel call
	children map[canceler]struct{} // set to nil by the first cancel call
	err      error                 // cancel的原因
}
  • timerCtx
type timerCtx struct {
	cancelCtx //父context
	timer *time.Timer // 定時器
	deadline time.Time // 截止時間
}
  • valueCtx
type valueCtx struct {
	Context // 父context
  key, val any // kv鍵值對
}

4、 emptyCtx 源碼分析

emptyCtx實(shí)現(xiàn)非常簡單,具體代碼如下,我們簡單看看就可以了

// An emptyCtx is never canceled, has no values, and has no deadline. It is not
// struct{}, since vars of this type must have distinct addresses.
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"
}

5、 cancelCtx 源碼分析

cancelCtx 的實(shí)現(xiàn)相對復(fù)雜點(diǎn),比如下面要介紹的timeCtx 底層也依賴它,所以弄懂cancelCtx的工作原理就能很好的理解context.

cancelCtx 不僅實(shí)現(xiàn)了Context接口也實(shí)現(xiàn)了canceler接口

5.1、對象創(chuàng)建withCancel()

func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
	if parent == nil { // 參數(shù)校驗(yàn)
		panic("cannot create context from nil parent")
	}
  // cancelCtx 初始化
	c := newCancelCtx(parent)
	propagateCancel(parent, &c) // cancelCtx 父子關(guān)系維護(hù)及傳播取消信號
	return &c, func() { c.cancel(true, Canceled) } // 返回cancelCtx對象及cannel方法
}
// newCancelCtx returns an initialized cancelCtx.
func newCancelCtx(parent Context) cancelCtx {
	return cancelCtx{Context: parent}
}

用戶調(diào)用WithCancel方法,傳入一個父 Context(這通常是一個 background,作為根節(jié)點(diǎn)),返回新建的 context,并通過閉包的形式返回了一個 cancel 方法。如果想要取消context時需手動調(diào)用cancel方法。

5.1.1、newCancelCtx

cancelCtx對象初始化, 其結(jié)構(gòu)如下:

type cancelCtx struct {
  // 父 context
	Context //  parent context
	// 鎖 并發(fā)場景下保護(hù)cancelCtx結(jié)構(gòu)中字段屬性的設(shè)置
	mu       sync.Mutex            // protects following fields 
  // done里存儲的是信號通道,其創(chuàng)建方式采用的是懶加載的方式
	done     atomic.Value          // of chan struct{}, created lazily, closed by first cancel call 
  // 記錄與父子cancelCtx對象,
	children map[canceler]struct{} // set to nil by the first cancel call
  // 記錄ctx被取消的原因
	err      error                 // set to non-nil by the first cancel call
}

5.1.2、propagateCancel

propagateCancel

// propagateCancel arranges for child to be canceled when parent is.
func propagateCancel(parent Context, child canceler) {
	done := parent.Done() // 獲取parent ctx的信號通道 done
	if done == nil {  // nil 代表 parent ctx 不是canelctx 類型,不會被取消,直接返回
		return // parent is never canceled
	}
	select { // parent ctx 是cancelCtx類型,判斷其是否被取消
	case <-done:
		// parent is already canceled
		child.cancel(false, parent.Err())
		return
	default:
	}
  //parentCancelCtx往樹的根節(jié)點(diǎn)方向找到最近的context是cancelCtx類型的
	if p, ok := parentCancelCtx(parent); ok { // 查詢到
		p.mu.Lock() // 加鎖
		if p.err != nil { // 祖父 ctx 已經(jīng)被取消了,則 子cancelCtx 也需要調(diào)用cancel 方法來取消
			// parent has already been canceled
			child.cancel(false, p.err)
		} else { // 使用map結(jié)構(gòu)來維護(hù) 將child加入到祖父context中
			if p.children == nil {
				p.children = make(map[canceler]struct{})
			}
			p.children[child] = struct{}{}
		}
		p.mu.Unlock()// 解鎖
	} else { // 開啟協(xié)程監(jiān)聽 parent Ctx的取消信號 來通知child ctx 取消
		atomic.AddInt32(&goroutines, +1)
		go func() {
			select {
			case <-parent.Done():
				child.cancel(false, parent.Err())
			case <-child.Done():
			}
		}()
	}
}
// parentCancelCtx returns the underlying *cancelCtx for parent.
// It does this by looking up parent.Value(&cancelCtxKey) to find
// the innermost enclosing *cancelCtx and then checking whether
// parent.Done() matches that *cancelCtx. (If not, the *cancelCtx
// has been wrapped in a custom implementation providing a
// different done channel, in which case we should not bypass it.)
// parentCancelCtx往樹的根節(jié)點(diǎn)方向找到最近的context是cancelCtx類型的
func parentCancelCtx(parent Context) (*cancelCtx, bool) {
	done := parent.Done()
  // closedchan 代表此時cancelCtx 已取消, nil 代表 ctx不是cancelCtx 類型的且不會被取消
	if done == closedchan || done == nil { 
		return nil, false
	}
  // 向上遍歷查詢canelCtx 類型的ctx
	p, ok := parent.Value(&cancelCtxKey).(*cancelCtx)
	if !ok { // 沒有
		return nil, false
	}
  // 存在判斷信號通道是不是相同
	pdone, _ := p.done.Load().(chan struct{})
	if pdone != done {
		return nil, false
	}
	return p, true
}

5.2 canceler

cancelCtx也實(shí)現(xiàn)了canceler接口,實(shí)現(xiàn)可以 取消上下文的功能。

canceler接口定義如下:

// A canceler is a context type that can be canceled directly. The
// implementations are *cancelCtx and *timerCtx.
type canceler interface {
	cancel(removeFromParent bool, err error) // 取消
	Done() <-chan struct{} // 只讀通道,簡稱取消信號通道
}

cancelCtx 接口實(shí)現(xiàn)如下:

整體邏輯不復(fù)雜,邏輯簡化如下:

  • 當(dāng)前 cancelCtx 取消 且 與之其關(guān)聯(lián)的子 cancelCtx 也取消
  • 根據(jù)removeFromParent標(biāo)識來判斷是否將子 cancelCtx 移除

注意

由于信號通道的初始化采用的懶加載方式,所以有未初始化的情況;

已初始化的:調(diào)用close 函數(shù)關(guān)閉channel

未初始化的:用 closedchan 初始化,其closedchan是已經(jīng)關(guān)閉的channel。

// cancel closes c.done, cancels each of c's children, and, if
// removeFromParent is true, removes c from its parent's children.
func (c *cancelCtx) cancel(removeFromParent bool, err error) {
	if err == nil {
		panic("context: internal error: missing cancel error")
	}
	c.mu.Lock()
	if c.err != nil {
		c.mu.Unlock()
		return // already canceled
	}
	c.err = err
	d, _ := c.done.Load().(chan struct{})
	if d == nil {
		c.done.Store(closedchan)
	} else {
		close(d)
	}
	for child := range c.children {
		// NOTE: acquiring the child's lock while holding parent's lock.
		child.cancel(false, err)
	}
	c.children = nil
	c.mu.Unlock()
	if removeFromParent {
		removeChild(c.Context, c)
	}
}
// removeChild removes a context from its parent.
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()
}

closedchan

可重用的關(guān)閉通道,該channel通道默認(rèn)已關(guān)閉

// closedchan is a reusable closed channel.
var closedchan = make(chan struct{})
func init() {
	close(closedchan) // 調(diào)用close 方法關(guān)閉
}

6、timerCtx 源碼分析

cancelCtx源碼已經(jīng)分析完畢,那timerCtx理解起來就很容易。

關(guān)注點(diǎn)timerCtx是如何取消上下文的,以及取消上下文的方式

6.1、對象創(chuàng)建 WithDeadline和WithTimeout

WithTimeout 底層調(diào)用是WithDeadline 方法 ,截止時間是 now+timeout;

func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
	return WithDeadline(parent, time.Now().Add(timeout))
}

WithDeadline 整體邏輯并不復(fù)雜,從源碼中可分析出timerCtx取消上下文 采用兩種方式 自動手動;其中自動方式采用定時器去處理,到達(dá)觸發(fā)時刻,自動調(diào)用cancel方法。

deadline: 截止時間

timer *time.Timer : 定時器

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) {
		// The current deadline is already sooner than the new one.
		return WithCancel(parent)
	}
	c := &timerCtx{
		cancelCtx: newCancelCtx(parent),
		deadline:  d,
	}
	propagateCancel(parent, c)
	dur := time.Until(d)
	if dur <= 0 {
		c.cancel(true, DeadlineExceeded) // deadline has already passed
		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) }
}
// A timerCtx carries a timer and a deadline. It embeds a cancelCtx to
// implement Done and Err. It implements cancel by stopping its timer then
// delegating to cancelCtx.cancel.
type timerCtx struct {
	cancelCtx
	timer *time.Timer // Under cancelCtx.mu.
	deadline time.Time
}

6.2 timerCtx的cancel

  • 調(diào)用cancelCtx的cancel 方法
  • 根據(jù)removeFromParent標(biāo)識,為true 調(diào)用removeChild 方法 從它的父cancelCtx的children中移除
  • 關(guān)閉定時器 ,防止內(nèi)存泄漏(著重點(diǎn))
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()
}

7、valueCtx 源碼分析

7.1、對象創(chuàng)建WithValue

valueCtx 結(jié)構(gòu)體中有keyval兩個字段,WithValue 方法也是將數(shù)據(jù)存放在該字段上

func WithValue(parent Context, key, val any) Context {
	if parent == nil {
		panic("cannot create context from nil parent")
	}
	if key == nil {
		panic("nil key")
	}
	if !reflectlite.TypeOf(key).Comparable() {
		panic("key is not comparable")
	}
	return &valueCtx{parent, key, val}
}
// A valueCtx carries a key-value pair. It implements Value for that key and
// delegates all other calls to the embedded Context.
type valueCtx struct {
	Context
	key, val any
}

7.2、獲取value值

func (c *valueCtx) Value(key any) any {
	if c.key == key { // 判斷當(dāng)前valuectx對象中的key是否匹配
		return c.val
	}
	return value(c.Context, key)
}
// value() 向根部方向遍歷,直到找到與key對應(yīng)的值
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 == &amp;cancelCtxKey { // 獲取cancelCtx對象
				return c
			}
			c = ctx.Context
		case *timerCtx:
			if key == &amp;cancelCtxKey {
				return &amp;ctx.cancelCtx
			}
			c = ctx.Context
		case *emptyCtx:
			return nil
		default:
			return c.Value(key)
		}
	}
}

總結(jié):從Context 中獲取對應(yīng)的值需要通過遍歷的方式來獲取,這里告誡我們嵌套太多的context反而對性能會有影響

8、規(guī)范&注意事項(xiàng)

  • 不要把context存在一個結(jié)構(gòu)體當(dāng)中,顯式地傳入函數(shù)。context變量需要作為第一個參數(shù)使用,一般命名為ctx;
  • 即使方法允許,也不要傳入一個nil的Context,如果你不確定你要用什么Context的時候傳一個context.TODO
  • 使用context的Value相關(guān)方法只應(yīng)該用于在程序和接口中傳遞的和請求相關(guān)的元數(shù)據(jù),不要用它來傳遞一些可選的參數(shù)
  • 同樣的Context可以用來傳遞到不同的goroutine中,Context在多個goroutine中是安全的

以上就是Go中Context使用源碼解析的詳細(xì)內(nèi)容,更多關(guān)于Go Context源碼解析的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • golang http 連接超時和傳輸超時的例子

    golang http 連接超時和傳輸超時的例子

    今天小編就為大家分享一篇golang http 連接超時和傳輸超時的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Golang定制化zap日志庫使用過程分析

    Golang定制化zap日志庫使用過程分析

    Zap是我個人比較喜歡的日志庫,是uber開源的,有較好的性能,在項(xiàng)目開發(fā)中,經(jīng)常需要把程序運(yùn)行過程中各種信息記錄下來,有了詳細(xì)的日志有助于問題排查和功能優(yōu)化,但如何選擇和使用性能好功能強(qiáng)大的日志庫,這個就需要我們從多角度考慮
    2023-03-03
  • B站新一代 golang規(guī)則引擎gengine基礎(chǔ)語法

    B站新一代 golang規(guī)則引擎gengine基礎(chǔ)語法

    這篇文章主要為大家介紹了B站新一代 golang規(guī)則引擎gengine基礎(chǔ)語法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • golang讀取yaml文件的示例代碼

    golang讀取yaml文件的示例代碼

    本文主要介紹了golang讀取yaml文件的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-09-09
  • GoLang OS包以及File類型詳細(xì)講解

    GoLang OS包以及File類型詳細(xì)講解

    go中對文件和目錄的操作主要集中在os包中,下面對go中用到的對文件和目錄的操作,做一個總結(jié)筆記。在go中的文件和目錄涉及到兩種類型,一個是type File struct,另一個是type Fileinfo interface
    2023-03-03
  • 詳解Golang利用反射reflect動態(tài)調(diào)用方法

    詳解Golang利用反射reflect動態(tài)調(diào)用方法

    這篇文章主要介紹了詳解Golang利用反射reflect動態(tài)調(diào)用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • golang如何實(shí)現(xiàn)三元運(yùn)算符功能

    golang如何實(shí)現(xiàn)三元運(yùn)算符功能

    這篇文章主要介紹了在其他一些編程語言中,如?C?語言,三元運(yùn)算符是一種可以用一行代碼實(shí)現(xiàn)條件選擇的簡便方法,那么在Go語言中如何實(shí)現(xiàn)類似功能呢,下面就跟隨小編一起學(xué)習(xí)一下吧
    2024-02-02
  • 解決golang 關(guān)于全局變量的坑

    解決golang 關(guān)于全局變量的坑

    這篇文章主要介紹了解決golang 關(guān)于全局變量的坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05
  • 一文帶你掌握Golang中的類型斷言

    一文帶你掌握Golang中的類型斷言

    類型斷言是?Golang?中的一個非常重要的特性,使用類型斷言可以判斷一個接口的實(shí)際類型是否是預(yù)期的類型,以便進(jìn)行對應(yīng)的處理,下面就跟隨小編一起深入了解一下Golang中的類型斷言吧
    2024-01-01
  • 詳解如何熱重啟golang服務(wù)器

    詳解如何熱重啟golang服務(wù)器

    這篇文章主要介紹了詳解如何熱重啟golang服務(wù)器,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08

最新評論