Context包原始碼決議
Context就相當于一個樹狀結構
最后請回答一下這個問題:context包中的方法是執行緒安全嗎?
Context包中主要有一個介面和三個結構體
Context介面
type Context interface {
Deadline() (deadline time.Time, ok bool)
Done() <-chan struct{}
Err() error
Value(key interface{}) interface{}
}
結構體
type valueCtx struct {
Context
key, val interface{}
}
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
}
type timerCtx struct {
cancelCtx
timer *time.Timer // Under cancelCtx.mu.
deadline time.Time
}
Context包有兩個根實體
package context
...
var (
background = new(emptyCtx)
todo = new(emptyCtx)
)
分別通過以下兩個方法回傳
其中Background()方法是回傳初始化時自動實體化的background物件,TODO方法跟Background()相同
-
context.Background() -
context.TODO()
func Background() Context {
return background
}
TODO方法跟Background()相同
func TODO() Context {
return todo
}
那么emptyCtx又是什么?
emptyCtx是一個自定義的型別,底層型別為int,實作了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 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"
}
context包中常用的幾個方法
-
創建具有dealline的Context
WithDeadline(parent Context, d time.Time) (Context, CancelFunc) -
創建具有取消方法的Context
WithCancel(parent Context) (ctx Context, cancel CancelFunc) -
創建具有超時的Context
WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) -
創建具有可保存鍵值的Context
WithValue(parent Context, key, val interface{}) Context
WithValue(parent Context, key, val interface{}) Context
type valueCtx struct {
Context //相當于父節點
key, val interface{}
}
func WithValue(parent Context, key, val interface{}) 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}
}
//重寫了Context介面中的 Value(key interface{})方法
func (c *valueCtx) Value(key interface{}) interface{} {
//先從自己節點中的鍵對值去尋找
if c.key == key {
return c.val
}
//找不到就往上遞回,依次尋找系結的父節點的value
return c.Context.Value(key)
}
寫一個小demo驗證一下
func main() {
ctx := context.WithValue(context.Background(), "xiaofu", "test")
ctx1 := context.WithValue(ctx, "xiaofu1", "test1")
fmt.Println(ctx1.Value("xiaofu1"))
fmt.Println(ctx1.Value("xiaofu"))
}
//輸出
test1
test
<nil>
//說明是會往上遞回,直到找到background的根節點
WithCancel(parent Context) (ctx Context, cancel CancelFunc)
查看以下代碼,都會發現每次新建Context,都會系結父節點的Context
type cancelCtx struct {
Context //父節點
mu sync.Mutex // 鎖
done chan struct{} // channel,用于關閉
children map[canceler]struct{} // 用于儲存子節點中的cancelCtx
err error // set to non-nil by the first cancel call
}
//重寫了Value方法,當key為cancelCtxKey時,回傳當前的cancelCtx,否則不斷向上遞回尋找cancelCtx
func (c *cancelCtx) Value(key interface{}) interface{} {
if key == &cancelCtxKey {
return c
}
return c.Context.Value(key)
}
//重寫了Done方法
func (c *cancelCtx) Done() <-chan struct{} {
//上鎖
c.mu.Lock()
//初始化done的channel,根節點的Done()方法回傳的是nil
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
}
var closedchan = make(chan struct{})
//用于控制取消操作的介面,其中因為cancelCtx實作了cancel方法和Done()方法,所以默認實作該介面
type canceler interface {
cancel(removeFromParent bool, err error)
Done() <-chan struct{}
}
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) }
}
//創建cancelCtx結構體,并系結父節點
func newCancelCtx(parent Context) cancelCtx {
return cancelCtx{Context: parent}
}
WithDeadline(parent Context, d time.Time) (Context, CancelFunc)
type timerCtx struct {
cancelCtx
timer *time.Timer // Under cancelCtx.mu.
deadline time.Time
}
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)
}
//把當前的父節點用一個中間物件cancelCtx做轉換,同時系結到timeCtx中
//約等于 temp := cancelCtx{Context: parent}
// c := &timerCtx{
// cancelCtx: temp,
// deadline: d,
// }
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) }
}
WithTimeout(parent Context, timeout time.Duration)
//相當于在當前時間dealline的基礎上,往后延遲一段時間,所以可以呼叫WithDeadline方法
func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
return WithDeadline(parent, time.Now().Add(timeout))
}
context包中的方法是執行緒安全嗎?
是執行緒安全
因為每次執行WithXXX方法,都會新建一個context物件,并且把父物件進行系結,
見demo
func main() {
ctx := context.WithValue(context.Background(), "xiaofu", "test")
go func() {
_ = context.WithValue(ctx, "xiaofu", "test1")
}()
go func() {
_ = context.WithValue(ctx, "xiaofu", "test2")
}()
fmt.Println(ctx.Value("xiaofu"))
fmt.Println(ctx.Value("xiaofu"))
time.Sleep(3 * time.Second)
}
//輸出
//test
//test
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/426341.html
標籤:Go
下一篇:Java如何實作消費資料隔離?
