Golang 之context用法

1. context

Golang 中的context 是Go語言在 golang1.7 發佈時新增的標準包

目的是增強Golang開發中併發控制技術

簡單來講當一個服務啓動時,可能由此服務派生出多個多層級的 goroutine , 但是本質上來講每個層級的 goroutine 都是平行調度使用,不存在goroutine ‘父子’ 關係 , 當其中一個 goroutine 執行的任務被取消了或者處理超時了,那麼其他被啓動起來的Goroutine 都應該迅速退出,另外多個多層的Goroutine 想傳遞請求域的數據該如何處理?

如果單個請求的Goroutine 結構比較簡單,或者處理起來也不麻煩,但是如果啓動的Goroutine 是多個並且結構層次很深那麼光是保障每個Goroutine 正常退出也不很容易了

爲此Go1.7以來提供了 context 來解決類似的問題 , context 可以跟蹤 Goroutine 的調用, 在調用內部維護一個調用樹,通過這個調用樹可以在傳遞超時或者退出通知,還能在調用樹中傳遞元數據

context的中文翻譯是上下文 ,我們可以理解爲 context 管理了一組呈現樹狀結構的 Goroutine ,讓每個Goroutine 都擁有相同的上下文,並且可以在這個上下文中傳遞數據

2. context.go

2.0 結構圖

在這裏插入圖片描述

我們看一 context.go 的源文件瞭解一下context 的構成 該文件通常位於

$GOROOT/src/context/context.go
2.1 Context interface

context 實際上只是定義的4個方法的接口,凡是實現了該接口的都稱爲一種 context

// A Context carries a deadline, a cancelation signal, and other values across
// API boundaries.
//
// Context's methods may be called by multiple goroutines simultaneously.
type Context interface {
    // 標識deadline是否已經設置了,沒有設置時,ok的值是false,並返回初始的time.Time
	Deadline() (deadline time.Time, ok bool)
    // 返回一個channel, 當返回關閉的channel時可以執行一些操作
	Done() <-chan struct{}
    // 描述context關閉的原因,通常在Done()收到關閉通知之後才能知道原因
	Err() error
    // 獲取上游Goroutine 傳遞給下游Goroutine的某些數據
	Value(key interface{}) interface{}
}
2.2 emptyCtx
// 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 interface{}) interface{} {
	return nil
}

func (e *emptyCtx) String() string {
	switch e {
	case background:
		return "context.Background"
	case todo:
		return "context.TODO"
	}
	return "unknown empty Context"
}

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
}

我們看到 emptyCtx 實現了Context 接口,但是其實現的方法都是空nil 那麼我們就可以知道其實emptyCtx 是不具備任何實際功能的,那麼它存在的目的是什麼呢?

emptyCtx 存在的意義是作爲 Context 對象樹根節點 root節點 , 在context.go 包中提供 Background()TODO() 兩個函數 ,這兩個函數都是返回的都是 emptyCtx 實例 ,通常我們使用他們來構建Context的根節點 , 有了root根節點之後就可同事 context.go 包中提供的其他的包裝函數創建具有意義的context 實例 ,並且沒有context 實例的創建都是以上一個 context 實例對象作爲參數的(所以必須有一個根節點) ,最終形成一個樹狀的管理結構

2.3 cancelCtx

定義了cancelCtx 類型的結構體

其中字段children 記錄派生的child,當該類型的context(上下文) 被執行cancel是會將所有派生的child都執行cancel

對外暴露了 Err() Done() String() 方法

// A cancelCtx can be canceled. When canceled, it also cancels any children
// that implement canceler.
type cancelCtx struct {
	Context

	mu       sync.Mutex            // protects following fields
	done     chan struct{}         // created lazily, closed by first cancel call
	children map[canceler]struct{} // set to nil by the first cancel call
	err      error                 // set to non-nil by the first cancel call
}

func (c *cancelCtx) Done() <-chan struct{} {
	c.mu.Lock()
	if c.done == nil {
		c.done = make(chan struct{})
	}
	d := c.done
	c.mu.Unlock()
	return d
}

func (c *cancelCtx) Err() error {
	c.mu.Lock()
	err := c.err
	c.mu.Unlock()
	return err
}

func (c *cancelCtx) String() string {
	return fmt.Sprintf("%v.WithCancel", c.Context)
}

// 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
	if c.done == nil {
		c.done = closedchan
	} else {
		close(c.done)
	}
	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)
	}
}
2.4 valueCtx

通過 valueCtx 結構知道僅是在Context 的基礎上增加了元素 keyvalue

通常用於在層級協程之間傳遞數據

// 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 interface{}
}

func (c *valueCtx) String() string {
	return fmt.Sprintf("%v.WithValue(%#v, %#v)", c.Context, c.key, c.val)
}

func (c *valueCtx) Value(key interface{}) interface{} {
	if c.key == key {
		return c.val
	}
	return c.Context.Value(key)
}
2.5 timerCtx

cancelCtx 基礎上增加了字段 timerdeadline

timer 觸發自動cancel的定時器

deadline 標識最後執行cancel的時間

type timerCtx struct {
	cancelCtx
	timer *time.Timer // Under cancelCtx.mu.

	deadline time.Time
}

func (c *timerCtx) Deadline() (deadline time.Time, ok bool) {
	return c.deadline, true
}

func (c *timerCtx) String() string {
	return fmt.Sprintf("%v.WithDeadline(%s [%s])", c.cancelCtx.Context, c.deadline, time.Until(c.deadline))
}

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()
}

3. 使用示例

context.go 包中提供了4個以 With 開頭的函數, 這幾個函數的主要功能是實例化不同類型的context

通過 Background()TODO() 創建最 emptyCtx 實例 ,通常是作爲根節點

通過 WithCancel() 創建 cancelCtx 實例

通過 WithValue() 創建 valueCtx 實例

通過 WithDeadlineWithTimeout 創建 timerCtx 實例

3.1 WithCancel

源碼如下

// newCancelCtx returns an initialized cancelCtx.
func newCancelCtx(parent Context) cancelCtx {
	return cancelCtx{Context: parent}
}
func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
    // 創建cancelCtx實例
    c := newCancelCtx(parent)
    // 添加到父節點的children中
	propagateCancel(parent, &c)
    // 返回實例和方法
	return &c, func() { c.cancel(true, Canceled) }
}

**使用示例 : **

package main

import (
	"context"
	"fmt"
	"time"
)

func MyOperate1(ctx context.Context) {
	for {
		select {
		default:
			fmt.Println("MyOperate1", time.Now().Format("2006-01-02 15:04:05"))
			time.Sleep(2 * time.Second)
		case <-ctx.Done():
			fmt.Println("MyOperate1 Done")
			return
		}
	}
}
func MyOperate2(ctx context.Context) {
	fmt.Println("Myoperate2")
}
func MyDo2(ctx context.Context) {
	go MyOperate1(ctx)
	go MyOperate2(ctx)
	for {
		select {
		default:
			fmt.Println("MyDo2 : ", time.Now().Format("2006-01-02 15:04:05"))
			time.Sleep(2 * time.Second)
		case <-ctx.Done():
			fmt.Println("MyDo2 Done")
			return
		}
	}

}
func MyDo1(ctx context.Context) {
	go MyDo2(ctx)
	for {
		select {
		case <-ctx.Done():
			fmt.Println("MyDo1 Done")
			// 打印 ctx 關閉原因
			fmt.Println(ctx.Err())
			return
		default:
			fmt.Println("MyDo1 : ", time.Now().Format("2006-01-02 15:04:05"))
			time.Sleep(2 * time.Second)
		}
	}
}
func main() {
	// 創建 cancelCtx 實例
	// 傳入context.Background() 作爲根節點
	ctx, cancel := context.WithCancel(context.Background())
	// 向協程中傳遞ctx
	go MyDo1(ctx)
	time.Sleep(5 * time.Second)
	fmt.Println("stop all goroutines")
	// 執行cancel操作
	cancel()
	time.Sleep(2 * time.Second)
}

3.2 WithDeadline

設置了deadlinecontext

這個deadline(最終期限) 表示context在指定的時刻結束

源碼如下

func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) {
	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) }
}

使用示例

package main

import (
	"context"
	"fmt"
	"time"
)

func dl2(ctx context.Context) {
	n := 1
	for {
		select {
		case <-ctx.Done():
			fmt.Println(ctx.Err())
			return
		default:
			fmt.Println("dl2 : ", n)
			n++
			time.Sleep(time.Second)
		}
	}
}

func dl1(ctx context.Context) {
	n := 1
	for {
		select {
		case <-ctx.Done():
			fmt.Println(ctx.Err())
			return
		default:
			fmt.Println("dl1 : ", n)
			n++
			time.Sleep(2 * time.Second)
		}
	}
}
func main() {
	// 設置deadline爲當前時間之後的5秒那個時刻
	d := time.Now().Add(5 * time.Second)
	ctx, cancel := context.WithDeadline(context.Background(), d)
	defer cancel()
	go dl1(ctx)
	go dl2(ctx)
	for{
		select {
			case <-ctx.Done():
				fmt.Println("over",ctx.Err())
				return
		}
	}
}

3.3 WithTimeout

實際就是調用了WithDeadline()

源碼如下

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

使用示例 :

package main

import (
   "context"
   "fmt"
   "time"
)

func to1(ctx context.Context) {
   n := 1
   for {
   	select {
   	case <-ctx.Done():
   		fmt.Println("to1 is over")
   		return
   	default:
   		fmt.Println("to1 : ", n)
   		n++
   		time.Sleep(time.Second)
   	}
   }
}
func main() {
   // 設置爲6秒後context結束
   ctx, cancel := context.WithTimeout(context.Background(), 6*time.Second)
   defer cancel()
   go to1(ctx)
   n := 1
   for {
   	select {
   	case <-time.Tick(2 * time.Second):
   		if n == 9 {
   			return
   		}
   		fmt.Println("number :", n)
   		n++
   	}
   }
}

3.4 WithValue

僅是在Context 基礎上添加了 key : value 的鍵值對

context 形成的樹狀結構,後面的節點可以訪問前面節點傳導的數據

源碼如下 :

func WithValue(parent Context, key, val interface{}) Context {
   if key == nil {
   	panic("nil key")
   }
   if !reflect.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 interface{}
}

使用示例 :

package main

import (
   "context"
   "fmt"
   "time"
)

func v3(ctx context.Context) {
   for {
   	select {
   	case <-ctx.Done():
   		fmt.Println("v3 Done : ", ctx.Err())
   		return
   	default:
   		fmt.Println(ctx.Value("key"))
   		time.Sleep(3 * time.Second)
   	}
   }
}
func v2(ctx context.Context) {
   fmt.Println(ctx.Value("key"))
   fmt.Println(ctx.Value("v1"))
   // 相同鍵,值覆蓋
   ctx = context.WithValue(ctx, "key", "modify from v2")
   go v3(ctx)
}
func v1(ctx context.Context) {
   if v := ctx.Value("key"); v != nil {
   	fmt.Println("key = ", v)
   }
   ctx = context.WithValue(ctx, "v1", "value of v1 func")
   go v2(ctx)
   for {
   	select {
   	default:
   		fmt.Println("print v1")
   		time.Sleep(time.Second * 2)
   	case <-ctx.Done():
   		fmt.Println("v1 Done : ", ctx.Err())
   		return
   	}
   }
}
func main() {
   ctx, cancel := context.WithCancel(context.Background())
   // 向context中傳遞值
   ctx = context.WithValue(ctx, "key", "main")
   go v1(ctx)
   time.Sleep(10 * time.Second)
   cancel()
   time.Sleep(3 * time.Second)
}

參考資料

- [1] context

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章