【问题标题】:gRPC Unary Stream from Python server -> C# Client: "Stream removed"来自 Python 服务器的 gRPC 一元流 -> C# 客户端:“流已删除”
【发布时间】:2018-01-26 06:44:05
【问题描述】:

我有一个从 Python 服务器 (1.8.4) 到 C# 客户端 (1.8.3) 的一元流服务。

当我向 Python 发出关闭 (TERM15) 的信号时,将调用下面代码清单中的 shutdown 方法,其目的是优雅地终止 RPC,并关闭服务器。

这在我在 localhost 上运行服务器时有效,会引发具有预期状态的 RpcException:Status(StatusCode=Cancelled, Detail="Completed")

但是,当在 Kubernetes(GKE) 中运行服务器并终止我收到的 pod 时:客户端中的Grpc.Core.RpcException: Status(StatusCode=Unknown, Detail="Stream removed")。 python 服务器位于 haproxy 入口控制器和 google-cloud-endpoint 代理后面,但据我了解,这些组件都不应该影响连接。

谁能想到是什么导致了客户端 RpcException 的不同状态?

class StreamsService(StreamsServicer):
    def MyHandler(self, request, context):
        while not stop_event.isSet():
            try:
                yield update_q.get(True, 0.1)
            except queue.Empty:
                continue

        context.set_code(grpc.StatusCode.CANCELLED)
        context.set_details("Completed")

def shutdown(subscriber_service: StreamsService,
             executor: futures.ThreadPoolExecutor,
             server: grpc.Server, exit_code):    

    logger.info("Stopping stream handlers")
    for stop_event in subscriber_service.stop_events:
        stop_event.set()

    logger.info("Stopping executor")
    executor.shutdown()

    logger.info("Stopping server")
    ev: threading.Event = server.stop(grace=10)  # allows RPCs to terminate gracefully

    logger.info("Waiting for server to stop gracefully")
    ev.wait()

    logger.info("Stopping process with exit code {}".format(exit_code))
    sys.exit(exit_code)

(ps.从https://groups.google.com/forum/#!topic/grpc-io/Ge6hcUUhXzo交叉发布)

【问题讨论】:

    标签: python grpc


    【解决方案1】:

    原来这一个问题,即 google-cloud-endpoint/NGINX 容器在同一 pod 中的 gRPC 服务器容器有机会正常关闭其连接之前被终止。

    如果有人有兴趣了解更多信息,我在这里与自己进行了一次对话:https://groups.google.com/forum/#!topic/google-cloud-endpoints/FyfdvD6xS1Q

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多