国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Golang > 正文

深入Golang之context的用法詳解

2020-04-01 18:58:14
字體:
來源:轉載
供稿:網友

context在Golang的1.7版本之前,是在包golang.org/x/net/context中的,但是后來發現其在很多地方都是需要用到的,所有在1.7開始被列入了Golang的標準庫。Context包專門用來簡化處理單個請求的多個goroutine之間與請求域的數據、取消信號、截止時間等相關操作,那么這篇文章就來看看其用法和實現原理。

源碼分析

首先我們來看一下Context里面核心的幾個數據結構:

Context interface

type Context interface {  Deadline() (deadline time.Time, ok bool)  Done() <-chan struct{}  Err() error  Value(key interface{}) interface{}}

Deadline返回一個time.Time,是當前Context的應該結束的時間,ok表示是否有deadline。

Done方法在Context被取消或超時時返回一個close的channel,close的channel可以作為廣播通知,告訴給context相關的函數要停止當前工作然后返回。

Err方法返回context為什么被取消。

Value可以讓Goroutine共享一些數據,當然獲得數據是協程安全的。但使用這些數據的時候要注意同步,比如返回了一個map,而這個map的讀寫則要加鎖。

canceler interface

canceler interface定義了提供cancel函數的context:

type canceler interface {  cancel(removeFromParent bool, err error)  Done() <-chan struct{}}

其現成的實現有4個:

  1. emptyCtx:空的Context,只實現了Context interface;
  2. cancelCtx:繼承自Context并實現了cancelerinterface
  3. timerCtx:繼承自cancelCtx,可以用來設置timeout;
  4. valueCtx:可以儲存一對鍵值對;

繼承Context

context包提供了一些函數,協助用戶從現有的 Context 對象創建新的 Context 對象。這些Context對象形成一棵樹:當一個 Context對象被取消時,繼承自它的所有Context都會被取消。

Background是所有Context對象樹的根,它不能被取消,它是一個emptyCtx的實例:

var (  background = new(emptyCtx))func Background() Context {  return background}

生成Context的主要方法

WithCancel

func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {  c := newCancelCtx(parent)  propagateCancel(parent, &c)  return &c, func() { c.cancel(true, Canceled) }}

返回一個cancelCtx示例,并返回一個函數,可以在外層直接調用cancelCtx.cancel()來取消Context。

WithDeadline

func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) {  if cur, ok := parent.Deadline(); ok && cur.Before(deadline) {    return WithCancel(parent)  }  c := &timerCtx{    cancelCtx: newCancelCtx(parent),    deadline: deadline,  }  propagateCancel(parent, c)  d := time.Until(deadline)  if d <= 0 {    c.cancel(true, DeadlineExceeded) // deadline has already passed    return c, func() { c.cancel(true, Canceled) }  }  c.mu.Lock()  defer c.mu.Unlock()  if c.err == nil {    c.timer = time.AfterFunc(d, func() {      c.cancel(true, DeadlineExceeded)    })  }  return c, func() { c.cancel(true, Canceled) }}

返回一個timerCtx示例,設置具體的deadline時間,到達 deadline的時候,后代goroutine退出。

WithTimeout

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

和WithDeadline一樣返回一個timerCtx示例,實際上就是WithDeadline包了一層,直接傳入時間的持續時間,結束后退出。

WithValue

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

WithValue對應valueCtx ,WithValue是在Context中設置一個 map,這個Context以及它的后代的goroutine都可以拿到map 里的值。

例子

Context的使用最多的地方就是在Golang的web開發中,在http包的Server中,每一個請求在都有一個對應的goroutine去處理。請求處理函數通常會啟動額外的goroutine用來訪問后端服務,比如數據庫和RPC服務。用來處理一個請求的goroutine通常需要訪問一些與請求特定的數據,比如終端用戶的身份認證信息、驗證相關的token、請求的截止時間。 當一個請求被取消或超時時,所有用來處理該請求的 goroutine都應該迅速退出,然后系統才能釋放這些goroutine占用的資源。雖然我們不能從外部殺死某個goroutine,所以我就得讓它自己結束,之前我們用channel+select的方式,來解決這個問題,但是有些場景實現起來比較麻煩,例如由一個請求衍生出的各個 goroutine之間需要滿足一定的約束關系,以實現一些諸如有效期,中止goroutine樹,傳遞請求全局變量之類的功能。

保存上下文

func middleWare(next http.Handler) http.Handler {  return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {    ctx := context.WithValue(req.Context(),"key","value")    next.ServeHTTP(w, req.WithContext(ctx))  })}func handler(w http.ResponseWriter, req *http.Request) {  value := req.Context().Value("value").(string)  fmt.Fprintln(w, "value: ", value)  return}func main() {  http.Handle("/", middleWare(http.HandlerFunc(handler)))  http.ListenAndServe(":8080", nil)}

我們可以在上下文中保存任何的類型的數據,用于在整個請求的生命周期去傳遞使用。

超時控制

func longRunningCalculation(timeCost int)chan string{  result:=make(chan string)  go func (){  time.Sleep(time.Second*(time.Duration(timeCost)))    result<-"Done"  }()  return result}func jobWithTimeoutHandler(w http.ResponseWriter, r * http.Request){  ctx,cancel := context.WithTimeout(context.Background(), 3*time.Second)  defer cancel()  select{  case <-ctx.Done():    log.Println(ctx.Err())    return  case result:=<-longRunningCalculation(5):    io.WriteString(w,result)  }  return}func main() {  http.Handle("/", jobWithTimeoutHandler)  http.ListenAndServe(":8080", nil)}

這里用一個timerCtx來控制一個函數的執行時間,如果超過了這個時間,就會被迫中斷,這樣就可以控制一些時間比較長的操作,例如io,RPC調用等等。

除此之外,還有一個重要的就是cancelCtx的實例用法,可以在多個goroutine里面使用,這樣可以實現信號的廣播功能,具體的例子我這里就不再細說了。

總結

context包通過構建樹型關系的Context,來達到上一層Goroutine能對傳遞給下一層Goroutine的控制。可以傳遞一些變量來共享,可以控制超時,還可以控制多個Goroutine的退出。

據說在Google,要求Golang程序員把Context作為第一個參數傳遞給入口請求和出口請求鏈路上的每一個函數。這樣一方面保證了多個團隊開發的Golang項目能夠良好地協作,另一方面它是一種簡單的超時和取消機制,保證了臨界區數據在不同的Golang項目中順利傳遞。

所以善于使用context,對于Golang的開發,特別是web開發,是大有裨益的。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江北区| 哈巴河县| 迁安市| 祁连县| 海淀区| 沈阳市| 伊川县| 琼中| 孝昌县| 明星| 尉犁县| 阿克| 津南区| 新和县| 教育| 桂林市| 潜江市| 沭阳县| 满洲里市| 泰宁县| 蒲城县| 台中县| 临颍县| 米脂县| 溧阳市| 九龙坡区| 松溪县| 海淀区| 米脂县| 江油市| 工布江达县| 晋宁县| 岳池县| 江西省| 互助| 连云港市| 虞城县| 湛江市| 庄浪县| 科技| 巴林左旗|