1# Copyright 2021 The 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"""A flaky backend for the gRPC Python retry example.""" 15 16import asyncio 17import collections 18import logging 19import random 20 21import grpc 22 23helloworld_pb2, helloworld_pb2_grpc = grpc.protos_and_services( 24 "helloworld.proto" 25) 26 27 28class ErrorInjectingGreeter(helloworld_pb2_grpc.GreeterServicer): 29 def __init__(self): 30 self._counter = collections.defaultdict(int) 31 32 async def SayHello( 33 self, 34 request: helloworld_pb2.HelloRequest, 35 context: grpc.aio.ServicerContext, 36 ) -> helloworld_pb2.HelloReply: 37 self._counter[context.peer()] += 1 38 if self._counter[context.peer()] < 5: 39 if random.random() < 0.75: 40 logging.info("Injecting error to RPC from %s", context.peer()) 41 await context.abort( 42 grpc.StatusCode.UNAVAILABLE, "injected error" 43 ) 44 logging.info("Successfully responding to RPC from %s", context.peer()) 45 return helloworld_pb2.HelloReply(message="Hello, %s!" % request.name) 46 47 48async def serve() -> None: 49 server = grpc.aio.server() 50 helloworld_pb2_grpc.add_GreeterServicer_to_server( 51 ErrorInjectingGreeter(), server 52 ) 53 listen_addr = "[::]:50051" 54 server.add_insecure_port(listen_addr) 55 logging.info("Starting flaky server on %s", listen_addr) 56 await server.start() 57 await server.wait_for_termination() 58 59 60if __name__ == "__main__": 61 logging.basicConfig(level=logging.INFO) 62 asyncio.run(serve()) 63