「讀源碼」為什么注冊路由時沒有傳入上下文,在接口方法中卻能取到?

這篇文章回答一下知識星球里星友的提問,是一個非常好的問題,也歡迎大家留言討論。

提問
在gofame框架的demo案例中,如下圖所示:


為什么左側路由綁定這里沒有向controller中傳入context的值,在controller中卻能取到值?
如何賦值和接收context?
先說結論

關于ctx context.Context上下文,Server組件會自動從請求中獲取并傳遞給接口方法,聲明并初始化了context初始值為context.Background()
可以通過GetCtx、SetCtx、GetCtxVar、SetCtxVar這些方法輕松的為context賦值和取值
通過示例代碼輕松可知:我們可以通過ghttp.Request實例輕松的操作context。
解答
問題1. 為什么左側路由綁定這里沒有向controller中傳入context的值,在controller中卻能取到值?

先說結論:關于ctx context.Context上下文,Server組件會自動從請求中獲取并傳遞給接口方法。

關鍵代碼是上圖中的s := g.Server():

追蹤一下它的源碼可以發(fā)現(xiàn):聲明并初始化了ctx的初始值:context.Background()


再帶大家看一下context.Background()的源碼和注釋。

// 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
}
我們可以發(fā)現(xiàn)這里返回了一個non-nil, empty Context


問題2. 如何賦值和接收context?

在GoFrame框架中,官方推薦的正是使用Context上下文對象來處理流程共享的上下文變量,甚至將該對象進一步傳遞到依賴的各個模塊方法中。

該Context對象類型實現(xiàn)了標準庫的context.Context接口,該接口往往會作為模塊間調用方法的第一個參數(shù),該接口參數(shù)也是Golang官方推薦的在模塊間傳遞上下文變量的推薦方式。

方法列表:
func (r *Request) GetCtx() context.Context
func (r *Request) SetCtx(ctx context.Context)
func (r *Request) GetCtxVar(key interface{}, def ...interface{}) *gvar.Var
func (r *Request) SetCtxVar(key interface{}, value interface{})
簡要說明:
GetCtx方法用于獲取當前的context.Context對象,作用同Context方法。
SetCtx方法用于設置自定義的context.Context上下文對象。
GetCtxVar方法用于獲取上下文變量,并可給定當該變量不存在時的默認值。
SetCtxVar方法用于設置上下文變量。
使用示例
示例1,SetCtxVar/GetCtxVar
package main

import (
 "github.com/gogf/gf/v2/frame/g"
 "github.com/gogf/gf/v2/net/ghttp"
)

const (
 TraceIdName = "trace-id"
)

func main() {
 s := g.Server()
 s.Group("/", func(group *ghttp.RouterGroup) {
  group.Middleware(func(r *ghttp.Request) {
      //向context中賦值
   r.SetCtxVar(TraceIdName, "1234567890abcd")
   r.Middleware.Next()
  })
  group.ALL("/", func(r *ghttp.Request) {
      //從context中取值
   r.Response.Write(r.GetCtxVar(TraceIdName))
  })
 })
 s.SetPort(8199)
 s.Run()
}
可以看到:我們可以通過SetCtxVar和GetCtxVar來設置和獲取自定義的變量,該變量生命周期僅限于當前請求流程。

執(zhí)行后,訪問 http://127.0.0.1:8199/ ,頁面輸出內(nèi)容為:1234567890abcd

示例2,SetCtx
SetCtx方法常用于中間件中整合一些第三方的組件,例如第三方的鏈路跟蹤組件等等。

為簡化示例,這里我們將上面的例子通過SetCtx方法來改造一下來做演示。

package main

import (
 "context"
 "github.com/gogf/gf/v2/frame/g"
 "github.com/gogf/gf/v2/net/ghttp"
)

const (
 TraceIdName = "trace-id"
)

func main() {
 s := g.Server()
 s.Group("/", func(group *ghttp.RouterGroup) {
  group.Middleware(func(r *ghttp.Request) {
   ctx := context.WithValue(r.Context(), TraceIdName, "1234567890abcd")
   r.SetCtx(ctx)
   r.Middleware.Next()
  })
  group.ALL("/", func(r *ghttp.Request) {
      //看到這里的示例代碼,更能解答問題1,通過ghttp.Request可以輕松獲得上下文對象
   r.Response.Write(r.Context().Value(TraceIdName))
   // 也可以使用
   // r.Response.Write(r.GetCtxVar(TraceIdName))
  })
 })
 s.SetPort(8199)
 s.Run()
}
執(zhí)行后,訪問 http://127.0.0.1:8199/ ,頁面輸出內(nèi)容為:1234567890abcd

總結
通過上面的示例,我們能更好的理解這位星友提出的困惑:

關于ctx context.Context上下文,Server組件會自動從請求中獲取并傳遞給接口方法,聲明并初始化了context初始值為context.Background()
可以通過GetCtx、SetCtx、GetCtxVar、SetCtxVar這些方法輕松的為context賦值和取值
通過示例代碼輕松可知:我們可以通過ghttp.Request實例輕松的操作context。




請前往:http://lygongshang.com/TeacherV2.html?id=365