go network_error ai_generated true

rpc error: code = DeadlineExceeded desc = context deadline exceeded

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
89%Confidence
0Evidence
2024-01-09First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
google.golang.org/grpc v1.55+ active — — —

Root Cause

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

中文

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

Workarounds

  1. 90% success
    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% success
    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))

Dead Ends

Common approaches that don't work:

  1. 80% fail

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

  2. 75% fail

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