debug_server.py
Go to the documentation of this file.
1 # Copyright 2019 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 """The Python example of utilizing Channelz feature."""
15 
16 from __future__ import absolute_import
17 from __future__ import division
18 from __future__ import print_function
19 
20 import argparse
21 from concurrent import futures
22 import logging
23 import random
24 
25 import grpc
26 
27 helloworld_pb2, helloworld_pb2_grpc = grpc.protos_and_services(
28  "helloworld.proto")
29 
30 # TODO: Suppress until the macOS segfault fix rolled out
31 from grpc_channelz.v1 import channelz # pylint: disable=wrong-import-position
32 
33 _LOGGER = logging.getLogger(__name__)
34 _LOGGER.setLevel(logging.INFO)
35 
36 _RANDOM_FAILURE_RATE = 0.3
37 
38 
40 
41  def __init__(self, failure_rate):
42  self._failure_rate = failure_rate
43 
44  def SayHello(self, request, context):
45  if random.random() < self._failure_rate:
46  context.abort(grpc.StatusCode.UNAVAILABLE,
47  'Randomly injected failure.')
48  return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)
49 
50 
51 def create_server(addr, failure_rate):
52  server = grpc.server(futures.ThreadPoolExecutor())
54  FaultInjectGreeter(failure_rate), server)
55 
56  # Add Channelz Servicer to the gRPC server
57  channelz.add_channelz_servicer(server)
58 
59  server.add_insecure_port(addr)
60  return server
61 
62 
63 def main():
64  parser = argparse.ArgumentParser()
65  parser.add_argument('--addr',
66  nargs=1,
67  type=str,
68  default='[::]:50051',
69  help='the address to listen on')
70  parser.add_argument(
71  '--failure_rate',
72  nargs=1,
73  type=float,
74  default=0.3,
75  help='a float indicates the percentage of failed message injections')
76  args = parser.parse_args()
77 
78  server = create_server(addr=args.addr, failure_rate=args.failure_rate)
79  server.start()
80  server.wait_for_termination()
81 
82 
83 if __name__ == '__main__':
84  logging.basicConfig(level=logging.INFO)
85  main()
debug_server.FaultInjectGreeter._failure_rate
_failure_rate
Definition: debug_server.py:42
debug_server.main
def main()
Definition: debug_server.py:63
helloworld_pb2.HelloReply
HelloReply
Definition: helloworld/helloworld_pb2.py:100
grpc.server
def server(thread_pool, handlers=None, interceptors=None, options=None, maximum_concurrent_rpcs=None, compression=None, xds=False)
Definition: src/python/grpcio/grpc/__init__.py:2034
debug_server.FaultInjectGreeter.SayHello
def SayHello(self, request, context)
Definition: debug_server.py:44
debug_server.FaultInjectGreeter
Definition: debug_server.py:39
grpc_channelz.v1
Definition: src/python/grpcio_channelz/grpc_channelz/v1/__init__.py:1
helloworld_pb2_grpc.GreeterServicer
Definition: helloworld/helloworld_pb2_grpc.py:24
main
Definition: main.py:1
debug_server.FaultInjectGreeter.__init__
def __init__(self, failure_rate)
Definition: debug_server.py:41
debug_server.create_server
def create_server(addr, failure_rate)
Definition: debug_server.py:51
helloworld_pb2_grpc.add_GreeterServicer_to_server
def add_GreeterServicer_to_server(servicer, server)
Definition: helloworld/helloworld_pb2_grpc.py:36


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:02