我正在用 python 在 grpc-gateway 后面编写一个 grpc 服务,如果某些用户请求太多,我想发出 429 响应并在响应消息正文中提供验证码 token 。
实际上我的问题是当我使用下面的代码块引发状态代码 429 时,之后我无法发送响应消息。
context.set_code(grpc.StatusCode.RESOURCE_EXHAUSTED)
context.set_details('Too many requests')
return MyServiceResponse()
据我所知,only-grpc 是不可能的,但我认为第三方可能有可能。
有什么解决办法吗?
请您参考如下方法:
一元-一元 RPC(双方均非流式传输)不允许发送状态为非正常的响应。对于流式 RPC,服务器可能会在发送错误代码之前发送响应,但不推荐这样做。将正常响应与错误状态混合可能会导致 future 的可维护性问题,例如如果同一个错误应用于多个 RPC,所有响应 ProtoBuf 消息都应该包含这些字段吗?
回到您的问题,“验证码 token ”应被视为错误状态的一部分,因此可以将其添加为尾随元数据之一。在您的情况下,您可以通过在尾随元数据键中添加 -bin 后缀来将序列化的原型(prototype)消息添加为二进制尾随元数据。
此外,还有一个官方支持包 grpcio-status 可以为您完成此操作。
服务器端将丰富的错误状态打包到“grpc_status.status_pb2.Status”原型(prototype)消息中。下面的示例仅使用了常见的错误原型(prototype),但您可以将“任何”原型(prototype)打包到 details 中,只要您的客户理解它们即可。
# Server side
from grpc_status import rpc_status
from google.protobuf import any_pb2
def ...Servicer(...):
def AnRPCCall(request, context):
...
detail = any_pb2.Any()
detail.Pack(
rpc_status.error_details_pb2.DebugInfo(
stack_entries=traceback.format_stack(),
detail="Can't recognize this argument",
)
)
rich_status = grpc_status.status_pb2.Status(
code=grpc_status.code_pb2.INVALID_ARGUMENT,
message='Invalid argument',
details=[detail]
)
context.abort_with_status(rpc_status.to_status(rich_status))
# The method handler will abort
客户端解码错误,并对它们使用react。
# Client side
try:
self._channel.unary_unary(_ERROR_DETAILS).with_call(_REQUEST)
except grpc.RpcError as rpc_error:
status = rpc_status.from_call(rpc_error)
for detail in status.details:
if detail.Is(error_details_pb2.DebugInfo.DESCRIPTOR):
info = error_details_pb2.DebugInfo()
detail.Unpack(info)
# Handle the debug info
elif detail.Is(OtherErrorProto.DESCRIPTOR):
# Handle the other error proto
else:
# Handle unknown error
了解更多关于富人的信息:https://github.com/grpc/proposal/blob/master/L44-python-rich-status.md
