Golang并发模型之Context详解

对于 Golang 开发者来说context(上下文)包一定不会陌生。但很多时候,我们懒惰的只是见过它,或能起到什么作用,并不会去深究它。
应用场景:在 Go http 包的 Server 中,每一个请求在都有一个对应的goroutine去处理。请求处理函数通常会启动额外的goroutine用来访问后端服务,比如数据库和 RPC 服务。用来处理一个请求的goroutine通常需要访问一些与请求特定的数据,比如终端用户的身份认证信息、验证相关的 token、请求的截止时间。当一个请求被取消或超时时,所有用来处理该请求的goroutine都应该迅速退出,然后系统才能释放这些goroutine占用的资源,官方博客。
注意:
go1.6及之前版本请使用golang.org/x/net/context。go1.7及之后已移到标准库context。
Context 原理
Context 的调用应该是链式的,通过WithCancel,WithDeadline,WithTimeout或WithValue派生出新的 Context。当父 Context 被取消时,其派生的所有 Context 都将取消。
通过context.WithXXX都将返回新的 Context 和 CancelFunc。调用 CancelFunc 将取消子代,移除父代对子代的引用,并且停止所有定时器。未能调用 CancelFunc 将泄漏子代,直到父代被取消或定时器触发。go vet工具检查所有流程控制路径上使用 CancelFuncs。
遵循规则
遵循以下规则,以保持包之间的接口一致,并启用静态分析工具以检查上下文传播。
- 不要将 Contexts 放入结构体,相反
context应该作为第一个参数传入,命名为ctx。func DoSomething(ctx context.Context,arg Arg)error { // ... use ctx ... } - 即使函数允许,也不要传入
nil的 Context。如果不知道用哪种 Context,可以使用context.TODO()。 - 使用context的Value相关方法只应该用于在程序和接口中传递的和请求相关的元数据,不要用它来传递一些可选的参数
- 相同的 Context 可以传递给在不同的
goroutine;Context 是并发安全的。
Context 包
Context 结构体。
// A Context carries a deadline, cancelation signal, and request-scoped values
// across API boundaries. Its methods are safe for simultaneous use by multiple
// goroutines.
type Context interface {
// Done returns a channel that is closed when this Context is canceled
// or times out.
Done() <-chan struct{}
// Err indicates why this context was canceled, after the Done channel
// is closed.
Err() error
// Deadline returns the time when this Context will be canceled, if any.
Deadline() (deadline time.Time, ok bool)
// Value returns the value associated with key or nil if none.
Value(key interface{}) interface{}
}
- Done(),返回一个channel。当times out或者调用cancel方法时,将会close掉。
- Err(),返回一个错误。该context为什么被取消掉。
- Deadline(),返回截止时间和ok。
- Value(),返回值。
所有方法
func Background() Context
func TODO() Context
func WithCancel(parent Context) (ctx Context, cancel CancelFunc)
func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc)
func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)
func WithValue(parent Context, key, val interface{}) Context
上面可以看到Context是一个接口,想要使用就得实现其方法。在context包内部已经为我们实现好了两个空的Context,可以通过调用Background()和TODO()方法获取。一般的将它们作为Context的根,往下派生。
WithCancel 例子
WithCancel 以一个新的 Done channel 返回一个父 Context 的拷贝。
229 func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
230 c := newCancelCtx(parent)
231 propagateCancel(parent, &c)
232 return &c, func() { c.cancel(true, Canceled) }
233 }
234
235 // newCancelCtx returns an initialized cancelCtx.
236 func newCancelCtx(parent Context) cancelCtx {
237 return cancelCtx{
238 Context: parent,
239 done: make(chan struct{}),
240 }
241 }
此示例演示使用一个可取消的上下文,以防止 goroutine 泄漏。示例函数结束时,defer 调用 cancel 方法,gen goroutine 将返回而不泄漏。
package main
import (
"context"
"fmt"
)
func main() {
// gen generates integers in a separate goroutine and
// sends them to the returned channel.
// The callers of gen need to cancel the context once
// they are done consuming generated integers not to leak
// the internal goroutine started by gen.
gen := func(ctx context.Context) <-chan int {
dst := make(chan int)
n := 1
go func() {
for {
select {
case <-ctx.Done():
return // returning not to leak the goroutine
case dst <- n:
n++
}
}
}()
return dst
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel() // cancel when we are finished consuming integers
for n := range gen(ctx) {
fmt.Println(n)
if n == 5 {
break
}
}
}
WithDeadline 例子
369 func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) {
370 if cur, ok := parent.Deadline(); ok && cur.Before(deadline) {
371 // The current deadline is already sooner than the new one.
372 return WithCancel(parent)
373 }
374 c := &timerCtx{
375 cancelCtx: newCancelCtx(parent),
376 deadline: deadline,
377 }
......
可以清晰的看到,当派生出的子 Context 的deadline在父Context之后,直接返回了一个父Context的拷贝。故语义上等效为父。
WithDeadline 的最后期限调整为不晚于 d 返回父上下文的副本。如果父母的截止日期已经早于 d,WithDeadline (父,d) 是在语义上等效为父。返回的上下文完成的通道关闭的最后期限期满后,返回的取消函数调用时,或当父上下文完成的通道关闭,以先发生者为准。
看看官方例子:
package main
import (
"context"
"fmt"
"time"
)
func main() {
d := time.Now().Add(50 * time.Millisecond)
ctx, cancel := context.WithDeadline(context.Background(), d)
// Even though ctx will be expired, it is good practice to call its
// cancelation function in any case. Failure to do so may keep the
// context and its parent alive longer than necessary.
defer cancel()
select {
case <-time.After(1 * time.Second):
fmt.Println("overslept")
case <-ctx.Done():
fmt.Println(ctx.Err())
}
}
WithTimeout 例子
WithTimeout 返回 WithDeadline(parent, time.Now().Add(timeout))。
436 func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
437 return WithDeadline(parent, time.Now().Add(timeout))
438 }
看看官方例子:
package main
import (
"context"
"fmt"
"time"
)
func main() {
// Pass a context with a timeout to tell a blocking function that it
// should abandon its work after the timeout elapses.
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
select {
case <-time.After(1 * time.Second):
fmt.Println("overslept")
case <-ctx.Done():
fmt.Println(ctx.Err()) // prints "context deadline exceeded"
}
}
WithValue 例子
454 func WithValue(parent Context, key, val interface{}) Context {
454 if key == nil {
455 panic("nil key")
456 }
457 if !reflect.TypeOf(key).Comparable() {
458 panic("key is not comparable")
459 }
460 return &valueCtx{parent, key, val}
461 }
WithValue 返回的父与键关联的值在 val 的副本。
使用上下文值仅为过渡进程和 Api 的请求范围的数据,而不是将可选参数传递给函数。
提供的键必须是可比性和应该不是字符串类型或任何其他内置的类型以避免包使用的上下文之间的碰撞。WithValue 用户应该定义自己的键的类型。为了避免分配分配给接口 {} 时,上下文键经常有具体类型结构 {}。另外,导出的上下文关键变量静态类型应该是一个指针或接口。
看看官方例子:
package main
import (
"context"
"fmt"
)
func main() {
type favContextKey string
f := func(ctx context.Context, k favContextKey) {
if v := ctx.Value(k); v != nil {
fmt.Println("found value:", v)
return
}
fmt.Println("key not found:", k)
}
k := favContextKey("language")
ctx := context.WithValue(context.Background(), k, "Go")
f(ctx, k)
f(ctx, favContextKey("color"))
}
参考连接
[1] https://segmentfault.com/a/1190000006744213
[2] http://www.01happy.com/golang-context-reading/
我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻。
Golang并发模型之Context详解的更多相关文章
- golang语言中的context详解,Go Concurrency Patterns: Context
https://blog.golang.org/context Introduction In Go servers, each incoming request is handled in its ...
- Java内存模型相关原则详解
在<Java内存模型(JMM)详解>一文中我们已经讲到了Java内存模型的基本结构以及相关操作和规则.而Java内存模型又是围绕着在并发过程中如何处理原子性.可见性以及有序性这三个特征来构 ...
- CSS3盒模型display:box详解
display:box;box-flex是css3新添加的盒子模型属性,它的出现可以解决我们通过N多结构.css实现的布局方式.经典的一个布局应用就是布局的垂直等高.水平均分.按比例划分. 目前box ...
- Android中Context详解 ---- 你所不知道的Context
转自:http://blog.csdn.net/qinjuning/article/details/7310620Android中Context详解 ---- 你所不知道的Context 大家好, ...
- 基于模型的特征选择详解 (Embedded & Wrapper)
目录 基于模型的特征选择详解 (Embedded & Wrapper) 1. 线性模型和正则化(Embedded方式) 2. 基于树模型的特征选择(Embedded方式) 3. 顶层特征选择算 ...
- C++11 并发指南四(<future> 详解二 std::packaged_task 介绍)
上一讲<C++11 并发指南四(<future> 详解一 std::promise 介绍)>主要介绍了 <future> 头文件中的 std::promise 类, ...
- C++11 并发指南四(<future> 详解三 std::future & std::shared_future)
上一讲<C++11 并发指南四(<future> 详解二 std::packaged_task 介绍)>主要介绍了 <future> 头文件中的 std::pack ...
- Kaggle网站流量预测任务第一名解决方案:从模型到代码详解时序预测
Kaggle网站流量预测任务第一名解决方案:从模型到代码详解时序预测 2017年12月13日 17:39:11 机器之心V 阅读数:5931 近日,Artur Suilin 等人发布了 Kaggl ...
- Android中Context详解 ---- 你所不知道的Context(转)
Android中Context详解 ---- 你所不知道的Context(转) 本文出处 :http://b ...
随机推荐
- SQL Server进阶 遍历表的几种方法
https://www.cnblogs.com/mcgrady/p/4182486.html
- 20155324 2016-2017-2 《Java程序设计》第3周学习总结
20155324 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 定义类 要产生对象必须先定义类,类是对象的设计图,对象是类的实例.类定义时使用class关键 ...
- flask的基础认识
刚开始学习flask基础知识,有了一点点的认识,所以在此大概写一下自己的理解,详细步骤和功能在代码段介绍: from flask import Flask,render_template,reques ...
- GitHub贡献第一的公司是谁?微软开源软件列表
参考:http://www.infoq.com/cn/news/2017/03/GitHub-first-Microsoft-open-sour 提起微软公司,不少人第一反应是老牌巨头专注于私有化软件 ...
- win7用VMware安装CentOs7搭建Linux环境
1. 首先要安装VMware 安装及破解教程 http://www.cnblogs.com/zhangqian27/p/9088237.html 2. 下载os镜像 CentOS下载地址: 阿里云开源 ...
- 2、jQuery的基本概念-必看-版本-入口函数- jq对象和dom对象区别
1.4. jQuery的版本 官网下载地址:http://jquery.com/download/ jQuery版本有很多,分为1.x 2.x 3.x 大版本分类: 1.x版本:能够兼容IE678浏览 ...
- VMware 11安装Mac和Linux
VMware 11安装Mac OS X 10.10_百度经验 https://jingyan.baidu.com/article/ff411625b9011212e48237b4.html?qq-pf ...
- [Docker]CentOS7通过rpm包安装Docker
前几天写过一篇文章:[Docker]CentOS7下Docker安装教程,直接使用yum命令下载Docker即可.但是前提是你需要***,所以这篇文章来讲讲,如果不会***,该如何安装Docker. ...
- SSH命令行管理文件
1.从服务器上下载文件 ssh root@13.111.122.133 2.从服务器上下载文件 scp username@servername:/path/filename /var/www/loca ...
- 【转】python模块分析之hashlib加密(二)
[转]python模块分析之hashlib加密(二) hashlib模块是用来对字符串进行hash加密的模块,明文与密文是一一对应不变的关系:用于注册.登录时用户名.密码等加密使用.一.函数分析:1. ...