【发布时间】: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交叉发布)
【问题讨论】: