• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2024 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"""Example gRPC server that applies back-pressure on client"""
15
16from concurrent import futures
17import logging
18import time
19
20import grpc
21import helloworld_pb2
22import helloworld_pb2_grpc
23import helpers
24
25_PORT = "50051"
26
27
28class Greeter(helloworld_pb2_grpc.GreeterServicer):
29    def SayHelloBidiStream(self, request_iterator, unused_context):
30        # Delay read on server side to apply back-pressure on client
31        time.sleep(5)
32
33        bytes_received = 0
34
35        # Read request from client on every iteration
36        for i, request in enumerate(request_iterator, start=1):
37            bytes_received += len(request.name)
38            if (i % 10) == 0:
39                print(
40                    f"{helpers.get_current_time()}   "
41                    f"Request {i}:   Received {bytes_received} bytes in total"
42                )
43
44            # Simulate server "work"
45            time.sleep(1)
46
47            # Send a response
48            msg = "Hello!"
49            yield helloworld_pb2.HelloReply(message=msg)
50
51            if (i % 10) == 0:
52                print(
53                    f"{helpers.get_current_time()}   "
54                    f"Request {i}:   Sent {bytes_received} bytes in total\n"
55                )
56
57
58def serve():
59    server = grpc.server(
60        futures.ThreadPoolExecutor(max_workers=10),
61        # Setting server options to minimal, to allow low number of maximum
62        # bytes in the window.
63        # `bdp_probe` is set to 0(false) to disable resizing of window
64        options=[
65            ("grpc.http2.max_frame_size", 16384),
66            ("grpc.http2.bdp_probe", 0),
67            ("grpc.max_concurrent_streams", 1),
68        ],
69    )
70    helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
71
72    server.add_insecure_port("[::]:" + _PORT)
73    server.start()
74    print("Server started, listening on " + _PORT)
75    server.wait_for_termination()
76
77
78if __name__ == "__main__":
79    logging.basicConfig()
80    serve()
81