跳过正文
  1. 文章/
  2. GoLang/
  3. 常用包/
  4. 标准包/

10、context

·2632 字·6 分钟· loading · loading · ·
GoLang 常用包 标准包
GradyYoung
作者
GradyYoung
标准包 - 点击查看当前系列文章
§ 10、context 「 当前文章 」

Context
#

context(上下文)包设计的核心目标是为了在不同goroutine之间传递截止时间、取消信号以及请求范围的值,尤其适用于处理请求的场景(如HTTP请求)。Context主要解决了以下问题:

  1. 取消控制:如何通知多个goroutine取消当前任务
  2. 超时控制:如何为操作设置截止时间或超时时间
  3. 值传递:如何安全地在请求范围内传递数据
  4. 层次化:如何构建可继承的上下文关系

Context接口设计
#

type Context interface {
    // 返回context的截止时间,如果没有设置截止时间,ok返回false
    Deadline() (deadline time.Time, ok bool)
    
    // 返回一个Channel,当context被取消时,该Channel会被关闭
    Done() <-chan struct{}
    
    // 如果Done()返回的Channel未关闭,返回nil
    // 如果Done()返回的Channel已关闭,返回context取消的原因
    Err() error
    
    // 从context中获取与key关联的值,如果没有则返回nil
    Value(key interface{}) interface{}
}

Context树结构
#

Context实例可以组成一个树状结构,当父Context取消时,所有从其派生的子Context都会被取消:

emptyCtx
    |
    +--- withCancel ------ withCancel
    |                         |
    +--- withDeadline         +--- withValue
            |
            +--- withTimeout
                    |
                    +--- withValue

使用Context取消操作
#

使用context.WithCancel可以创建一个可取消的Context:

package main

import (
	"context"
	"fmt"
	"time"
)

func main() {
	// 创建一个可取消的 Context
	ctx, cancel := context.WithCancel(context.Background())
	go dosomething(ctx)
	time.Sleep(5 * time.Second)
	// 取消 context
	cancel()
  // 等待观察结果
	time.Sleep(5 * time.Second)
}

func dosomething(ctx context.Context) {
	for {
		// 监听 context
		select {
		// context 被 Cancel
		case <-ctx.Done():
			fmt.Println("Cancel")
			return
		// 正常执行业务
		default:
			time.Sleep(1 * time.Second)
			fmt.Println(time.Now())
		}
	}
}

取消传播机制
#

当一个父Context被取消时,从它派生的所有子Context也会被取消。这种传播机制非常适合处理复杂的请求场景:

func main() {
    // 创建根Context
    rootCtx, rootCancel := context.WithCancel(context.Background())
    defer rootCancel()
    
    // 创建子Context
    childCtx, childCancel := context.WithCancel(rootCtx)
    defer childCancel()
    
    // 启动工作goroutine
    go doSomething(childCtx)
    
    // 5秒后取消根Context
    time.Sleep(5 * time.Second)
    fmt.Println("Cancelling root context...")
    rootCancel()
    
    // 等待观察结果
    time.Sleep(1 * time.Second)
}

rootCancel()被调用时,childCtx也会被取消,即使childCancel()没有被调用。

示例:并发下载器
#

这个示例展示了如何使用Context控制多个下载任务,当任一下载失败或Context被取消时,所有下载都会停止。

func downloadFiles(ctx context.Context, urls []string) error {
    // 创建一个错误通道,用于收集下载过程中的错误
    errCh := make(chan error, len(urls))
    
    // 创建一个WaitGroup,用于等待所有下载任务完成
    var wg sync.WaitGroup
    
    // 为每个URL启动一个下载goroutine
    for _, url := range urls {
        wg.Add(1)
        go func(url string) {
            defer wg.Done()
            
            // 检查Context是否已取消
            select {
            case <-ctx.Done():
                errCh <- fmt.Errorf("download of %s canceled: %v", url, ctx.Err())
                return
            default:
                // 继续下载
            }
            
            // 模拟下载操作
            err := downloadFile(ctx, url)
            if err != nil {
                errCh <- err
            }
        }(url)
    }
    
    // 创建一个goroutine等待所有下载完成并关闭错误通道
    go func() {
        wg.Wait()
        close(errCh)
    }()
    
    // 收集第一个错误
    for err := range errCh {
        return err // 返回第一个遇到的错误
    }
    
    return nil
}

func downloadFile(ctx context.Context, url string) error {
    // 创建HTTP请求
    req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
    if err != nil {
        return err
    }
    
    // 执行请求
    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        return err
    }
    defer resp.Body.Close()
    
    // 处理响应...
    return nil
}

超时控制
#

截止时间控制
#

使用context.WithDeadline可以创建一个具有截止时间的Context:

// 创建一个10秒后到期的Context
deadline := time.Now().Add(10 * time.Second)
ctx, cancel := context.WithDeadline(context.Background(), deadline)

// 确保在函数退出时取消Context以释放资源
defer cancel()

// 使用具有截止时间的Context
go doSomethingWithDeadline(ctx)

超时控制
#

context.WithTimeoutWithDeadline的便捷包装,用于设置相对超时时间:

// 创建一个5秒后超时的Context
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

// 使用具有超时的Context
go doSomethingWithTimeout(ctx)

示例:使用超时控制HTTP请求
#

func fetchURL(url string) ([]byte, error) {
    // 创建一个30秒超时的Context
    ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    defer cancel()
    
    // 创建一个带有Context的请求
    req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
    if err != nil {
        return nil, err
    }
    
    // 执行请求
    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()
    
    // 读取响应体
    return io.ReadAll(resp.Body)
}

优雅处理超时
#

当Context超时时,可以通过Err()方法区分不同的错误类型:

func processWithTimeout(ctx context.Context) error {
    select {
    case <-ctx.Done():
        err := ctx.Err()
        if err == context.DeadlineExceeded {
            return fmt.Errorf("operation timed out: %v", err)
        }
        if err == context.Canceled {
            return fmt.Errorf("operation canceled: %v", err)
        }
        return err
    case result := <-doWork():
        return processResult(result)
    }
}

使用Context传递值
#

创建带值的Context
#

使用context.WithValue可以创建一个包含键值对的Context:

// 创建一个包含请求ID的Context
ctx := context.WithValue(context.Background(), "request_id", "12345")

// 使用包含值的Context
go processRequest(ctx)

获取Context中的值
#

在goroutine中,可以通过Context的Value()方法获取传递的值:

func processRequest(ctx context.Context) {
    // 获取请求ID
    requestID, ok := ctx.Value("request_id").(string)
    if !ok {
        requestID = "unknown"
    }
    
    fmt.Printf("Processing request %s\n", requestID)
    // 处理请求...
}

使用自定义类型避免键冲突
#

为了避免不同包之间的键名冲突,建议使用自定义类型作为Context的键:

// 定义一个私有的类型作为键
type contextKey string

// 定义常量作为具体的键
const (
    requestIDKey contextKey = "request_id"
    userIDKey    contextKey = "user_id"
)

// 使用自定义类型作为键创建Context
ctx := context.WithValue(context.Background(), requestIDKey, "12345")
ctx = context.WithValue(ctx, userIDKey, "user-567")

// 获取值时使用相同的键类型
func getRequestID(ctx context.Context) string {
    requestID, ok := ctx.Value(requestIDKey).(string)
    if !ok {
        return "unknown"
    }
    return requestID
}

值传递的最佳实践
#

Context中的值传递主要用于传递请求范围的数据,如请求ID、认证令牌等。不应该将它用于传递可选参数或功能控制。一些最佳实践包括:

  • 只存储请求范围内的数据,不要存储全局状态
  • 值应该是不可变的,避免并发修改
  • 对于简单类型,使用强类型键避免类型断言错误
  • 不要滥用Context存储大量数据,这会降低代码可读性和可维护性

Context使用的最佳实践与陷阱
#

Context传递规范
#

按照Go的惯例,Context应该是函数的第一个参数

// 正确的Context参数位置
func DoSomething(ctx context.Context, arg Arg) error {
    // ...
}

// 不推荐的做法
func DoSomething(arg Arg, ctx context.Context) error {
    // ...
}

避免将Context存储在结构体中
#

Context应该通过函数参数显式传递,而不是存储在结构体中

// 不推荐的做法
type Service struct {
    ctx context.Context
    // ...
}

// 推荐的做法
type Service struct {
    // 没有存储Context
}

func (s *Service) DoWork(ctx context.Context) error {
    // 通过参数使用Context
}

不要传递nil Context
#

如果不确定使用哪个Context,可以使用context.Background()context.TODO()

// 不推荐的做法
func DoSomething(ctx context.Context) {
    if ctx == nil {
        ctx = context.Background()
    }
    // ...
}

// 推荐的做法
func DoSomething(ctx context.Context) {
    // 调用者负责提供有效的Context
    // ...
}

正确管理取消函数
#

每次调用WithCancelWithTimeoutWithDeadline都会返回一个cancel函数,应该在不再需要Context时调用这个函数

// 推荐的做法
func main() {
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel() // 确保在函数退出时调用cancel
    
    // 使用ctx...
}

避免在Context中存储过多数据
#

Context主要用于传递请求范围的元数据,不应该用于传递大量数据或业务逻辑参数

// 不推荐的做法
ctx := context.WithValue(ctx, "database", db)
ctx = context.WithValue(ctx, "config", config)
ctx = context.WithValue(ctx, "user_data", hugeUserDataStruct)

// 推荐的做法 - 只存储请求范围的元数据
ctx := context.WithValue(ctx, requestIDKey, requestID)
ctx = context.WithValue(ctx, userIDKey, userID)
标准包 - 点击查看当前系列文章
§ 10、context 「 当前文章 」