go network_error ai_generated true

rpc 错误:code = DeadlineExceeded desc = 上下文超时

rpc error: code = DeadlineExceeded desc = context deadline exceeded

ID: go/grpc-deadline-exceeded-server-slow

其他格式: JSON · Markdown 中文 · English
80%修复率
89%置信度
0证据数
2024-01-09首次发现

版本兼容性

版本状态引入弃用备注
google.golang.org/grpc v1.55+ active — — —

根因分析

客户端上下文截止时间在服务器响应之前耗尽。通常是因为服务器处理程序较慢(数据库查询、下游调用)或截止时间设置过于激进。

English

The client-side context deadline elapsed before the server responded. Usually the server handler is slow (DB query, downstream call) or the deadline is too aggressive.

generic

解决方案

  1. 90% 成功率
    func (s *server) Get(ctx context.Context, req *pb.GetReq) (*pb.GetResp, error) {
        ctx, span := trace.StartSpan(ctx, "Get")
        defer span.End()
        if dl, ok := ctx.Deadline(); ok {
            log.Printf("deadline in %v", time.Until(dl))
        }
        return s.db.Query(ctx, req.Id) // pass ctx through
    }
  2. 85% 成功率
    b := backoff.NewExponentialBackOff()
    b.MaxElapsedTime = 5 * time.Second
    b.InitialInterval = 100 * time.Millisecond
    return backoff.Retry(func() error {
        _, err := client.Get(ctx, req)
        return err
    }, backoff.WithContext(b, ctx))

无效尝试

常见但无效的做法:

  1. 80% 失败

    Removes the timeout but leaves slow handlers blocking goroutines indefinitely; cascading failures.

  2. 75% 失败

    Synchronized retries amplify load on an already-overloaded server (thundering herd).