经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Go语言 » 查看文章
Go 语言 context 都能做什么?
来源:cnblogs  作者:yongxinz  时间:2023/7/3 9:18:20  对本文有异议

原文链接: Go 语言 context 都能做什么?

很多 Go 项目的源码,在读的过程中会发现一个很常见的参数 ctx,而且基本都是作为函数的第一个参数。

为什么要这么写呢?这个参数到底有什么用呢?带着这样的疑问,我研究了这个参数背后的故事。

开局一张图:

核心是 Context 接口:

  1. // A Context carries a deadline, cancelation signal, and request-scoped values
  2. // across API boundaries. Its methods are safe for simultaneous use by multiple
  3. // goroutines.
  4. type Context interface {
  5. // Done returns a channel that is closed when this Context is canceled
  6. // or times out.
  7. Done() <-chan struct{}
  8. // Err indicates why this context was canceled, after the Done channel
  9. // is closed.
  10. Err() error
  11. // Deadline returns the time when this Context will be canceled, if any.
  12. Deadline() (deadline time.Time, ok bool)
  13. // Value returns the value associated with key or nil if none.
  14. Value(key interface{}) interface{}
  15. }

包含四个方法:

  • Done():返回一个 channel,当 times out 或者调用 cancel 方法时。
  • Err():返回一个错误,表示取消 ctx 的原因。
  • Deadline():返回截止时间和一个 bool 值。
  • Value():返回 key 对应的值。

有四个结构体实现了这个接口,分别是:emptyCtx, cancelCtx, timerCtxvalueCtx

其中 emptyCtx 是空类型,暴露了两个方法:

  1. func Background() Context
  2. func TODO() Context

一般情况下,会使用 Background() 作为根 ctx,然后在其基础上再派生出子 ctx。要是不确定使用哪个 ctx,就使用 TODO()

另外三个也分别暴露了对应的方法:

  1. func WithCancel(parent Context) (ctx Context, cancel CancelFunc)
  2. func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc)
  3. func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)
  4. func WithValue(parent Context, key, val interface{}) Context

遵循规则

在使用 Context 时,要遵循以下四点规则:

  1. 不要将 Context 放入结构体,而是应该作为第一个参数传入,命名为 ctx
  2. 即使函数允许,也不要传入 nil 的 Context。如果不知道用哪种 Context,可以使用 context.TODO()
  3. 使用 Context 的 Value 相关方法只应该用于在程序和接口中传递和请求相关的元数据,不要用它来传递一些可选的参数。
  4. 相同的 Context 可以传递给不同的 goroutine;Context 是并发安全的。

WithCancel

  1. func WithCancel(parent Context) (ctx Context, cancel CancelFunc)

WithCancel 返回带有新 Done 通道的父级副本。当调用返回的 cancel 函数或关闭父上下文的 Done 通道时,返回的 ctxDone 通道将关闭。

取消此上下文会释放与其关联的资源,因此在此上下文中运行的操作完成后,代码应立即调用 cancel

举个例子:

这段代码演示了如何使用可取消上下文来防止 goroutine 泄漏。在函数结束时,由 gen 启动的 goroutine 将返回而不会泄漏。

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. )
  6. func main() {
  7. // gen generates integers in a separate goroutine and
  8. // sends them to the returned channel.
  9. // The callers of gen need to cancel the context once
  10. // they are done consuming generated integers not to leak
  11. // the internal goroutine started by gen.
  12. gen := func(ctx context.Context) <-chan int {
  13. dst := make(chan int)
  14. n := 1
  15. go func() {
  16. for {
  17. select {
  18. case <-ctx.Done():
  19. return // returning not to leak the goroutine
  20. case dst <- n:
  21. n++
  22. }
  23. }
  24. }()
  25. return dst
  26. }
  27. ctx, cancel := context.WithCancel(context.Background())
  28. defer cancel() // cancel when we are finished consuming integers
  29. for n := range gen(ctx) {
  30. fmt.Println(n)
  31. if n == 5 {
  32. break
  33. }
  34. }
  35. }

输出:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5

WithDeadline

  1. func WithDeadline(parent Context, d time.Time) (Context, CancelFunc)

WithDeadline 返回父上下文的副本,并将截止日期调整为不晚于 d。如果父级的截止日期已经早于 d,则 WithDeadline(parent, d) 在语义上等同于 parent

当截止时间到期、调用返回的取消函数时或当父上下文的 Done 通道关闭时,返回的上下文的 Done 通道将关闭。

取消此上下文会释放与其关联的资源,因此在此上下文中运行的操作完成后,代码应立即调用取消。

举个例子:

这段代码传递具有截止时间的上下文,来告诉阻塞函数,它应该在到达截止时间时立刻退出。

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. )
  7. const shortDuration = 1 * time.Millisecond
  8. func main() {
  9. d := time.Now().Add(shortDuration)
  10. ctx, cancel := context.WithDeadline(context.Background(), d)
  11. // Even though ctx will be expired, it is good practice to call its
  12. // cancellation function in any case. Failure to do so may keep the
  13. // context and its parent alive longer than necessary.
  14. defer cancel()
  15. select {
  16. case <-time.After(1 * time.Second):
  17. fmt.Println("overslept")
  18. case <-ctx.Done():
  19. fmt.Println(ctx.Err())
  20. }
  21. }

输出:

  1. context deadline exceeded

WithTimeout

  1. func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)

WithTimeout 返回 WithDeadline(parent, time.Now().Add(timeout))

取消此上下文会释放与其关联的资源,因此在此上下文中运行的操作完成后,代码应立即调用取消。

举个例子:

这段代码传递带有超时的上下文,以告诉阻塞函数应在超时后退出。

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. )
  7. const shortDuration = 1 * time.Millisecond
  8. func main() {
  9. // Pass a context with a timeout to tell a blocking function that it
  10. // should abandon its work after the timeout elapses.
  11. ctx, cancel := context.WithTimeout(context.Background(), shortDuration)
  12. defer cancel()
  13. select {
  14. case <-time.After(1 * time.Second):
  15. fmt.Println("overslept")
  16. case <-ctx.Done():
  17. fmt.Println(ctx.Err()) // prints "context deadline exceeded"
  18. }
  19. }

输出:

  1. context deadline exceeded

WithValue

  1. func WithValue(parent Context, key, val any) Context

WithValue 返回父级的副本,其中与 key 关联的值为 val

其中键必须是可比较的,并且不应是字符串类型或任何其他内置类型,以避免使用上下文的包之间发生冲突。 WithValue 的用户应该定义自己的键类型。

为了避免分配给 interface{},上下文键通常具有具体的 struct{} 类型。或者,导出的上下文键变量的静态类型应该是指针或接口。

举个例子:

这段代码演示了如何将值传递到上下文以及如何检索它(如果存在)。

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. )
  6. func main() {
  7. type favContextKey string
  8. f := func(ctx context.Context, k favContextKey) {
  9. if v := ctx.Value(k); v != nil {
  10. fmt.Println("found value:", v)
  11. return
  12. }
  13. fmt.Println("key not found:", k)
  14. }
  15. k := favContextKey("language")
  16. ctx := context.WithValue(context.Background(), k, "Go")
  17. f(ctx, k)
  18. f(ctx, favContextKey("color"))
  19. }

输出:

  1. found value: Go
  2. key not found: color

本文的大部分内容,包括代码示例都是翻译自官方文档,代码都是经过验证可以执行的。如果有不是特别清晰的地方,可以直接去读官方文档。

以上就是本文的全部内容,如果觉得还不错的话欢迎点赞转发关注,感谢支持。


官方文档:

源码分析:

推荐阅读:

原文链接:https://www.cnblogs.com/alwaysbeta/p/17520650.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号