【发布时间】:2017-10-06 14:34:23
【问题描述】:
我正在使用 Github 的 grpc-gateway 项目从 gRPC 规范自动生成 REST API。在这个 REST API 中,我想支持 ETag 标头和 304 Not Modified 响应。
据我了解,通常您会在 gRPC 服务器中创建一个带有特定状态代码的响应,然后该状态代码将被 grpc-gateway 转换为 HTTP 状态代码。但是,由于标准 gRPC 并不真正支持缓存概念,因此没有映射到 HTTP 304 状态代码的 gRPC 状态代码。
使用 grpc-gateway,似乎可以在 gRPC 状态码为错误码时自定义 HTTP 状态码(覆盖 runtime.HTTPError 函数)。但是,我还没有找到任何方法可以在 gRPC 响应代码正常的情况下自定义 HTTP 响应代码。
那么,有没有推荐的方法来实现这一点?
【问题讨论】:
-
我也对此感兴趣。以下是我通过 github repo 发现的一些相关问题:github.com/grpc-ecosystem/grpc-gateway/issues/240 & github.com/grpc-ecosystem/grpc-gateway/issues/517
-
相关google群组讨论:groups.google.com/forum/#!topic/grpc-io/1TJspS7QRwUquote
I would say that this [caching] isn't really something we would like to introduce in the core of grpc. It is indeed left as an exercise to the reader in the sense that it is rather something that you would implement in your messages. Not all kinds of rpc would find that feature relevant, so let's not make all of them pay its cost on the critical path. Authentication on the other hand is on the channel itself, and the presence of the code won't impact non-authenticated channels.