1# Copyright 2023 gRPC authors. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""The Python implementation of the GRPC helloworld.Greeter server with health checking.""" 15 16from concurrent import futures 17import logging 18import threading 19from time import sleep 20 21import grpc 22from grpc_health.v1 import health 23from grpc_health.v1 import health_pb2 24from grpc_health.v1 import health_pb2_grpc 25import helloworld_pb2 26import helloworld_pb2_grpc 27 28 29class Greeter(helloworld_pb2_grpc.GreeterServicer): 30 def SayHello(self, request, context): 31 return helloworld_pb2.HelloReply(message=request.name) 32 33 34def _toggle_health(health_servicer: health.HealthServicer, service: str): 35 next_status = health_pb2.HealthCheckResponse.SERVING 36 while True: 37 if next_status == health_pb2.HealthCheckResponse.SERVING: 38 next_status = health_pb2.HealthCheckResponse.NOT_SERVING 39 else: 40 next_status = health_pb2.HealthCheckResponse.SERVING 41 42 health_servicer.set(service, next_status) 43 sleep(5) 44 45 46def _configure_health_server(server: grpc.Server): 47 health_servicer = health.HealthServicer( 48 experimental_non_blocking=True, 49 experimental_thread_pool=futures.ThreadPoolExecutor(max_workers=10), 50 ) 51 health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server) 52 53 # Use a daemon thread to toggle health status 54 toggle_health_status_thread = threading.Thread( 55 target=_toggle_health, 56 args=(health_servicer, "helloworld.Greeter"), 57 daemon=True, 58 ) 59 toggle_health_status_thread.start() 60 61 62def serve(): 63 port = "50051" 64 server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) 65 helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) 66 server.add_insecure_port("[::]:" + port) 67 _configure_health_server(server) 68 server.start() 69 print("Server started, listening on " + port) 70 server.wait_for_termination() 71 72 73if __name__ == "__main__": 74 logging.basicConfig() 75 serve() 76