🚀 优质资源分享 🚀
学习路线指引(点击解锁) | 知识定位 | 人群定位 |
---|---|---|
🧡 Python实战微信订餐小程序 🧡 | 进阶级 | 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。 |
💛Python量化交易实战💛 | 入门级 | 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统 |
实现一个小目标
很开心的一件事,学习了一个月的后端拿到一个13k的offer,今年年底目标拿到一个30k的go方向offer。
好了回归正文,这篇文章是回答交流时一个老哥的问题,跟go的context相关内容,上一篇(https://blog.csdn.net/dojo-lzz/p/16183006.html)讲了一些基础知识,这一篇继续在并发处理上进行研究。主要是Go Context的使用、原理。因为时间和精力有限,所以文章中大量引用相关资料中的内容以及图片,再此致敬。
Go Context
React中Context主要用来跨组件传递一些数据,Go中Context其中一个作用也跟传递数据有关,不过是在goroutine中相互传递数据;Context的另一个作用在于可以便捷关闭被创建出来的goroutine。
在实际中当服务器端收到一个请求时,很可能需要发送几个请求去请求其他服务的数据,由于Go 语法上的同步阻塞写法,我们一般会创建几个goroutine并发去做一些事情;那么这时候很可能几个goroutine之间需要共享数据,还有当request被取消时,创建的几个goroutine也应该被取消掉。那么这就是Go Context的用武之地。
关于协程泄露:
一般main函数是主协程,主协程执行完毕后子协程也会被销毁;但是对于服务来说,主协程不会执行完毕就退出。
所以如果每个请求都自己创建协程,而协程有没有受到完毕信息结束信息,可能处于阻塞状态,这种情况下才会产生协程泄露
context包中核心是Context接口:
type Context interface {
Deadline() (deadline time.Time, ok bool)
Done() <-chan struct{}
Err() error
Value(key interface{}) interface{}
}
- Deadline 方法返回当前Context被取消的时间,也就是完成工作的截止时间(deadline);
- Done方法需要返回一个channel,这个Channel会在当前工作完成或者上下文被取消之后关闭,可以在子goroutine中利用select进行监控,来回收子goroutine;多次调用Done方法会返回同一个Channel;
// Done is provided for use in select statements:
// // Stream generates values with DoSomething and sends them to out
// // until DoSomething returns an error or ctx.Done is closed.
// func Stream(ctx context.Context, out chan<- Value) error {
// for {
// v, err := DoSomething(ctx)
// if err != nil {
// return err
// }
// select {
// case <-ctx.Done():
// return ctx.Err()
// case out <- v:
// }
// }
// }
// See https://blog.golang.org/pipelines for more examples of how to use
// a Done channel for cancellation.
- Err方法会返回当前Context结束的原因,它只会在Done返回的Channel被关闭时才会返回空值:
- 如果当前Context被取消就会返回Canceled错误;
- 如果当前Context超时就会返回DeadlineExceeded错误;
- Value 方法会从Context中返回键对应的值,对于同一个上下文来说,多次调用Value并传入相同的Key会返回相同的结果,该方法仅用于传递跨API和进程间跟请求域的数据。
// // Package user defines a User type that's stored in Contexts.
// package user
// import "context"
// // User is the type of value stored in the Contexts.
// type User struct {...}
//
// // key is an unexported type for keys defined in this package.
// // This prevents collisions with keys defined in other packages.
// type key int
// // userKey is the key for user.User values in Contexts. It is
// // unexported; clients use user.NewContext and user.FromContext
// // instead of using this key directly.
// var userKey key
// // NewContext returns a new Context that carries value u.
// func NewContext(ctx context.Context, u *User) context.Context {
// return context.WithValue(ctx, userKey, u)
// }
// // FromContext returns the User value stored in ctx, if any.
// func FromContext(ctx context.Context) (*User, bool) {
// u, ok := ctx.Value(userKey).(*User)
// return u, ok
// }
ctx.Value(userKey).(*User)这里是Go语言中的类型断言(http://c.biancheng.net/view/4281.html)
value, ok := x.(T)
x 表示一个接口的类型,T 表示一个具体的类型(也可为接口类型)
该断言表达式会返回 x 的值(也就是 value)和一个布尔值(也就是 ok),可根据该布尔值判断 x 是否为 T 类型:
如果 T 是具体某个类型,类型断言会检查 x 的动态类型是否等于具体类型 T。如果检查成功,类型断言返回的结果是 x 的动态值,其类型是 T。
如果 T 是接口类型,类型断言会检查 x 的动态类型是否满足 T。如果检查成功,x 的动态值不会被提取,返回值是一个类型为 T 的接口值。
无论 T 是什么类型ÿ