?文章來源地址http://www.zghlxwxcb.cn/news/detail-710267.html
Context本質(zhì)
golang標(biāo)準(zhǔn)庫里Context實際上是一個接口(即一種編程規(guī)范、 一種約定)。
type Context interface { Deadline() (deadline time.Time, ok bool) Done() <-chan struct{} Err() error Value(key any) any }
?
通過查看源碼里的注釋,我們得到如下約定:
- Done()函數(shù)返回一個只讀管道,且管道里不存放任何元素(struct{}),所以用這個管道就是為了實現(xiàn)阻塞
- Deadline()用來記錄到期時間,以及是否到期。
- Err()用來記錄Done()管道關(guān)閉的原因,比如可能是因為超時,也可能是因為被強(qiáng)行Cancel了。
- Value()用來返回key對應(yīng)的value,你可以想像成Context內(nèi)部維護(hù)了一個map。
Context實現(xiàn)
go源碼里提供了Context接口的一個具體實現(xiàn),遺憾的是它只是一個空的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 any) any { return nil }
?
emptyCtx以小寫開頭,包外不可見,所以golang又提供了Background和TODO這2個函數(shù)讓我們能獲取到emptyCtx。
var ( background = new(emptyCtx) todo = new(emptyCtx) ) func Background() Context { return background } func TODO() Context { return todo }
?
backgroud和todo明明是一模一樣的東西,就是emptyCtx,為什么要搞2個呢?真心求教,知道的同學(xué)請在評論區(qū)告訴我。
emptyCtx有什么用?創(chuàng)建Context時通常需要傳遞一個父Context,emptyCtx用來充當(dāng)最初的那個Root Context。
With Value
當(dāng)業(yè)務(wù)邏輯比較復(fù)雜,函數(shù)調(diào)用鏈很長時,參數(shù)傳遞會很復(fù)雜,如下圖:
f1產(chǎn)生的參數(shù)b要傳給f2,雖然f2并不需要參數(shù)b,但f3需要,所以b還是得往后傳。
如果把每一步產(chǎn)生的新變量都放到Context這個大容器里,函數(shù)之間只傳遞Context,需要什么變量時直接從Context里取,如下圖:
?
f2能從context里取到a和b,f4能從context里取到a、b、c、d。
package main import ( "context" "fmt" ) func step1(ctx context.Context) context.Context { //根據(jù)父context創(chuàng)建子context,創(chuàng)建context時允許設(shè)置一個<key,value>對,key和value可以是任意數(shù)據(jù)類型 child := context.WithValue(ctx, "name", "大臉貓") return child } func step2(ctx context.Context) context.Context { fmt.Printf("name %s\n", ctx.Value("name")) //子context繼承了父context里的所有key value child := context.WithValue(ctx, "age", 18) return child } func step3(ctx context.Context) { fmt.Printf("name %s\n", ctx.Value("name")) //取出key對應(yīng)的value fmt.Printf("age %d\n", ctx.Value("age")) } func main1() { grandpa := context.Background() //空context father := step1(grandpa) //father里有一對<key,value> grandson := step2(father) //grandson里有兩對<key,value> step3(grandson) }
?
Timeout
在視頻?https://www.bilibili.com/video/BV1C14y127sv/?里介紹了超時實現(xiàn)的核心原理,視頻中演示的done管道可以用Context的Done()來替代,Context的Done()管道什么時候會被關(guān)系呢?2種情況:
1. 通過context.WithCancel創(chuàng)建一個context,調(diào)用cancel()時會關(guān)閉context.Done()管道。
func f1() { ctx, cancel := context.WithCancel(context.Background()) go func() { time.Sleep(100 * time.Millisecond) cancel() //調(diào)用cancel,觸發(fā)Done }() select { case <-time.After(300 * time.Millisecond): fmt.Println("未超時") case <-ctx.Done(): //ctx.Done()是一個管道,調(diào)用了cancel()都會關(guān)閉這個管道,然后讀操作就會立即返回 err := ctx.Err() //如果發(fā)生Done(管道被關(guān)閉),Err返回Done的原因,可能是被Cancel了,也可能是超時了 fmt.Println("超時:", err) //context canceled } }
?
2. 通過context.WithTimeout創(chuàng)建一個context,當(dāng)超過指定的時間或者調(diào)用cancel()時會關(guān)閉context.Done()管道。
func f2() { ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100) //超時后會自動調(diào)用context的Deadline,Deadline會,觸發(fā)Done defer cancel() select { case <-time.After(300 * time.Millisecond): fmt.Println("未超時") case <-ctx.Done(): //ctx.Done()是一個管道,context超時或者調(diào)用了cancel()都會關(guān)閉這個管道,然后讀操作就會立即返回 err := ctx.Err() //如果發(fā)生Done(管道被關(guān)閉),Err返回Done的原因,可能是被Cancel了,也可能是超時了 fmt.Println("超時:", err) //context deadline exceeded } }
?
Timeout的繼承問題
通過context.WithTimeout創(chuàng)建的Context,其壽命不會超過父Context的壽命。比如:
- 父Context設(shè)置了10號到期,5號誕生了子Context,子Context設(shè)置了100天后到期,則實際上10號的時候子Context也會到期。
- 父Context設(shè)置了10號到期,5號誕生了子Context,子Context設(shè)置了1天后到期,則實際上6號的時候子Context就會到期。
func inherit_timeout() { parent, cancel1 := context.WithTimeout(context.Background(), time.Millisecond*1000) //parent設(shè)置100ms超時 t0 := time.Now() defer cancel1() time.Sleep(500 * time.Millisecond) //消耗掉500ms // child, cancel2 := context.WithTimeout(parent, time.Millisecond*1000) //parent還剩500ms,child設(shè)置了1000ms之后到期,child.Done()管道的關(guān)閉時刻以較早的為準(zhǔn),即500ms后到期 child, cancel2 := context.WithTimeout(parent, time.Millisecond*100) //parent還剩500ms,child設(shè)置了100ms之后到期,child.Done()管道的關(guān)閉時刻以較早的為準(zhǔn),即100ms后到期 t1 := time.Now() defer cancel2() select { case <-child.Done(): t2 := time.Now() fmt.Println(t2.Sub(t0).Milliseconds(), t2.Sub(t1).Milliseconds()) fmt.Println(child.Err()) //context deadline exceeded } }
?
context超時在http請求中的實際應(yīng)用
定心丸來了,最后說一遍:”context在實踐中真的很有用“
客戶端發(fā)起http請求時設(shè)置了一個2秒的超時時間:
package main import ( "fmt" "io/ioutil" "net/http" "time" ) func main() { client := http.Client{ Timeout: 2 * time.Second, //小于10秒,導(dǎo)致請求超時,會觸發(fā)Server端的http.Request.Context的Done } if resp, err := client.Get("http://127.0.0.1:5678/"); err == nil { defer resp.Body.Close() fmt.Println(resp.StatusCode) if bs, err := ioutil.ReadAll(resp.Body); err == nil { fmt.Println(string(bs)) } } else { fmt.Println(err) //Get "http://127.0.0.1:5678/": context deadline exceeded (Client.Timeout exceeded while awaiting headers) } }
?
服務(wù)端從Request里取提context,故意休息10秒鐘,同時監(jiān)聽context.Done()管道有沒有關(guān)閉。由于Request的context是2秒超時,所以服務(wù)端還沒休息夠context.Done()管道就關(guān)閉了。
package main import ( "fmt" "net/http" "time" ) func welcome(w http.ResponseWriter, req *http.Request) { ctx := req.Context() //取得request的context select { case <-time.After(10 * time.Second): //故意慢一點,10秒后才返回結(jié)果 fmt.Fprintf(w, "welcome") case <-ctx.Done(): //超時后client會撤銷請求,觸發(fā)ctx.cancel(),從而關(guān)閉Done()管道 err := ctx.Err() //如果發(fā)生Done(管道被關(guān)閉),Err返回Done的原因,可能是被Cancel了,也可能是超時了 fmt.Println("server:", err) //context canceled } } func main() { http.HandleFunc("/", welcome) http.ListenAndServe(":5678", nil) }
?文章來源:http://www.zghlxwxcb.cn/news/detail-710267.html
?
到了這里,關(guān)于golang Context應(yīng)用舉例的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!