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 keepAlive options.""" 15 16from concurrent import futures 17import logging 18from time import sleep 19 20import grpc 21import helloworld_pb2 22import helloworld_pb2_grpc 23 24 25class Greeter(helloworld_pb2_grpc.GreeterServicer): 26 def SayHello(self, request, context): 27 message = request.name 28 if message.startswith("[delay]"): 29 sleep(5) 30 return helloworld_pb2.HelloReply(message=message) 31 32 33def serve(): 34 """ 35 grpc.keepalive_time_ms: The period (in milliseconds) after which a keepalive ping is 36 sent on the transport. 37 grpc.keepalive_timeout_ms: The amount of time (in milliseconds) the sender of the keepalive 38 ping waits for an acknowledgement. If it does not receive an acknowledgement within 39 this time, it will close the connection. 40 grpc.http2.min_ping_interval_without_data_ms: Minimum allowed time (in milliseconds) 41 between a server receiving successive ping frames without sending any data/header frame. 42 grpc.max_connection_idle_ms: Maximum time (in milliseconds) that a channel may have no 43 outstanding rpcs, after which the server will close the connection. 44 grpc.max_connection_age_ms: Maximum time (in milliseconds) that a channel may exist. 45 grpc.max_connection_age_grace_ms: Grace period (in milliseconds) after the channel 46 reaches its max age. 47 grpc.http2.max_pings_without_data: How many pings can the client send before needing to 48 send a data/header frame. 49 grpc.keepalive_permit_without_calls: If set to 1 (0 : false; 1 : true), allows keepalive 50 pings to be sent even if there are no calls in flight. 51 For more details, check: https://github.com/grpc/grpc/blob/master/doc/keepalive.md 52 """ 53 server_options = [ 54 ("grpc.keepalive_time_ms", 20000), 55 ("grpc.keepalive_timeout_ms", 10000), 56 ("grpc.http2.min_ping_interval_without_data_ms", 5000), 57 ("grpc.max_connection_idle_ms", 10000), 58 ("grpc.max_connection_age_ms", 30000), 59 ("grpc.max_connection_age_grace_ms", 5000), 60 ("grpc.http2.max_pings_without_data", 5), 61 ("grpc.keepalive_permit_without_calls", 1), 62 ] 63 port = "50051" 64 server = grpc.server( 65 thread_pool=futures.ThreadPoolExecutor(max_workers=10), 66 options=server_options, 67 ) 68 helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) 69 server.add_insecure_port("[::]:" + port) 70 server.start() 71 print("Server started, listening on " + port) 72 server.wait_for_termination() 73 74 75if __name__ == "__main__": 76 logging.basicConfig() 77 serve() 78