MeWrite Docs

gRPC: DEADLINE_EXCEEDED

gRPCタイムアウトエラーの解決方法

概要

gRPC呼び出しが指定されたデッドラインを超えた場合に発生するエラーです。

エラーメッセージ

rpc error: code = DeadlineExceeded desc = context deadline exceeded

原因

  1. 処理時間超過: サーバー側の処理が遅い
  2. デッドライン設定が短い: タイムアウト値が不適切
  3. ネットワーク遅延: 通信に時間がかかる
  4. サーバー過負荷: リソース不足

解決策

1. デッドラインを設定(Go)

1
2
3
4
5
6
7
8
9
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

resp, err := client.SomeMethod(ctx, &pb.Request{})
if err != nil {
    if status.Code(err) == codes.DeadlineExceeded {
        log.Println("Timeout exceeded")
    }
}

2. Python設定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import grpc

channel = grpc.insecure_channel('localhost:50051')
stub = service_pb2_grpc.MyServiceStub(channel)

try:
    response = stub.SomeMethod(request, timeout=30)
except grpc.RpcError as e:
    if e.code() == grpc.StatusCode.DEADLINE_EXCEEDED:
        print("Timeout exceeded")

3. リトライ設定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
retryPolicy := `{
    "methodConfig": [{
        "name": [{"service": "myservice.MyService"}],
        "retryPolicy": {
            "maxAttempts": 3,
            "initialBackoff": "0.1s",
            "maxBackoff": "1s",
            "backoffMultiplier": 2
        }
    }]
}`

よくある間違い

  • デフォルトのタイムアウトに依存
  • サーバー側でのデッドラインチェック忘れ

gRPC の他のエラー

最終更新: 2025-12-09