1. context
Golang 中的context 是Go語言在
golang1.7
發(fā)布時(shí)新增的標(biāo)準(zhǔn)包目的是增強(qiáng)Golang開發(fā)中并發(fā)控制技術(shù)
簡單來講當(dāng)一個(gè)服務(wù)啟動(dòng)時(shí),可能由此服務(wù)派生出多個(gè)多層級(jí)的
goroutine
, 但是本質(zhì)上來講每個(gè)層級(jí)的goroutine
都是平行調(diào)度使用,不存在goroutine
'父子' 關(guān)系 , 當(dāng)其中一個(gè)goroutine
執(zhí)行的任務(wù)被取消了或者處理超時(shí)了,那么其他被啟動(dòng)起來的Goroutine
都應(yīng)該迅速退出,另外多個(gè)多層的Goroutine
想傳遞請求域的數(shù)據(jù)該如何處理?如果單個(gè)請求的
Goroutine
結(jié)構(gòu)比較簡單,或者處理起來也不麻煩,但是如果啟動(dòng)的Goroutine
是多個(gè)并且結(jié)構(gòu)層次很深那么光是保障每個(gè)Goroutine
正常退出也不很容易了
為此Go1.7以來提供了 context
來解決類似的問題 , context
可以跟蹤 Goroutine
的調(diào)用, 在調(diào)用內(nèi)部維護(hù)一個(gè)調(diào)用樹,通過這個(gè)調(diào)用樹可以在傳遞超時(shí)或者退出通知,還能在調(diào)用樹中傳遞元數(shù)據(jù)
context的中文翻譯是上下文
,我們可以理解為 context 管理了一組呈現(xiàn)樹狀結(jié)構(gòu)的 Goroutine
,讓每個(gè)Goroutine
都擁有相同的上下文,并且可以在這個(gè)上下文中傳遞數(shù)據(jù)
2. context.go
2.0 結(jié)構(gòu)圖
我們看一
context.go
的源文件了解一下context
的構(gòu)成 該文件通常位于
$GOROOT/src/context/context.go
2.1 Context interface
context
實(shí)際上只是定義的4個(gè)方法的接口,凡是實(shí)現(xiàn)了該接口的都稱為一種 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 {
// 標(biāo)識(shí)deadline是否已經(jīng)設(shè)置了,沒有設(shè)置時(shí),ok的值是false,并返回初始的time.Time
Deadline() (deadline time.Time, ok bool)
// 返回一個(gè)channel, 當(dāng)返回關(guān)閉的channel時(shí)可以執(zhí)行一些操作
Done() <-chan struct{}
// 描述context關(guān)閉的原因,通常在Done()收到關(guān)閉通知之后才能知道原因
Err() error
// 獲取上游Goroutine 傳遞給下游Goroutine的某些數(shù)據(jù)
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
實(shí)現(xiàn)了Context
接口,但是其實(shí)現(xiàn)的方法都是空nil
那么我們就可以知道其實(shí)emptyCtx
是不具備任何實(shí)際功能的,那么它存在的目的是什么呢?
emptyCtx
存在的意義是作為Context
對象樹根節(jié)點(diǎn)root節(jié)點(diǎn)
, 在context.go
包中提供Background()
和TODO()
兩個(gè)函數(shù) ,這兩個(gè)函數(shù)都是返回的都是emptyCtx
實(shí)例 ,通常我們使用他們來構(gòu)建Context
的根節(jié)點(diǎn) , 有了root
根節(jié)點(diǎn)之后就可同事context.go
包中提供的其他的包裝函數(shù)創(chuàng)建具有意義的context
實(shí)例 ,并且沒有context
實(shí)例的創(chuàng)建都是以上一個(gè)context
實(shí)例對象作為參數(shù)的(所以必須有一個(gè)根節(jié)點(diǎn)) ,最終形成一個(gè)樹狀的管理結(jié)構(gòu)
2.3 cancelCtx
定義了
cancelCtx
類型的結(jié)構(gòu)體其中字段
children
記錄派生的child,當(dāng)該類型的context(上下文) 被執(zhí)行cancel是會(huì)將所有派生的child都執(zhí)行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
結(jié)構(gòu)知道僅是在Context
的基礎(chǔ)上增加了元素key
和value
通常用于在層級(jí)協(xié)程之間傳遞數(shù)據(jù)
// 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
基礎(chǔ)上增加了字段timer
和deadline
timer
觸發(fā)自動(dòng)cancel的定時(shí)器
deadline
標(biāo)識(shí)最后執(zhí)行cancel的時(shí)間
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個(gè)以With
開頭的函數(shù), 這幾個(gè)函數(shù)的主要功能是實(shí)例化不同類型的context
通過
Background()
和TODO()
創(chuàng)建最emptyCtx
實(shí)例 ,通常是作為根節(jié)點(diǎn)通過
WithCancel()
創(chuàng)建cancelCtx
實(shí)例通過
WithValue()
創(chuàng)建valueCtx
實(shí)例通過
WithDeadline
和WithTimeout
創(chuàng)建timerCtx
實(shí)例
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) {
// 創(chuàng)建cancelCtx實(shí)例
c := newCancelCtx(parent)
// 添加到父節(jié)點(diǎn)的children中
propagateCancel(parent, &c)
// 返回實(shí)例和方法
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 關(guān)閉原因
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() {
// 創(chuàng)建 cancelCtx 實(shí)例
// 傳入context.Background() 作為根節(jié)點(diǎn)
ctx, cancel := context.WithCancel(context.Background())
// 向協(xié)程中傳遞ctx
go MyDo1(ctx)
time.Sleep(5 * time.Second)
fmt.Println("stop all goroutines")
// 執(zhí)行cancel操作
cancel()
time.Sleep(2 * time.Second)
}
3.2 WithDeadline
設(shè)置了
deadline
的context
這個(gè)deadline(最終期限) 表示context在指定的時(shí)刻結(jié)束
源碼如下
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() {
// 設(shè)置deadline為當(dāng)前時(shí)間之后的5秒那個(gè)時(shí)刻
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
實(shí)際就是調(diào)用了
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() {
// 設(shè)置為6秒后context結(jié)束
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
基礎(chǔ)上添加了 key : value 的鍵值對
context
形成的樹狀結(jié)構(gòu),后面的節(jié)點(diǎn)可以訪問前面節(jié)點(diǎn)傳導(dǎo)的數(shù)據(jù)
源碼如下 :
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