0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

gRPCのint32 <-> int64の互換性について

Posted at

以下ドキュメントにも記載のあるとおり、gRPCにおいて int32int64 は相互互換性があり、呼び出し元/先でデータ型が変わっていても通信が可能です

ただ、int32の範囲をオーバーする値が設定された時に以下の変換規則が適用されるので注意

  • クライアントが int32 & サーバーが int64 の場合
    • リクエストで int32 をオーバーする値を設定した場合、 クライアント側でエラーが発生 します
    • レスポンスに int32 をオーバーする値が設定された場合、 クライアント側にはオーバーフローした値が設定 されます
      • 例えばint32の上限値 + 1の値である 2147483648 であれば、 -2147483648 としてクライアントにて読み取られます
  • クライアントが int64 & サーバーが int32 の場合
    • リクエストで int32 をオーバーする値を設定した場合、 サーバ側にはオーバーフローした値が設定 されます
    • レスポンスに int32 をオーバーする値が設定された場合、 サーバー側でエラーが発生 します

ただ、これはPython実装の場合における挙動で、例えばgRPCクライアントツールとして有名なevans だと、上記において値がオーバーフローするケースにおいてはエラー応答が返却されます

エラーのイメージ
$ evans --host localhost -p 14880 example.proto

  ______
 |  ____|
 | |__    __   __   __ _   _ __    ___
 |  __|   \ \ / /  / _. | | '_ \  / __|
 | |____   \ V /  | (_| | | | | | \__ \
 |______|   \_/    \__,_| |_| |_| |___/

 more expressive universal gRPC client


example.Example@localhost:14880> call getExample
id (TYPE_INT32) => 1
command call: rpc error: code = Internal desc = grpc: failed to unmarshal the received message proto: integer overflow

仕様はあくまで参考値に留め、実際の実装の挙動を確認するのがよさそうです

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?