golang 1.7版本中context库被很多标准库的模块所使用,比如net/http和os的一些模块中,利用这些原生模块,我们就不需要自己再写上下文的管理器了,直接调用函数接口即可实现,利用context我们可以实现一些比如请求的声明周期内的变量管理,执行一些操作的超时等等。

保存上下文对象

这里我们通过一个简单的例子来看一下如何使用context的特性来实现上下文的对象保存,这里我们写了一个简单的http server,具有登录和退出,状态检查路由(检查用户是否登录)

func main(){
  mux:=http.NewServeMux()
  mux.HandleFunc("/",StatusHandler)
  mux.HandleFunc("/login",LoginHandler)
  mux.HandleFunc("/logout",LogoutHandler)
  contextedMux:=AddContextSupport(mux)
  log.Fatal(http.ListenAndServe(":8080",contextedMux))
}

其中的AddContextSupport是一个中间件,用来绑定一个context到原来的handler中,所有的请求都必须先经过该中间件后才能进入各自的路由处理中。具体的实现代码如下:

func AddContextSupport(next http.Handler)http.Handler{
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    log.Println(r.Method, "-", r.RequestURI)
    cookie, _ := r.Cookie("username")
    if cookie != nil {
      ctx := context.WithValue(r.Context(), "username", cookie.Value)
      // WithContext returns a shallow copy of r with its context changed
      // to ctx. The provided ctx must be non-nil.
      next.ServeHTTP(w, r.WithContext(ctx))
    } else {
      next.ServeHTTP(w, r)
    }
  })
}

该中间件可以打印每次请求的方法和请求的url,然后获得请求的cookie值,如果cookie为空的话则继续传递到对应的路由处理函数中,否则保存cookie的值到Context, 注意这里的Context()是request对象的方法, 将创建一个新的上下文对象(如果context为空),context.WithValue()函数将key和value保存在新的上下文对象中并返回该对象。

其余的路由处理函数代码如下, 分别用来保存cookie的登录路由LoginHandler(),还有删除cookie的退出路由处理函数LogoutHandler()。

func LoginHandler(w http.ResponseWriter,r *http.Request){
  expitation := time.Now().Add(24*time.Hour)
  var username string
  if username=r.URL.Query().Get("username");username==""{
    username = "guest"
  }
  cookie:=http.Cookie{Name:"username",Value:username,Expires:expitation}
  http.SetCookie(w,&cookie)
}

func LogoutHandler(w http.ResponseWriter, r *http.Request) {
  expiration := time.Now().AddDate(0, 0, -1)
  cookie := http.Cookie{Name: "username", Value: "alice_cooper@gmail.com", Expires: expiration}
  http.SetCookie(w, &cookie)
}

这里我们在请求/login的时候,可以传递一个参数username到函数中,比如/login"htmlcode">

func StatusHandler(w http.ResponseWriter,r *http.Request){

  if username:=r.Context().Value("username"); username!=nil{
    w.WriteHeader(http.StatusOK)
    w.Write([]byte("Hi username:"+username.(string)+"\n"))
  }else{
    w.WriteHeader(http.StatusNotFound)
    w.Write([]byte("Not Logged in"))
  }
}

我们不仅仅可以在上下文中保存简单的类型,我们可以保存任何类型的数据,因为Value() 返回的对象是一个interface{}对象,所以我们需要转换一下才能使用。

超时处理

对于简单的保存和传递对象,使用context的确很方便,但是该库的使用不仅仅是保存变量,还可以创建一个超时和取消的行为,比如说我们web端去请求了其他的资源,但是该资源的处理比较耗时,我们无法预见什么时候能够返回,如果让用户超时的话,实在是不太好,所以我们需要创建一个超时的操作,主动判断是否超时,然后传递一个合适的行为给用户。

这里我们现在路由中增加一个长期运行的job路由

mux.HandleFunc("/longjob",jobWithCancelHandler)

具体的处理如下,我们的handler会利用WithCancel() 返回一个新的(如果没有创建)或者原来已保存的上下文,还有一个cancel对象,这个对象可以用来手动执行取消操作。另外我们的url中可以指定这个任务模拟执行的长度,比如/longjob"htmlcode">

func longRunningCalculation(timeCost int)chan string{

  result:=make(chan string)
  go func (){
    time.Sleep(time.Second*(time.Duration(timeCost)))
    result<-"Done"
  }()
  return result
}
func jobWithCancelHandler(w http.ResponseWriter, r * http.Request){
  var ctx context.Context
  var cancel context.CancelFunc
  var jobtime string
  if jobtime=r.URL.Query().Get("jobtime");jobtime==""{
    jobtime = "10"
  }
  timecost,err:=strconv.Atoi(jobtime)
  if err!=nil{
    timecost=10
  }
  log.Println("Job will cost : "+jobtime+"s")
  ctx,cancel = context.WithCancel(r.Context())
  defer cancel()

  go func(){
    time.Sleep(5*time.Second)
    cancel()
  }()

  select{
  case <-ctx.Done():
    log.Println(ctx.Err())
    return
  case result:=<-longRunningCalculation(timecost):
    io.WriteString(w,result)
  }
  return
}

这就是使用context的一些基本方式,其实context还有很多函数这里没有涉及,比如WithTimeout和WithDeadline等,但是使用上都比较相似,这里就不在举例。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
Golang,Context,上下文

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“使用Golang的Context管理上下文的方法”

暂无“使用Golang的Context管理上下文的方法”评论...